android - 如何在recyclerView中显示和隐藏imageview
问题描述
我正在使用 for 循环,我在回收站视图中显示了大约 20 个项目!现在我如何在 for 循环运行时隐藏/显示图像视图...这里 tts 工作正常,但是当我尝试使用它显示/隐藏时它没有发生..
目前使用下面的代码,一旦 for 循环结束整个 imageview 正在影响,我想要它一行 wis(显示当前(image1)和隐藏上一个 imageview(image))
我从主 Activity 类调用此方法,但 imageview 显示或隐藏没有发生
活动
......
......
private void ConvertTextToSpeech() {
// TODO Auto-generated method stub
//items.forEach( Multiples obj -> System.out.println());
int z=0;
View holder=null; ImageView imageView=null;ImageView imageView1=null;
for (Multiples p : items) {
if(z>0){
holder = recyclerView.findViewHolderForAdapterPosition(z).itemView;
holder.findViewById(R.id.image).setVisibility(View.VISIBLE);
holder.findViewById(R.id.image1).setVisibility(View.INVISIBLE);
}
if(z < items.size()) {
holder = recyclerView.findViewHolderForAdapterPosition(z).itemView;
holder.findViewById(R.id.image).setVisibility(View.INVISIBLE);
holder.findViewById(R.id.image1).setVisibility(View.VISIBLE);
}
text = p.first + " " + p.getSecond() + " Za "+p.getResult()+".";
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
z++;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/lyt_parent"
style="@style/RippleStyleBlack"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/grey_10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal"
android:textAlignment="gravity" >
<View
android:layout_width="@dimen/spacing_large"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_arrow_right" />
<ImageView
android:id="@+id/image1"
android:layout_width="51dp"
android:layout_height="35dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_arrow_right"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/spacing_middle"
android:paddingTop="@dimen/spacing_middle">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/spacing_middle"
android:layout_marginRight="@dimen/spacing_middle"
android:fontFamily="sans-serif-condensed"
android:gravity="center|left"
android:text="36"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/grey_80"
android:textSize="30sp"
/>
</LinearLayout>
<View
android:layout_width="@dimen/spacing_large"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/grey_10" />
</LinearLayout>
</com.balysv.materialripple.MaterialRippleLayout>
</RelativeLayout>
适配器
public class AdapterListAnimation extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Multiples> items = new ArrayList<>();
private Context ctx;
private OnItemClickListener mOnItemClickListener;
private int animation_type = 0;
.........
.........
解决方案
您只需在适配器中添加字段即可保存当前活动项目。然后调用notifyDataSetChanged
。你也应该onBindViewHolder
像这样更新你的:
private int currentPosition = -1;
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText(items.get(position));
if (currentPosition == position) {
holder.img1.setVisibility(View.INVISIBLE);
holder.img2.setVisibility(View.VISIBLE);
} else {
holder.img1.setVisibility(View.VISIBLE);
holder.img2.setVisibility(View.INVISIBLE);
}
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
推荐阅读
- fullcalendar - FullLCalendar 5:每个事件源的不同颜色不会显示在月视图中
- javascript - 将获取的数据推送到对象数组
- angularjs - 如何在freemarker宏中使用角度值?
- python - pandas 错误:试图在 DataFrame 中的切片副本上设置值
- c++ - 如何从成员指针模板类型中获取类类型
- flutter - firebase_admob 错误构建失败可能是由于插件中的 AndroidX 不兼容
- python - 无法在 Jupyter notebook 中查看 dt 访问器下的所有方法
- swift - 使用 UIPanGestureRecognizer 获取视图的翻译 - 快速 - 以编程方式
- html - 无法将我的 nodejs 中的数据(在 mysql 上使用 select * 查询后)呈现到 ejs 页面
- typescript - 如何接受泛型元组作为参数并返回包含泛型类型的泛型