Android:RecyclerView行可见性无法正常工作 [英] Android : RecyclerView row Visibility not working properly

查看:75
本文介绍了Android:RecyclerView行可见性无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用(Android Studio).我想在每行中实现Visibility GONE/VISIBLE.图片中有三行recyclerView行,

I am developing an android app (Android Studio). I want to implement Visibility GONE/VISIBLE in every row. In the image there are three recyclerView rows,

我想做以下事情:

每行有两个部分标题详细信息.

  1. 当本活动"打开时,所有行的详细信息都应隐藏.
  2. 当我单击特定行时,该行的详细信息应显示为 可见性.
  3. 当我再次单击行时,可见性消失"将使详细信息消失.

  1. When This Activity open all the rows details should hide.
  2. When I click on particular row , details of the row should appear by Visibility VISIBLE.
  3. When i again click on row details should disappear by Visibility GONE.

问题是:如果我单击第一行,则显示第一行的详细信息,然后 单击任何下一行标题时,不隐藏第一行的详细信息.我有 单击两次以显示下一行的详细信息,或隐藏以下内容的详细信息 下一行.

Problem is : If i clicked on first row, Details of first row is showing and without hiding details of first row when click on next any row Heading. I have to click two times to show the details of the next row or Hide the details of next row.

此处的图片

此处的图片二

适配器代码:

int i = 0;
holder.salt_head_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                if (i % 2 == 1) {
                    holder.have_show_ll.setVisibility(View.VISIBLE);
                    holder.l1_arrow.setImageResource(R.drawable.down_arrow_black);

                } else if (i % 2 == 0) {
                    holder.have_show_ll.setVisibility(View.GONE);
                    holder.l1_arrow.setImageResource(R.drawable.right_arrow_black);

                }
            }
        });

推荐答案

这是无需扩展listView即可实现的方法

Here is how you can achieve it without extended listView

您的卡片或itemView类似于

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:id="@+id/tvTitle"
        />

    <!--this is your detail view layout, modify it accordingly-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/detail"
        android:visibility="gone">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="16sp"
            android:text="detail goes here\n and here\n and here"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#747474"/>
</LinearLayout>

您的适配器

public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.ViewHolder>{

    private List<TextBean> data;
    private Context context;

    public SecondAdapter(Context context, List<TextBean> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.card_second, parent , false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final TextBean textBean = data.get(position);
        holder.title.setText(textBean.getTitle());
        if(textBean.isVisibility())
            holder.detailLayout.setVisibility(View.VISIBLE);
        else
            holder.detailLayout.setVisibility(View.GONE);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(textBean.isVisibility()){
                    textBean.setVisibility(false);
                    holder.detailLayout.setVisibility(View.GONE);
                    notifyDataSetChanged();
                }else{
                    textBean.setVisibility(true);
                    holder.detailLayout.setVisibility(View.VISIBLE);
                    notifyDataSetChanged();
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public LinearLayout detailLayout;
        public ViewHolder(View item){
            super(item);

            title = (TextView)item.findViewById(R.id.tvTitle);
            detailLayout = (LinearLayout) item.findViewById(R.id.detail);
        }

    }
}

您的豆类课程 我只考虑了标题,您也可以在此处添加详细信息

YOUR BEAN class I have taken only title into account you can add details here also

public class TextBean {

    private String title;
    private boolean visibility; // donot forget this, this is to handle visibility

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isVisibility() {
        return visibility;
    }

    public void setVisibility(boolean visibility) {
        this.visibility = visibility;
    }
}

如何将此附加到RecyclerView

public class SecondActivity extends AppCompatActivity {

    List<TextBean> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        populatedata(); // populate your data here
        SecondAdapter secondAdapter = new SecondAdapter(this,data);
        recyclerView.setAdapter(secondAdapter);
    }

    public void populatedata(){
        int count = 1;
        TextBean textBean = new TextBean();
        textBean.setTitle("Title"+count);
        textBean.setVisibility(false); // keep them false in beginning
        data.add(textBean);
        count++;

        textBean = new TextBean();
        textBean.setTitle("Title"+count);
        textBean.setVisibility(false);
        data.add(textBean);
        count++;

        textBean = new TextBean();
        textBean.setTitle("Title"+count);
        textBean.setVisibility(false);
        data.add(textBean);
        count++;

        textBean = new TextBean();
        textBean.setTitle("Title"+count);
        textBean.setVisibility(false);
        data.add(textBean);
        count++;

        textBean = new TextBean();
        textBean.setTitle("Title"+count);
        textBean.setVisibility(false);
        data.add(textBean);
        count++;

        textBean = new TextBean();
        textBean.setTitle("Title"+count);
        textBean.setVisibility(false);
        data.add(textBean);
        count++;
    }
}

最终答案看起来像

这是完整的解决方案.如果您有任何疑问可以问.

this is the complete solution. if there are any doubts you can ask.

这篇关于Android:RecyclerView行可见性无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆