我有一个良好设置的可扩展 Recyclerview。当点击任何项目时,它会展开以显示更多细节。但我想在这里做出一些改变。当单击一个项目以展开时,所有其他项目都应该折叠。这是我的适配器代码:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<ListItem> android_versions;
private List<ListItem> listItems;
private LayoutInflater layoutInflater;
private Animation animationUp, animationDown;
private Context context;
private final int COUNTDOWN_RUNNING_TIME = 500;
int i =0;
public DataAdapter(Context context, java.util.List<ListItem> android_versions,
Animation animationUp, Animation animationDown) {
this.context = context;
this.listItems = android_versions;
this.animationDown = animationDown;
this.animationUp = animationUp;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(view);
}
public String getRandomColor(int a){
String colors[] = {"#ef5350","#f44336","#ef5350"};
return colors[a];
}
@Override
public void onBindViewHolder(final ViewHolder holder, int i) {
ListItem listItem = listItems.get(i);
if (i%2==2)
holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
else if (i%2==1)
holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
else if (i%2==0)
holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
holder.tv_android.setText(listItem.getSubName());
holder.contentLayout.setText(listItem.getContent());
holder.expandit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.contentLayout.isShown()) {
holder.contentLayout.startAnimation(animationUp);
CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
holder.contentLayout.setVisibility(View.GONE);
}
};
countDownTimerStatic.start();
} else {
holder.contentLayout.setVisibility(View.VISIBLE);
holder.contentLayout.startAnimation(animationDown);
}
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tv_android;
private TextView contentLayout;
RelativeLayout expandit;
public ViewHolder(View view) {
super(view);
tv_android = (TextView)view.findViewById(R.id.tv_android);
contentLayout = (TextView) view.findViewById(R.id.content);
expandit = (RelativeLayout)view.findViewById(R.id.expandit);
}
}
}
我认为无法通过位置更改viewHolder的内容。那么我如何通过位置隐藏所有其他项目的内容?我认为不需要其他代码,因此我没有包含它。
编辑:在Ashish建议之后的代码:
private void changeStateOfItemsInLayout(ListItem listItem) {
for (int i = 0; i < listItems.size(); i++) {
if (listItems.get(i) == listItem)
listItem.setShouldBeExpanded(true);
else
listItem.setShouldBeExpanded(false);
}
}
@Override
public void onBindViewHolder(final ViewHolder holder, int i) {
ListItem listItem = listItems.get(i);
if (i%2==2)
holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
else if (i%2==1)
holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
else if (i%2==0)
holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
holder.tv_android.setText(listItem.getSubName());
holder.contentLayout.setText(listItem.getContent());
if(listItem.getShouldBeExpanded()){
holder.contentLayout.setVisibility(View.VISIBLE);
holder.contentLayout.startAnimation(animationDown);
}else{
holder.contentLayout.startAnimation(animationUp);
CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
holder.contentLayout.setVisibility(View.GONE);
}
};
countDownTimerStatic.start();
}
holder.expandit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.contentLayout.isShown()) {
holder.contentLayout.startAnimation(animationUp);
CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
holder.contentLayout.setVisibility(View.GONE);
}
};
countDownTimerStatic.start();
} else {
holder.contentLayout.setVisibility(View.VISIBLE);
holder.contentLayout.startAnimation(animationDown);
}
listItems.get(holder.getLayoutPosition()).setShouldBeExpanded(true);
changeStateOfItemsInLayout(listItems.get(holder.getLayoutPosition()));
notifyDataSetChanged();
}
});
}
虽然在onClick中,我仍然有展开/折叠布局的代码,但即使我删除它,它也不起作用。
如果需要,这是我的模态框类:
public class ListItem {
private String subName;
private String content;
private String param;
private Boolean shouldBeExpanded=false;
public Boolean getShouldBeExpanded() {
return shouldBeExpanded;
}
public void setShouldBeExpanded(Boolean shouldBeExpanded) {
this.shouldBeExpanded = shouldBeExpanded;
}
public ListItem(String subName, String content) {
this.subName = subName;
this.content = content;
this.param = "Nothing";
}
public String getParam() {
return param;
}
public ListItem(String subName, String content, String param) {
this.subName = subName;
this.content = content;
this.param = param;
}
public String getSubName() {
return subName;
}
public String getContent() {
return content;
}
}
编辑:根据要求,以下是animationUp文件和animationDown文件的代码:
animationUp.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
animationDown.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>