Android:RecyclerView行可见性无法正常工作

时间:2017-08-10 06:00:17

标签: android android-studio android-recyclerview android-adapter

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

我想做以下事情:

每一行都有两部分标题&的详情

  1. 当此活动打开所有行时,应隐藏详细信息。
  2. 当我点击特定行时,该行的详细信息应显示为 可见性可见。
  3. 当我再次点击行详细信息时,Visibility GONE将消失。

    问题是:如果我点击了第一行,则会显示第一行的详细信息 单击下一行任何行标题时不隐藏第一行的详细信息。我有 单击两次以显示下一行的详细信息或隐藏详细信息 下一行。

    Image One Here

    Image Two Here

  4. 适配器代码:

    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);
    
                    }
                }
            });
    

4 个答案:

答案 0 :(得分:2)

如果没有扩展的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);
        }

    }
}

您的BEAN课程 我只考虑了标题,你也可以在这里添加细节

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++;
    }
}

最后的答案会是这样的 enter image description here

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

答案 1 :(得分:0)

以下是我的建议,您必须尝试RecyclerView,而不是使用ExpandableListViewExpandableListView的示例是here,如果您想要列表中的单个子项,请设置:

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
} 

答案 2 :(得分:0)

首先,按照以下步骤制作适配器的clicklistener:

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ViewHolder(View itemView) {
            super(itemView);
itemView.setOnClickListener(this);
 @Override
        public void onClick(View view) {
            if (clicklistener != null) {
                clicklistener.itemClicked("adapter intials", view, getAdapterPosition());
            }
        }
    }

    public void setClickListener(RecycleListener clicklistener) {
        this.clicklistener = clicklistener;
    }}

创建一个接口类:

    // interface designed to handle list view click events
public interface RecycleListener {

    // adapterPage argument for identifying the adapter it is clicked from.
    void itemClicked(String adapterPage, View view, int position);
//    public void selfItemClicked(View view, int position);
}

然后在您的活动实现中使用此RecycleListener 它会给你覆盖方法

@Override
    public void itemClicked(final String adapterType, View view, final int position) {
    //here control visibility of your layout using position and view
    // find your view using this view
}

答案 3 :(得分:0)

尝试在代码中添加一个标志,然后基于该标志在onBindViewHolder中添加可见性。

@Override
public void onBindViewHolder(@NonNull final ProductViewHolder holder, final int position) {
holder.linearLayoutItemButton.setVisibility(canShow(productOrderList.get(position))?
            View.VISIBLE : View.GONE);
holder.addButton.setVisibility(canShow(productOrderList.get(position))?
            View.GONE : View.VISIBLE);