2017-12-24 34 views
0

RecyclerView 항목에는 4 개의 TextView가있는보기가 포함되어 있습니다. onClick(View view)을 구현하여 각각 TextViews의 View.visibility를 View.GONEView.VISIBILITY으로 설정하여 각보기 항목을 "축소"하고 "확장"합니다. 나는 내 내 RecyclerView 어댑터의 기능을 붕괴/확장 후 호출해야한다는 것을 알고 있지만, 문제는 그 ViewHolder에서 (일반적으로)보기 항목의 position에 액세스 할 수 없다는 것입니다.RecyclerView.ViewHolder에서 notifyItemChanged (int position) 또는 동등 물을 호출하십시오.

해결 방법으로, 항목의 position을 보유 할보기 항목에 다섯 번째 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(); 
    } 
} 

답변

1

: 항목의 위치를 ​​저장하기 위해 뷰를 사용하면

여기 내 코드의 중요한 부분을의 좋은 생각이 확실히 아니다. 대신

getAdapterPosition() 

을 사용하면 현재 adapter 위치를 얻을 수 있습니다.

당신의 온 클릭이

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

모습이

notifyViewToggle(int position) 
에 notifyViewToggle을 수정해야