gpt4 book ai didi

android - 如何在 nestedscrollview 中使用无限滚动的 recyclerview?

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:31 26 4
gpt4 key购买 nike

屏幕高度不够,因为一个屏幕上有很多包含两个recyclerviews的item。所以我试图在 nestedscrollview 中有一个 subview 。但是,所有元素一次装载,或者回收商不回收。

所以我阅读了其他文章并尝试过但它对我不起作用。

例如添加 app:layout_behavior="@string/appbar_scrolling_view_behavior"mRecyclerView.setNestedScrollingEnabled(false);

如果你知道怎么做,请告诉我。感谢阅读。

这是我的java代码

myDataset = new ArrayList<>();
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.map_recycler_view);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mcontext);
mLayoutManager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(this);
mAdapter.setLinearLayoutManager(mLayoutManager);
mAdapter.setRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(mAdapter);

这是我的xml代码

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<fragment
android:id="@+id/googleMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">



<android.support.v7.widget.RecyclerView
android:id="@+id/tag_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layoutManager="LinearLayoutManager">

</android.support.v7.widget.RecyclerView>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<android.support.v7.widget.RecyclerView
android:id="@+id/map_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>

</LinearLayout>

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

这是我的适配器代码太多所以我上传相关部分当到达回收站 View 的底部时,它会添加一个新项目。

public interface OnLoadMoreListener {
void onLoadMore();
}

public MyAdapter(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
mDataset = new ArrayList<>();
}

public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}

public void setRecyclerView(final RecyclerView mView) {
mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);

if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();

if (!isMoreLoading && (totalItemCount - visibleItemCount)<= (firstVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
isMoreLoading = true;
}
}
}
}
});
}

@Override
public int getItemViewType(int position) {
return mDataset.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM) {
context = parent.getContext();
return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_contents, parent, false));
} else {
context = parent.getContext();
return new ProgressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
}
}

public void addAll(List<mainitem> lst) {
mDataset.clear();
mDataset.addAll(lst);
notifyDataSetChanged();
}

public void addItemMore(List<mainitem> lst) {
mDataset.addAll(lst);
notifyItemRangeChanged(mDataset.size()-6, mDataset.size());
}


public void setMoreLoading(boolean isMoreLoading) {
this.isMoreLoading=isMoreLoading;
}

@Override
public int getItemCount() {
return mDataset.size();
}

public void setProgressMore(final boolean isProgress) {
if (isProgress) {
new Handler().post(new Runnable() {
@Override
public void run() {
mDataset.add(null);
notifyItemInserted(mDataset.size() - 1);
}
});
} else if(!isProgress) {
mDataset.remove(mDataset.size() - 1);
notifyItemRemoved(mDataset.size()-1);
}
}

最佳答案

android:nestedScrollingEnabled="false" 用于您的 RecyclerView

<android.support.v7.widget.RecyclerView
android:id="@+id/map_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:nestedScrollingEnabled="false"
app:layoutManager="LinearLayoutManager"/>

关于android - 如何在 nestedscrollview 中使用无限滚动的 recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49448937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com