问题
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,
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.
Image One Here
Image Two Here
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);
}
}
});
回答1:
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.
回答2:
Here is my suggestion, instead of using RecyclerView
, you have to try ExpandableListView
. The example for ExpandableListView
is here and if you want single child in list then set:
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
回答3:
First off all make a clicklistener of your adapter by following procedure:
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;
}}
make a interface class:
// 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);
}
Then in your activity implements with this RecycleListener it will give you override method
@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
}
来源:https://stackoverflow.com/questions/45605589/android-recyclerview-row-visibility-not-working-properly