问题描述
我想在我的交错网格视图中实现加载更多功能.我已经尝试了一些代码行,比如使用 addOnScrollListener
但当我到达列表底部时没有调用.
请找到我试图实现加载更多功能的代码,但是没有得到预期的结果.
I want to implement the load more functionality inside my Staggered gridview. I have tried some lines of code for it like using addOnScrollListener
but did not call when i come to the bottom the list.
Please find my code which i have tried to implement the load more functionality but not getting the expected result.
MY_STRAGGED_RECYCLIVIEW.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = staggeredGridLayoutManager.getItemCount();
lastVisibleItem = staggeredGridLayoutManager
.findLastCompletelyVisibleItemPositions(null)[0];
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
System.out.println("I amm here dd ");
loading = true;
}
}
});
在我上面的代码中,System.out 没有打印...
In my above code, System.out is not getting print...
我已经为 setOnScrollChangeListener
的 stragged gridview 尝试了另一个侦听器,但它也不起作用
I have tried another listener for the stragged gridview that is setOnScrollChangeListener
, but it is also not working
MY_STRAGGED_RECYCLIVIEW.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
System.out.println("I amm here fffffffffff ");
}
});
这个监听器出现了同样的问题,无法打印我的 System.out
Same problem arises with this listener , not getting to print my System.out
关于我在 onBindViewHolder
方法中的 adapter
类中尝试过的更多内容
On more thing which i have tried in my adapter
class inside the onBindViewHolder
method that is
if(getCount()==position)
{
////for getting the last item of the recycleview
}
以上代码也不起作用..请帮我解决这个问题..谢谢:)
Above code is also not working..Please help me to short out from this problem..Thanks :)
@Abbas 请检查下面我的适配器代码
@Abbas please check my adapter code below
public class StraggredView extends RecyclerView.Adapter<StraggredView.ViewHolder> {
private List<Content> mDataSet;
private Context ctx;
public StraggredView(Context context, List<Content> arrList) {
ctx = context;
mDataSet = arrList;
}
@Override
public StraggredView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if(getItemCount()==position)
{
//// I AM NOT GETTING IT AT THE BOTTOM... IT INVOKED AS THE ADAPTER IS CALLED FIRST TIME...LOOK AT IT
}
if (mDataSet.get(position).getContentImage() == null || mDataSet.get(position).getContentImage().isEmpty()) {
Glide.with(ctx).load(R.drawable.no_content)
// .override(screenWidth / 2, Utils.dpToPx(height))
.into(holder.imgContent);
} else {
Glide.with(ctx).load(mDataSet.get(position).getContentImage())
// .override(screenWidth / 2, Utils.dpToPx(height))
// .centerCrop()
//.transform(new CircleTransform())
.into(holder.imgContent);
}
holder.imgContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDataSet.get(position).getContentImage() != null
|| !mDataSet.get(position).getContentImage().isEmpty()) {
FragmentActivity activity = (FragmentActivity) (ctx);
FragmentManager fm = activity.getSupportFragmentManager();
FullScreenFragment dialog = FullScreenFragment.newInstance(
mDataSet.get(position).getContentImage());
dialog.show(fm, "dialog");
}
}
});
}
@Override
public int getItemCount() {
return this.mDataSet.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgContent;
public ViewHolder(View itemView) {
super(itemView);
imgContent = (ImageView) itemView.findViewById(R.id.imgContent);
}
}
}
推荐答案
实际上我使用的是 NestedScrollView
,它是我的 Staggered recycleviewview
/code> .因此 addOnScrollListener
侦听器和 setOnScrollChangeListener
在其中不起作用..
我在 NestedScrollView
中使用了 setOnScrollChangeListener
并且效果很好.检查我的以下解决方案:-
Actually i was using the NestedScrollView
which was the parent view
of my Staggered recycleview
.Therefore addOnScrollListener
listener and setOnScrollChangeListener
was not working in it..
I have used setOnScrollChangeListener
in a NestedScrollView
and it worked fine. Check my below solution for it:-
NestedScrollView myNestedScroll= (NestedScrollView) findViewById(R.id.myNestedScroll);
myNestedScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > oldScrollY) {
Log.i(TAG, "Scroll DOWN");
}
if (scrollY < oldScrollY) {
Log.i(TAG, "Scroll UP");
}
if (scrollY == 0) {
Log.i(TAG, "TOP SCROLL");
}
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
}
}
});
这篇关于在 NestedScrollView 中加载更多功能的交错 Gridview (Recycleview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!