1

我在我的项目中使用可扩展recyclerview,这些数据来自Firebase咨询。从firebase可扩展recyclerview更新

,我使用的扩张recyclerView是:bignerdranch

什么是更新我认为最好的方式,因为该网站说:

请注意,RecyclerView.Adapter的传统notifyDataSetChanged()不工作打算

,并推荐使用:

//子变化

notifyChildInserted(int parentPosition, int childPosition)

...

不过,我不知道childPositions随着改变。

+0

使用 “点击收听” 你的视图以及使用库方法的视图和适配器位置。如果它不是一个很长的列表,可以从第一个位置迭代到单击的位置(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

/** 
* Given the index relative to the entire RecyclerView for a child item, 
* returns the child position within the child list of the parent. 
*/ 
@UiThread 
int getChildPosition(int flatPosition) { 
    if (flatPosition == 0) { 
     return 0; 
    } 

    int childCount = 0; 
    for (int i = 0; i < flatPosition; i++) { 
     ExpandableWrapper<P, C> listItem = mFlatItemList.get(i); 
     if (listItem.isParent()) { 
      childCount = 0; 
     } else { 
      childCount++; 
     } 
    } 
    return childCount; 
} 

一种简单的方法来定义听众here

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

使用this得到viewHolder:

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

检查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; 
    }