1

私はプロジェクトで拡張可能なrecyclerviewを使用しています。データはFirebaseコンサルティングから提供されています。firebaseからexpandable recyclerview update

私が使用する拡張可能recyclerViewは次のとおりです。bignerdranch

サイトが言うので、私のビューを更新するための最良の方法は何ですか:

をRecyclerView.Adapterの伝統的なnotifyDataSetChanged()にはないことに注意してください

を意図して使用することをお勧めしますように作業

//子の変更

notifyChildInserted(int parentPosition, int childPosition)

...

しかし、私はように変更することをchildPositionsを知りません。

+0

でチェック

@Override public void onClick(View v) { MyHolder holder = (MyHolder) mRecyclerView.getChildViewHolder(v); holder.textView.setText("Clicked!"); } 

をビュー、およびビューホルダーとアダプターの位置を表示することができます。非常に長いリストでない場合は、最初の位置からクリックした位置まで反復することができます(parent?count = 0:count ++)。countはループの後の子の位置です。コードをチェックすると、同じことをする場所が見つけられます。 – albodelu

答えて

0

私はループのために、このソリューションの非常に誇りに思ってないんだけど、これは短いリストのための代替です:

あなたがビューとアダプタから、リスナーからviewHolderをクリックしたビューを取得することができます

Use this method

/** 
* Returns the adapter position of the Child associated with this ChildViewHolder 
* 
* @return The adapter position of the Child if it still exists in the adapter. 
* RecyclerView.NO_POSITION if item has been removed from the adapter, 
* RecyclerView.Adapter.notifyDataSetChanged() has been called after the last 
* layout pass or the ViewHolder has already been recycled. 
*/ 
@UiThread 
public int getChildAdapterPosition() { 
    int flatPosition = getAdapterPosition(); 
    if (mExpandableAdapter == null || flatPosition == RecyclerView.NO_POSITION) { 
     return RecyclerView.NO_POSITION; 
    } 

    return mExpandableAdapter.getChildPosition(flatPosition); 
} 

Also see

:ビューホルダーからの位置は、最終的にはこれらのメソッドを使用します

リスナーを簡単に定義する方法here

ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() { 
    @Override 
    public void onItemClicked(RecyclerView recyclerView, int position, View v) { 
     // do it 
    } 
}); 

使用this viewHolder取得する:view typeknow whenにクリックされたビューは、親またはあなたが得る "リスナーをクリックして" を使用して子

//Returns the view type of the item at position for the purposes of view recycling. 
    @Override 
    public int getItemViewType(int position) { 
     if (items.get(position) instanceof User) { 
      return USER; 
     } else if (items.get(position) instanceof String) { 
      return IMAGE; 
     } 
     return -1; 
    }