首页 > 解决方案 > 如何在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;
    .........
    ......... 

标签: androidandroid-recyclerview

解决方案


您只需在适配器中添加字段即可保存当前活动项目。然后调用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;
}

我创建了一个测试项目来展示它是如何实现的。在这里你可以看到它是如何工作的。是我的 github 存储库。


推荐阅读