我正在创建一个RecyclerView
,当用户触摸它时会自动展开,当用户再次触摸它时会自行关闭。以下是我的代码:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecViewHolder> {
ArrayList<String> values;
ArrayList<Integer> expandedPosition;
public static class RecViewHolder extends RecyclerView.ViewHolder {
...
private boolean resultsOpened = false;
...
RelativeLayout favHeaderLayout;
RelativeLayout favResultsLayout;
public RecViewHolder (View itemView) {
super(itemView);
favHeaderLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!resultsOpened) {
expandLayout();
} else {
favResultsLayout.setVisibility(View.GONE);
resultsOpened = false;
}
}
});
}
public void expandLayout() {
favResultsLayout.setVisibility(View.VISIBLE);
resultsOpened = true;
//I populate my expanded layout here
}
}
public RecyclerViewAdapter (Context context, ArrayList<String> values) {
this.context = context;
this.values= values;
}
@Override
public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file, parent, false);
RecViewHolder recViewHolder= new RecViewHolder(v);
return recViewHolder;
}
@Override
public void onBindViewHolder(RecViewHolder recViewHolder , int position) {
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return values.size();
}
}
当我触摸第一个视图时,它应该自行扩展。但是,当适配器有更多数据并且我触摸第一个视图时,视图会扩展,RecyclerView
(例如第8个视图)底部的另一个视图也会展开。如何确保只有被触摸的视图扩展而其他视图保持关闭?
修改
我已经尝试将展开的视图位置存储在arraylist和onBindViewHolder
中,检查哪些视图已展开并仅展开它们:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecViewHolder> {
ArrayList<String> values;
ArrayList<Integer> expandedPosition;
public static class RecViewHolder extends RecyclerView.ViewHolder {
...
private boolean resultsOpened = false;
...
RelativeLayout favHeaderLayout;
RelativeLayout favResultsLayout;
public RecViewHolder (View itemView) {
super(itemView);
}
}
public RecyclerViewAdapter (Context context, ArrayList<String> values) {
this.context = context;
this.values= values;
}
@Override
public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file, parent, false);
RecViewHolder recViewHolder= new RecViewHolder(v);
return recViewHolder;
}
@Override
public void onBindViewHolder(RecViewHolder recViewHolder , int position) {
recViewHolder.favResultsLayout.setVisibility(View.GONE);
for(int i= 0; i < expandedPosition.size(); i ++){
recViewHolder.favResultsLayout.setVisibility(View.VISIBLE);
}
recViewHolder.favHeaderLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!expandedPosition.contains(position)) {
expandLayout(recViewHolder);
expandedPosition.add(position);
} else {
recViewHolder.favResultsLayout.setVisibility(View.GONE);
for(int i = 0; i < expandedPosition.size(); i ++){
if (expandedPosition.get(i) == position){
expandedPosition.remove(i);
}
}
}
}
});
}
public void expandLayout() {
favResultsLayout.setVisibility(View.VISIBLE);
//I populate my expanded layout here
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return values.size();
}
}
仍然没有运气:(
答案 0 :(得分:2)
正如班级名称所说的那样都是关于回收......
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecViewHolder> {
ArrayList<String> values;
SparseBooleanArray expanded = new SparseBooleanArray();
public static class RecViewHolder extends RecyclerView.ViewHolder {
//EDIT!!!!!
public RecViewHolder (View itemView, final RecyclerViewAdapter adapter) {
//rest of the code ...
favHeaderLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int currentPosition = getAdapterPosition();
//is a view at current position expanded?
if(adapter.expanded.get(currentPosition ))
//if so it shouldn't be
adapter.expanded.delete(getAdapterPosition(currentPosition));
else
//if not, expand
adapter.expanded.put(currentPosition, true);
//wheeee inform the adapter to rebind
adapter.notifyItemChanged(currentPosition);
//setupExpanded(expanded.get(currentPosition));
}
});
}
protected void setupExpanded(boolean state) {
//ONLY hide show layouts based on state
}
}
@Override
public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file, parent, false);
RecViewHolder recViewHolder= new RecViewHolder(v, this);
return recViewHolder;
}
@Override
public void onBindViewHolder(RecViewHolder recViewHolder , int position) {
//this line should be self explanatory
recViewHolder.setupExpanded(expanded.get(position));
}
//rest of the code
}