从RecyclerView.ViewHolder调用notifyItemChanged(int position)或等效函数

时间:2017-12-24 18:17:56

标签: android android-recyclerview android-viewholder

我的RecyclerView项目包含包含4个TextView的视图。我通过实施View.GONE将2个TextView的View.visibility分别设置为View.VISIBILITYonClick(View view),从而“折叠”并“展开”每个视图项。我知道在崩溃/扩展后我必须调用notifyItemChanged(int position)适配器的RecyclerView函数,但问题是position的{​​{1}}视图项无法(通常)从其ViewHolder访问

作为解决方法,我在视图项中创建了第5个TextView来保存项目的position。我从传递给onClick()的视图中访问此textview,从而获取用于调用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();
    }
}

1 个答案:

答案 0 :(得分:1)

您不必使用其他文字视图来存储该位置。相反,使用

getAdapterPosition()

为您提供当前adapter位置。

你的onClick应该是这样的

@Override
        public void onClick(View view){
            if(descTv.getVisibility()==View.GONE){
                expandView();
            }
            else collapseView();
            notifyViewToggle(getAdapterPosition());
        }

并将notifyViewToggle修改为此

notifyViewToggle(int position)