2016-06-13 18 views
0

私はFragmentsを使って分割ビュー画面を持っているマスター詳細アプリを持っています。マスター画面はRecyclerViewを含むリストです。レイアウトXMLは次のとおりです。RecyclerViewで選択したアイテムを強調表示できません

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_content" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:text="Page Title" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/page_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" />     

</LinearLayout> 

また、項目行レイアウトxmlは次のとおりです。アダプタクラスで

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:background="?android:attr/selectableItemBackground" 
    android:clickable="true" 
    android:id="@+id/item_layout"> 

<TextView 
    android:id="@+id/contact_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 

<Button 
    android:id="@+id/message_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:textSize="10sp" 
    /> 
</LinearLayout> 

が、私は、リスト上の項目をクリックすると、選択した項目

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> { 
    private int selectedPos; 

    @Override 
    public void onBindViewHolder(ListViewHolder holder, int position) { 
     holder.itemView.setSelected(selectedPos == position); 
     holder.bind(mData.get(position)); 
    } 

    class ListViewHolder extends RecyclerView.ViewHolder { 
     public ListViewHolder(View itemView) { 
      super(itemView); 
     } 
     public void bind(ListEntity entity) { 
      //UI rendering 
     } 
     @OnClick(R.id.layout_campaign) 
     public void onItemClicked(View view) { 
      notifyItemChanged(selectedPos); 
      selectedPos = getLayoutPosition(); 
      notifyItemChanged(selectedPos); 
     } 
} 

の位置を保持する変数を作成し、リスト項目が強調表示されません。この問題に関するかなりの数の類似記事を探しましたが、うまく機能しませんでした。私は何か不足していますか?

答えて

0

ユーザーが行を押したときにハイライトしたい場合は、行の親レイアウトの背景にセレクタを使用します。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:state_selected="true" 
      android:drawable="@color/red" /> <!-- pressed --> 
    <item android:state_pressed="true" 
      android:drawable="@color/red" /> <!-- pressed --> 
    <item android:drawable="@color/gray" /> <!-- default --> 
</selector> 

row_selector.xmlは描画可能で、このXMLを入れて、自分の親のレイアウトの背景として設定します。

希望する:

+0

返信ありがとうございました!リップルアニメーションを含むデフォルトの行セレクタを保持し、ハイライトロジックを使用するにはどうすればよいですか? – Renjith

+0

行オブジェクトにフラグを設定するだけで強調表示します。ユーザーが行をクリックした場合は、そのフラグをtrueとマークします。この変数を使用すると、行の背景を制御することができます。つまり、選択されていることを意味してtrueであれば、選択した色(onBind)で背景を変更します。 – Neo

関連する問題