2016-05-27 6 views
0

私はthisライブラリを使用してExpandableRecyclerViewを実装しています。ExpandableRecyclerViewグループビュー内のクリックイベントを処理する

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/expandable_list_group_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Group Heading" /> 

    <ImageButton 
     android:id="@+id/expandable_list_delete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_delete_black_24dp" 
     android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

ライブラリによって処理されていると私はアダプタでのメソッドを介して、リストに要素を追加している崩壊/拡張するためのクリックイベント:グループビューは、次のようになります。ライブラリがコード化された方法、具体的な存在は、アイテムの追加と削除する方法を通知:

が意図したとおりに動作しない伝統的なnotifyDataSetChanged()RecyclerView.Adapterのことに注意してください。代わりに拡張可能なRecyclerViewは、ParentListItemのリストに対する変更をアダプタに通知する機能を備えた通知メソッドのセットを提供します。

notifyParentItemInserted(int型parentPosition)、notifyParentItemRemoved(int型parentPosition)、notifyParentItemChanged(int型parentPosition)、notifyParentItemRangeInserted(int型parentPositionStart、int型ITEMCOUNT個)

私は与えられたグループの削除を処理するためのImageButtonを使用したいですリストから。これを行うには、アダプタメソッドnotifyParentItemRemovedにアクセスする必要があります。

public class MyParentViewHolder extends ParentViewHolder { 

    TextView groupTitle; 
    ImageButton deleteButton; 

    public MyParentViewHolder(View itemView) { 
     super(itemView); 
     groupTitle = (TextView) itemView.findViewById(R.id.expandable_list_group_title); 
     deleteButton = (ImageButton) itemView.findViewById(R.id.expandable_list_delete); 
     deleteButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        ... // somehow delete 
      } 
     }); 
    } 

public void bind(...) { 
    ... 
} 

もしそうなら、どのように私

  1. アクセス私が削除したいリスト項目になりますので、同様に、私は、グループのViewHolder内のクリックを処理すべきか? ArrayListはAdapterクラスにあります。
  2. リストを更新しますか?アダプタの通知メソッドを呼び出す必要があります。

ViewHolder内でアダプタを参照する必要がありますか?このように扱うのは正しいとは感じません。そうでない場合は、展開可能なグループビューからクリックイベントをどのように処理できますか?

答えて

0

応答がなかったので、私は自分で問題を解決しようとする必要がありました。私はこれが(これはおそらくそうではない)これをやっていく最善の方法かどうかは分かりませんが、他に選択肢がないようです。

アダプタから、アダプタオブジェクトへの参照をViewHolderコンストラクタに渡します。クリックはViewHolder自体で処理されます。アダプタメソッドが呼び出され、さらにnotifyメソッドが呼び出されます。これは正常に動作します。

関連する問題