1

RecyclerViewTextViewが配列MyDataからロードされています。実際に私が必要とするのは、RecyclerViewのアイテムをクリックするとアイテムがカラーになる必要があり、別のアイテムをクリックしたときに最初のアイテムが正常になるようにすることです。Recycler Viewの選択項目の色を設定するには?

private static class MyOnClickListener implements View.OnClickListener { 

    private final Context context; 
    String PersonGenerated; 

    private MyOnClickListener(Context context) { 
     this.context = context; 
    } 

    @Override 
    public void onClick(View v) { 
     // removeItem(v); 
     int selectedItemPosition = recyclerView.getChildPosition(v); 
     RecyclerView.ViewHolder viewHolder 
       = recyclerView.findViewHolderForPosition(selectedItemPosition); 
     TextView textViewName 
       = (TextView) viewHolder.itemView.findViewById(R.id.textview_name); 
     String selectedName = (String) textViewName.getText(); 

    data = new ArrayList<DataModel>(); 
    for (int i = 0; i < MyData.nameArray.length; i++) { 
     data.add(new DataModel(
       MyData.id_[i], 
       MyData.nameArray[i] 

     )); 
    } 
    } 
} 

これは私のアダプタクラスです:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> { 

private ArrayList<DataModel> dataSet; 



public CustomAdapter(ArrayList<DataModel> data) { 
    this.dataSet = data; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, 
             int viewType) { 
    View view = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.adapter_persons, parent, false); 

    view.setOnClickListener(Persons.myOnClickListener); 

    MyViewHolder myViewHolder = new MyViewHolder(view); 
    return myViewHolder; 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) { 

    holder.textViewName.setText(dataSet.get(listPosition).getName()); 

} 


@Override 
public int getItemCount() { 
    return dataSet.size(); 


} 

public void animate(RecyclerView.ViewHolder viewHolder) { 
    final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(context, R.anim.anticipateovershoot_interpolator); 
    viewHolder.itemView.setAnimation(animAnticipateOvershoot); 
} 

public static class MyViewHolder extends RecyclerView.ViewHolder { 

    TextView textViewName; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     this.textViewName = (TextView) itemView.findViewById(R.id.textview_name); 
    } 
} 
} 

これは私の配列クラスである:

public class MyData { 

    static String[] nameArray = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"}; 
    static Integer[] id_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}; 
} 

アダプタレイアウト

<LinearLayout 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:background="@drawable/select_row" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true"> 


    <TextView 
     android:layout_weight="1" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     style="@style/custom_text_oval" 
     android:text="1" 
     android:textColor="#8F8F8F" 
     android:id="@+id/textview_name" /> 

Recyclerviewレイアウト

<LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/linearLayout3" 
      android:layout_gravity="center_horizontal"> 


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


     </LinearLayout> 

答えて

-2

android:background="@drawable/selector" 
+0

おかげで、私はこの方法を試してみましたが、無大成功で、私はTextViewのから私の指を削除すると、別のアクション –

+0

が私の編集したANSを確認して、もう一度私がチェック –

+0

を試してみたがまでのTextViewの滞在がクリックされたことをしたいです色は再び普通になります:/ –

1

ユーザーとしてRecyclerView行レイアウトの背景として設定されたこの描画可能にこれらのセレクタファイル::

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <shape> 
      <solid android:color="@color/blue" /> 
     </shape> 
    </item> 

    <item android:state_pressed="false"> 
     <shape> 
      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 

<item android:state_selected="true"> 
    <shape> 
     <solid android:color="@color/blue" /> 
    </shape> 
</item> 
    </selector> 

以下のようにセレクタ描画可能を作成
<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <shape> 
      <solid android:color="@color/blue" /> 
     </shape> 
    </item> 

    <item android:state_pressed="false"> 
     <shape> 
      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 

<item android:state_selected="true"> 
    <shape> 
     <solid android:color="@color/blue" /> 
    </shape> 
</item> 
    </selector> 
+0

これは文字通り上記と同じ答えでは機能しません –

0

これは古い質問ですが、誰かに助けてくれるので、私はこのように解決しました: 選択したアイテムを保存し、最後に選択したものをバックグラウンドで更新する必要があります。

private int selectedItem = 0; 
private int lastSelected = 0; 
public void onBindViewHolder(ViewHolder h, int position) { 
    //If is selected the color change 
    int backgroundColor = (position == selectedItem) ? R.color.selected : R.color.no_selected; 

    layout.setBackgroundColor(ContextCompat.getColor(context, backgroundColor)); 

    h.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //Save the position of the last selected item 
      lastSelected = selectedItem; 
      //Save the position of the current selected item 
      selectedItem = position; 

      //This update the last item selected 
      notifyItemChanged(lastSelected); 

      //This update the item selected 
      notifyItemChanged(selectedItem); 
     } 
    }); 
} 
関連する問題