1

나는 배열 MyData에서로드 된 TextViewRecyclerView을 가지고 있습니다. 실제로 내가 필요한 것은 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

감사하지만 난이 방법을 시도하지만 succes에와, 나는 내가 텍스트 뷰에서 내 손가락을 제거 할 때 다른 동작 –

+0

이 내 편집 ANS를 확인하고 다시 내가 확인 –

+0

을 시도하지만 때까지 텍스트 뷰의 체류를 클릭 한 것을 원하는 색상이 다시 정상적으로됩니다./ –

1

사용자로

<?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> 

아래에 이러한 선택 파일을 당신의 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> 
+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); 
     } 
    }); 
} 
관련 문제