我的片段滚动有问题。 当用户到达片段末尾时,我想加载添加列表的另一部分。
片段一:
View inflatedView = inflater.inflate(R.layout.tab1, container, false);
list = (ListView) inflatedView.findViewById(R.id.list);
...
MainActivity.Adapter adapter = new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
list.setAdapter(adapter);
list.setOnScrollListener(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
类适配器:
public static class Adapter extends ArrayAdapter implements AbsListView.OnScrollListener {
....
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Toast.makeText(getContext().getApplicationContext(), "one", Toast.LENGTH_SHORT).show();
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Toast.makeText(getContext().getApplicationContext(), "two", Toast.LENGTH_SHORT).show();
}
}
当我走到页面末尾时,此功能不起作用。错误在哪里?
答案 0 :(得分:0)
我会在列表中使用RecyclerView
并在其上添加滚动侦听器。您需要在回收器视图上保留对布局管理器的引用。
private LinearLayoutManager layoutManager;
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// don't load more unless actually scrolled
if (dx == 0 && dy == 0) {
return;
}
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
// load more items depending on visible items and total
}
};