问题描述
如何在RecyclerView中实现setOnScrollListener
嗨,我是android的新手,我对无限滚动有更多关于lode的查询,请给我一些解决我查询的方法。
我有此功能mAdapter.setOnLoadMoreListener(new OnLoadMoreListener()
,我想放一些整数,如1、2,然后接下来的20个数据将自动从server中显示。
我的代码如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.latest_upload_ten, container, false);
handler = new Handler();
recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
progressBar = (ProgressBar) rootView.findViewById(R.id.progress_bar);
return rootView;
}
public static void setAdapter(final Context context) {
if (recyclerView != null)
recyclerView.setVisibility(View.VISIBLE);
Settings.loading = false;
Utils.e("MessageFragment 106", "setAdapter ok");
mAdapter = new TopRankedAdapter(adapterList, recyclerView, R.layout.video_items, 1);
Utils.e("MessageFragment 109", "setAdapter ok");
recyclerView.setAdapter(mAdapter);
Utils.e("MessageFragment 109", "setAdapter ok");
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
//add null , so the adapter will check view_type and show progress bar at bottom
adapterList.add(null);
mAdapter.notifyItemInserted(adapterList.size() - 1);
++pageNumber;
Log.e("ee","dd"+pageNumber);
}
});
}
public static void loadData(Context context, List<HashMap<String, Object>> viewList, String mode) {
Utils.e("TopRankedFragment144", "loadData");
Utils.e("mode= ", "www "+mode);
if (viewList != null && viewList.size() > 0) {
try {
if (adapterList == null) {
Utils.e("TopRankedFragment148", "TopRankedFragmentList else");
adapterList = new ArrayList<OfficeData>();
} else {
adapterList.clear();
}
for (int i = 0; i < viewList.size(); i++) {
HashMap<String, Object> mp = new HashMap<String, Object>();
mp = viewList.get(i);
Utils.e("TopRankedFragment157", "i " + i);
if (!adapterList.contains(mp))
adapterList.add(new OfficeData(mp, 1));
}
} catch (Exception e) {
Utils.e("TopRankedFragment 166", "Exception======================Exception======================Exception");
e.printStackTrace();
} finally {
setAdapter(context);
}}
}
谢谢你的帮助 :)
1楼
资料来源: 和
/**
* A child class shall subclass this Adapter and
* implement method getDataRow(int position, View convertView, ViewGroup parent),
* which supplies a View present data in a ListRow.
*
* This parent Adapter takes care of displaying ProgressBar in a row or
* indicating that it has reached the last row.
*
*/
public abstract class GenericAdapter<T> extends BaseAdapter {
// the main data list to save loaded data
protected List<T> dataList;
protected Activity mActivity;
// the serverListSize is the total number of items on the server side,
// which should be returned from the web request results
protected int serverListSize = -1;
// Two view types which will be used to determine whether a row should be displaying
// data or a Progressbar
public static final int VIEW_TYPE_LOADING = 0;
public static final int VIEW_TYPE_ACTIVITY = 1;
public GenericAdapter(Activity activity, List<T> list) {
mActivity = activity;
dataList = list;
}
public void setServerListSize(int serverListSize){
this.serverListSize = serverListSize;
}
/**
* disable click events on indicating rows
*/
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) == VIEW_TYPE_ACTIVITY;
}
/**
* One type is normal data row, the other type is Progressbar
*/
@Override
public int getViewTypeCount() {
return 2;
}
/**
* the size of the List plus one, the one is the last row, which displays a Progressbar
*/
@Override
public int getCount() {
return dataList.size() + 1;
}
/**
* return the type of the row,
* the last row indicates the user that the ListView is loading more data
*/
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return (position >= dataList.size()) ? VIEW_TYPE_LOADING
: VIEW_TYPE_ACTIVITY;
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? dataList
.get(position) : null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? position
: -1;
}
/**
* returns the correct view
*/
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
// display the last row
return getFooterView(position, convertView, parent);
}
View dataRow = convertView;
dataRow = getDataRow(position, convertView, parent);
return dataRow;
};
/**
* A subclass should override this method to supply the data row.
* @param position
* @param convertView
* @param parent
* @return
*/
public abstract View getDataRow(int position, View convertView, ViewGroup parent);
/**
* returns a View to be displayed in the last row.
* @param position
* @param convertView
* @param parent
* @return
*/
public View getFooterView(int position, View convertView,
ViewGroup parent) {
if (position >= serverListSize && serverListSize > 0) {
// the ListView has reached the last row
TextView tvLastRow = new TextView(mActivity);
tvLastRow.setHint("Reached the last row.");
tvLastRow.setGravity(Gravity.CENTER);
return tvLastRow;
}
View row = convertView;
if (row == null) {
row = mActivity.getLayoutInflater().inflate(
R.layout.progress, parent, false);
}
return row;
}
}
或者你可以试试这个