我有一个RecyclerViewAdapter,它在OnCLick中实现了一个expands方法,但在最后一个项目中展开,不起作用。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {
private static String LOG_TAG = "MyRecylerViewAdapter";
private ArrayList<Person> mDataset;
private static MyClickListener myClickListener;
private int mExpandedPosition;
private ViewGroup viewGroup;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
TextView dateTime;
LinearLayout txtDetails;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
dateTime = (TextView) itemView.findViewById(R.id.textView2);
txtDetails = (LinearLayout) itemView.findViewById(R.id.txtDetails);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<Person> myDataset) {
mDataset = myDataset;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_row, parent, false);
viewGroup = parent;
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
//Para expandir
mExpandedPosition = -1;
return dataObjectHolder;
}
@Override
public void onBindViewHolder(final DataObjectHolder holder, final int position) {
holder.label.setText(mDataset.get(position).getCompletName());
holder.dateTime.setText("" + mDataset.get(position).getAge());
final boolean isExpanded = position==mExpandedPosition;
holder.txtDetails.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
// TransitionManager.beginDelayedTransition(viewGroup, new Fade());
notifyDataSetChanged();
}
});
}
public void addItem(Person dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
有人有同样的问题吗?
有任何建议吗?
感谢。
PD:对不起我的英语技能。