Recyclerview 项目在滚动时重置
Recyclerview items get reset on scrolling
所以,这是我的问题。我的 recyclerview 项目底部有一个视图,我最初将其设置为 GONE。现在,当它们被点击时,我想让它们再次可见。所以在 onClick 方法中我将视图设置为可见。一切都很好,但是当我向下滚动并向上滚动时,视图再次被隐藏。我想这与 ViewHolder 模式有关。我想保持状态不变,即打开。我该怎么做?谢谢。
视图持有者:
public static class CustomCardViewHolder extends RecyclerView.ViewHolder {
View mCard;
View mFooter;
ImageView mIcon;
TextView mTitle;
TextView mSummary;
public CustomCardViewHolder(View view) {
super(view);
mCard = view.findViewById(R.id.container);
mCard.setTag(this);
mFooter = view.findViewById(R.id.footer); // view to be shown or hidden
mIcon = (ImageView) view.findViewById(R.id.icon);
mTitle = (TextView) view.findViewById(R.id.title);
mSummary = (TextView) view.findViewById(R.id.summary);
}
点击时:
@Override
public void onClick(View view) {
CustomCardViewHolder holder = (CustomCardViewHolder) view.getTag();
if(holder.mFooter.getVisibility() == View.GONE) {
expand(holder.mFooter); // this is just an animation and I'm setting the visibility to visible
notifyItemChanged(holder.getPosition());
notifyAll();
} else {
collapse(holder.mFooter); // similarly this too
notifyItemChanged(holder.getPosition());
notifyAll();
}
}
编辑:上传的代码。此外,我尝试在 onClick 中更新项目的布尔值并在 BindViewHolder 上强制执行它。问题是我在工具栏后面有一种假视图(保险杠)。当我展开 recyclerview 底部的项目并再次向上滚动时,它变得不可见。随着我不断滚动 recyclerview,它逐渐开始出现。
我的activityxml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/widget_bumper" />
<include layout="@layout/widget_recyclerview"/>
<include layout="@layout/widget_toolbar" />
</FrameLayout>
还有我的保险杠:
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bumper"
android:layout_width="match_parent"
android:layout_height="@dimen/widget_bumper_height"
android:background="?colorPrimary" >
</View>
是的,你想得对,你必须保留一些标志来确定视图中的哪些项目可见,哪些不可见。根据您必须设置 View.VISIBLE 或 View.GONE.
只要试一试,你就会成功。如果没有,请分享代码,我会告诉你该怎么做。
已将 RecyclerView 更新到最新的库。这解决了问题。
所以,这是我的问题。我的 recyclerview 项目底部有一个视图,我最初将其设置为 GONE。现在,当它们被点击时,我想让它们再次可见。所以在 onClick 方法中我将视图设置为可见。一切都很好,但是当我向下滚动并向上滚动时,视图再次被隐藏。我想这与 ViewHolder 模式有关。我想保持状态不变,即打开。我该怎么做?谢谢。
视图持有者:
public static class CustomCardViewHolder extends RecyclerView.ViewHolder {
View mCard;
View mFooter;
ImageView mIcon;
TextView mTitle;
TextView mSummary;
public CustomCardViewHolder(View view) {
super(view);
mCard = view.findViewById(R.id.container);
mCard.setTag(this);
mFooter = view.findViewById(R.id.footer); // view to be shown or hidden
mIcon = (ImageView) view.findViewById(R.id.icon);
mTitle = (TextView) view.findViewById(R.id.title);
mSummary = (TextView) view.findViewById(R.id.summary);
}
点击时:
@Override
public void onClick(View view) {
CustomCardViewHolder holder = (CustomCardViewHolder) view.getTag();
if(holder.mFooter.getVisibility() == View.GONE) {
expand(holder.mFooter); // this is just an animation and I'm setting the visibility to visible
notifyItemChanged(holder.getPosition());
notifyAll();
} else {
collapse(holder.mFooter); // similarly this too
notifyItemChanged(holder.getPosition());
notifyAll();
}
}
编辑:上传的代码。此外,我尝试在 onClick 中更新项目的布尔值并在 BindViewHolder 上强制执行它。问题是我在工具栏后面有一种假视图(保险杠)。当我展开 recyclerview 底部的项目并再次向上滚动时,它变得不可见。随着我不断滚动 recyclerview,它逐渐开始出现。
我的activityxml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/widget_bumper" />
<include layout="@layout/widget_recyclerview"/>
<include layout="@layout/widget_toolbar" />
</FrameLayout>
还有我的保险杠:
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bumper"
android:layout_width="match_parent"
android:layout_height="@dimen/widget_bumper_height"
android:background="?colorPrimary" >
</View>
是的,你想得对,你必须保留一些标志来确定视图中的哪些项目可见,哪些不可见。根据您必须设置 View.VISIBLE 或 View.GONE.
只要试一试,你就会成功。如果没有,请分享代码,我会告诉你该怎么做。
已将 RecyclerView 更新到最新的库。这解决了问题。