Skip to content

Commit

Permalink
Optimize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadCym committed May 11, 2016
1 parent bf08bc5 commit f01b30b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ public void run() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
DataServer.addData(mQuickAdapter.getData(), PAGE_SIZE);
mQuickAdapter.notifyDataChangedAfterLoadMore(DataServer.getSampleData(PAGE_SIZE),true);
mCurrentCounter = mQuickAdapter.getItemCount();
mQuickAdapter.notifyDataChangedAfterLoadMore(true);

}
}, delayMillis);
}
Expand All @@ -86,7 +84,8 @@ public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
initAdapter();
mQuickAdapter.setNewData(DataServer.getSampleData(PAGE_SIZE));
mCurrentCounter = PAGE_SIZE;
mSwipeRefreshLayout.setRefreshing(false);
}
}, delayMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
public class DefAdpater extends RecyclerView.Adapter<DefAdpater.ViewHolder> {
private final List<Status> sampleData = DataServer.getSampleData(100);
private Context mContext;

private LayoutInflater mLayoutInflater;
public DefAdpater(Context context) {
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.tweet, parent, false);
View item = mLayoutInflater.inflate(R.layout.tweet, parent, false);
return new ViewHolder(item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


Expand Down Expand Up @@ -214,28 +213,25 @@ public void add(int position, T item) {
notifyItemInserted(position);
}


/**
* After clear the adapter's data, add new collection
* setting up a new instance to data;
*
* @param collection
* @param data
*/
public void addAfterClear(Collection<? extends T> collection) {
try {
mData.clear();
mData.addAll(collection);
notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
public void setNewData(List<T> data) {
this.mData = data;
if (mRequestLoadMoreListener != null) mNextLoadEnable = true;
notifyDataSetChanged();
}

/**
* setting up a new instance to data;
* additional data;
*
* @param data
*/
public void setNewData(List<T> data) {
this.mData = data;
public void addData(List<T> data) {
this.mData.addAll(data);
notifyDataSetChanged();
}

Expand Down Expand Up @@ -412,6 +408,12 @@ public void notifyDataChangedAfterLoadMore(boolean isNextLoad) {

}

public void notifyDataChangedAfterLoadMore(List<T> data, boolean isNextLoad) {
mData.addAll(data);
notifyDataChangedAfterLoadMore(isNextLoad);

}


private void addLoadMore(RecyclerView.ViewHolder holder) {
if (isLoadMore()) {
Expand Down

0 comments on commit f01b30b

Please sign in to comment.