Android:RecyclerView 行可见性无法正常工作 [英] Android : RecyclerView row Visibility not working properly
问题描述
我正在开发一个 Android 应用程序 (Android Studio).我想在每一行中实现 Visibility
GONE/VISIBLE.在图像中有三个 recyclerView
行,
我想做以下事情:
每行有两个部分标题 &详情.
- 当此活动打开时,所有行的详细信息都应隐藏.
- 当我单击特定行时,该行的详细信息应显示在可见性可见.
当我再次单击行详细信息时,Visibility GONE 应该会消失.
问题是:如果我单击第一行,则显示第一行的详细信息并且单击下一个任何行标题时,不隐藏第一行的详细信息.我有单击两次以显示下一行的详细信息或隐藏详细信息下一行.
这是完整的解决方案.如果有任何疑问,您可以询问.
I am developing an android app (Android Studio). I want to implement
Visibility
GONE/VISIBLE in every row. In the image there are threerecyclerView
rows,I want to do the following things :
There are two part in every row Heading & Details.
- When This Activity open all the rows details should hide.
- When I click on particular row , details of the row should appear by Visibility VISIBLE.
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.
Adapter Code :
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); } } });
解决方案Here is how you can achieve it without extended listView
Your card or itemView will be something like
<?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>
YOUR adapter
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; } }
How to attach this to 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++; } }
final answer will look like
this is the complete solution. if there are any doubts you can ask.
这篇关于Android:RecyclerView 行可见性无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!