Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
optimize images gridview loading data
Browse files Browse the repository at this point in the history
  • Loading branch information
mcxiaoke committed Mar 9, 2016
1 parent 3d2e4ff commit b61c64d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 96 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/com/mcxiaoke/minicat/app/UIGallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import com.mcxiaoke.minicat.R;
import com.mcxiaoke.minicat.fragment.GalleryFragment;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

Expand Down Expand Up @@ -38,6 +37,12 @@ protected void onCreate(Bundle savedInstanceState) {

}

@Override
protected void onDestroy() {
super.onDestroy();
ImageLoader.getInstance().clearMemoryCache();
}

@Override
public void finish() {
super.finish();
Expand Down
157 changes: 77 additions & 80 deletions app/src/main/java/com/mcxiaoke/minicat/fragment/PhotosFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -40,7 +41,9 @@ public class PhotosFragment extends AbstractFragment implements AdapterView.OnIt
private ArrayAdapter<StatusModel> mArrayAdapter;
private List<StatusModel> mData;
private UserModel user;
private LoadDataTask mLoadDataTask;
private Thread mThread;
private Handler mHandler;
private volatile boolean mCancelled;

public static PhotosFragment newInstance(UserModel user) {
PhotosFragment fragment = new PhotosFragment();
Expand All @@ -53,6 +56,7 @@ public static PhotosFragment newInstance(UserModel user) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
user = getArguments().getParcelable("user");
}

Expand Down Expand Up @@ -121,14 +125,6 @@ private void showProgress() {

private void showContent(int size) {
mEmptyViewController.hideProgress();
if (size < 10) {
// mGridView.setNumColumns(1);
// mGridView.setPadding(mGridViewPaddingMax, mGridViewPaddingMax, mGridViewPaddingMax, mGridViewPaddingMax);
} else {
// mGridView.setVerticalSpacing(mGridViewPaddingMin);
// mGridView.setHorizontalSpacing(mGridViewPaddingMin);
// mGridView.setNumColumns(3);
}
mGridView.setVisibility(View.VISIBLE);
}

Expand All @@ -138,18 +134,84 @@ private void showEmpty(String text) {
}

private void stopTask() {
if (mLoadDataTask != null) {
mLoadDataTask.stop();
mLoadDataTask = null;
if (mThread != null) {
mCancelled = true;
mThread.interrupt();
mThread = null;
}
}

private void startTask() {
stopTask();
mLoadDataTask = new LoadDataTask();
mLoadDataTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
loadData();
}

private void updateUI(final List<StatusModel> data) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
getBaseSupport().hideProgressIndicator();
if (data != null && data.size() > 0) {
int size = data.size();
showContent(size);
mArrayAdapter.addAll(data);
mArrayAdapter.notifyDataSetChanged();
} else {
if (mArrayAdapter.isEmpty()) {
showEmpty("无法获取数据");
}
}
}
};
mHandler.post(runnable);

}

private void loadData() {

mThread = new Thread() {
@Override
public void run() {
final String id = user.getId();
final Api api = AppContext.getApi();
List<StatusModel> statusModels = new ArrayList<StatusModel>();
try {
// 分页获取,最多3页=180张照片
Paging paging = new Paging();
paging.count = 60;
final int max = NetworkHelper.isWifi(getActivity()) ? 10 : 1;
for (int i = 0; i < max; i++) {
paging.page = i;
if (mCancelled) {
break;
}
if (AppContext.DEBUG) {
Log.d("PhotosFragment", "fetch next page photos " + i);
}
final List<StatusModel> models = api.getPhotosTimeline(id, paging);
if (mCancelled) {
break;
}
updateUI(models);
if (models == null || models.size() < 60) {
break;
}
}
if (AppContext.DEBUG) {
Log.d("PhotosFragment", "fetch photos finished");
}
} catch (Exception ex) {
if (AppContext.DEBUG) {
Log.w("PhotosFragment", "fetch photos error:" + ex);
}
updateUI(null);
}
}
};
mThread.start();
}


static class GridViewAdapter extends ArrayAdapter<StatusModel> {
private Picasso picasso;

Expand Down Expand Up @@ -188,69 +250,4 @@ static class ViewHolder {
}
}

private class LoadDataTask extends AsyncTask<Void, Void, List<StatusModel>> {

private volatile boolean mCancelled;
private Exception mException;

public LoadDataTask() {
mCancelled = false;
}

public void stop() {
mCancelled = true;
cancel(true);
}

@Override
protected List<StatusModel> doInBackground(Void... params) {
final String id = user.getId();
final Api api = AppContext.getApi();
List<StatusModel> statusModels = new ArrayList<StatusModel>();
try {
// 分页获取,最多3页=180张照片
Paging paging = new Paging();
paging.count = 60;
final int max = NetworkHelper.isWifi(getActivity()) ? 3 : 1;
for (int i = 0; i < max; i++) {
paging.page = i;
List<StatusModel> models = api.getPhotosTimeline(id, paging);
if (models != null) {
statusModels.addAll(models);
}
if (models == null || models.size() < 60) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
mException = e;
}
return statusModels;
}

@Override
protected void onPostExecute(List<StatusModel> statusModels) {
super.onPostExecute(statusModels);
getBaseSupport().hideProgressIndicator();
if (mCancelled) {
showEmpty("");
} else {
if (statusModels != null && statusModels.size() > 0) {
int size = statusModels.size();
showContent(size);
mArrayAdapter.addAll(statusModels);
mArrayAdapter.notifyDataSetChanged();
} else {
if (mException != null) {
showEmpty("无法获取数据");
} else {
showEmpty("没有数据");
}
}
}

}
}

}
14 changes: 0 additions & 14 deletions app/src/main/res/layout/view_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
<TextView
android:id="@+id/text_title1"
android:text="990"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
style="@style/ProfileItemTextStyle" />

<TextView
android:id="@+id/text_value1"
android:layout_alignParentRight="true"
android:text="Statuses Count"
android:layout_alignParentTop="true"
style="@style/ProfileItemTextMetaStyle" />
</LinearLayout>

Expand All @@ -49,12 +45,10 @@
<TextView
android:id="@+id/text_title2"
android:text="1234"
android:layout_alignParentLeft="true"
style="@style/ProfileItemTextStyle" />

<TextView
android:id="@+id/text_value2"
android:layout_alignParentRight="true"
android:text="Statuses Count"
style="@style/ProfileItemTextMetaStyle" />
</LinearLayout>
Expand All @@ -79,15 +73,11 @@
<TextView
android:id="@+id/text_title3"
android:text="990"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
style="@style/ProfileItemTextStyle" />

<TextView
android:id="@+id/text_value3"
android:layout_alignParentRight="true"
android:text="Statuses Count"
android:layout_alignParentTop="true"
style="@style/ProfileItemTextMetaStyle" />
</LinearLayout>

Expand All @@ -103,12 +93,10 @@
<TextView
android:id="@+id/text_title4"
android:text="1234"
android:layout_alignParentLeft="true"
style="@style/ProfileItemTextStyle" />

<TextView
android:id="@+id/text_value4"
android:layout_alignParentRight="true"
android:text="Statuses Count"
style="@style/ProfileItemTextMetaStyle" />
</LinearLayout>
Expand All @@ -125,8 +113,6 @@
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/header_intro"
android:layout_below="@+id/header_intro"
android:textColor="@color/text_primary"
android:textSize="@dimen/text_size_medium"
android:padding="@dimen/list_padding"
Expand Down

0 comments on commit b61c64d

Please sign in to comment.