私のRecyclerView
アイテムには、4つのTextViewがあるビューが含まれています。 onClick(View view)
を実装して、2 TextViewsのView.visibilityをそれぞれView.GONE
とView.VISIBILITY
に設定して、各ビュー項目を「折りたたんで」展開します。私はnotifyItemChanged(int position)
私のRecyclerView
アダプタの機能を崩壊/拡張後に呼び出さなければならないことを知っているが、問題はposition
の(通常)そのViewHolder
からアクセスすることはできません。RecyclerView.ViewHolderからnotifyItemChanged(int position)または同等のものを呼び出します。
回避策として、アイテムのposition
を保持するためにビューアイテムに5番目のTextView
を作成しました。 onClick()
に渡されたビューからこのテキストビューにアクセスし、notifyItemChanged(position)
を呼び出して画面を更新するために使用するアイテムの位置を取得します。
私のソリューションは機能しますが、私はよりクリーンなソリューションを探しています。確かに良くないアイデアがここに私のコードの重要な部分だアイテムの位置を保存するためにビューを
使用:
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> {
Context context;
List<Notification> notifications;
// Notification is my custom class (POJO)
public NotificationAdapter(Context context, List<Notification> notificationList) {
this.context = context;
this.notifications = notificationList;
}
public class NotificationHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
View view;
//The 4 TextViews
TextView titleTv, descTv, timeTv,extraTv;
//TextView added to hold position of the view item
TextView positionJugad;
public NotificationHolder(View itemView) {
super(itemView);
titleTv = itemView.findViewById(R.id.notification_subject_tv);
descTv = itemView.findViewById(R.id.notification_body_tv);
timeTv = itemView.findViewById(R.id.notification_time_tv);
extraTv = itemView.findViewById(R.id.notification_extra_info_tv);
positionJugad = itemView.findViewById(R.id.notification_view_position);
itemView.setOnClickListener(this);
this.view = itemView;
}
public void bindNotification(Notification notification, int position){
titleTv.setText(notification.getTitle());
descTv.setText(notification.getDescription());
timeTv.setText(notification.getTime();
extraTv.setText(notification.getExtra());
positionJugad.setText(""+position);
}
public void collapseView(){
descTv.setVisibility(View.GONE);
extraTv.setVisibility(View.GONE);
}
public void expandView(){
descTv.setVisibility(View.VISIBLE);
extraTv.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View view){
//get the textview that stores position
TextView jugad = view.findViewById(R.id.notification_view_position);
if(descTv.getVisibility()==View.GONE){
expandView();
}
else collapseView();
notifyViewToggle(Integer.parseInt(jugad.getText().toString()));
}
}
@Override
public NotificationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.notification_item,parent,false);
return new NotificationHolder(view);
}
@Override
public void onBindViewHolder(NotificationHolder holder, int position) {
Notification notification = notifications.get(position);
holder.bindNotification(notification,position);
}
//To change view state from collapsed to expanded and vice versa
public void notifyViewToggle(int position){
notifyItemChanged(position);
}
@Override
public int getItemCount() {
return notifications.size();
}
}