在 NestedScrollView 中加载更多功能的交错 Gridview(Recycleview)
Staggered Gridview (Recycleview) with load more functionality inside the NestedScrollView
我想在交错网格视图中实现加载更多功能。我已经尝试了一些代码行,比如使用 addOnScrollListener
但是当我来到列表底部时没有调用。
请找到我的代码,我试图实现加载更多功能但没有得到预期的结果。
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 没有打印...
我已经为 setOnScrollChangeListener
的 stragged gridview 尝试了另一个监听器,但它也不起作用
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
关于我在 adapter
class 中尝试的 onBindViewHolder
方法中的更多内容,即
if(getCount()==position)
{
////for getting the last item of the recycleview
}
上面的代码也不起作用..请帮我解决这个问题..谢谢:)
@Abbas 请检查下面我的适配器代码
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 recycleview
的父 view
。因此 addOnScrollListener
侦听器和 setOnScrollChangeListener
无法在其中工作..
我在 NestedScrollView
中使用了 setOnScrollChangeListener
并且效果很好。检查我的以下解决方案:-
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");
}
}
});
我想在交错网格视图中实现加载更多功能。我已经尝试了一些代码行,比如使用 addOnScrollListener
但是当我来到列表底部时没有调用。
请找到我的代码,我试图实现加载更多功能但没有得到预期的结果。
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 没有打印...
我已经为 setOnScrollChangeListener
的 stragged gridview 尝试了另一个监听器,但它也不起作用
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
关于我在 adapter
class 中尝试的 onBindViewHolder
方法中的更多内容,即
if(getCount()==position)
{
////for getting the last item of the recycleview
}
上面的代码也不起作用..请帮我解决这个问题..谢谢:)
@Abbas 请检查下面我的适配器代码
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 recycleview
的父 view
。因此 addOnScrollListener
侦听器和 setOnScrollChangeListener
无法在其中工作..
我在 NestedScrollView
中使用了 setOnScrollChangeListener
并且效果很好。检查我的以下解决方案:-
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");
}
}
});