0

我使用recyclerView.I试图改变背景颜色项单击与selector.xml文件RecyclerView改变所选项目的背景和文本颜色

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/trains_vagon_selected_departure" android:state_pressed="true" /> 
<item android:drawable="@drawable/trains_vagon_selected_departure" android:state_focused="true" /> 

<item android:drawable="@drawable/trains_vagon_selected_white"/> 

这是我的适配器代码

public class TrainsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
public ArrayList<Train> trains; 
public Context context; 
private LayoutInflater inflater; 
private String statusString = ""; 

public class GenericViewHolder extends RecyclerView.ViewHolder { 
    public TextView trainTime; 
    public LinearLayout vagoncontainer; 
    private ImageView statusImageView; 

    public GenericViewHolder(View view) { 
     super(view); 

     trainTime = (TextView) view.findViewById(R.id.train_time); 
     statusImageView = (ImageView) view.findViewById(R.id.departure_next_img); 

     vagoncontainer = (LinearLayout) view.findViewById(R.id.vagon_container); 


    } 
} 

public TrainsAdapter(Context context, ArrayList<Train> trains, String statusString) { 
    this.context = context; 
    this.trains = trains; 
    inflater = LayoutInflater.from(context); 
    this.statusString = statusString; 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflater.inflate(R.layout.trains_adapter_item, parent, false); 
    GenericViewHolder holder = new GenericViewHolder(view); 
    return holder; 


} 


@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { 
    if (holder instanceof GenericViewHolder) { 
     final GenericViewHolder genericViewHolder = (GenericViewHolder) holder; 
     genericViewHolder.vagoncontainer.removeAllViews(); 
     Train train = trains.get(position); 
     if (statusString.equals("departure")) 
      genericViewHolder.statusImageView.setImageResource(R.mipmap.departure_icon); 
     else 
      genericViewHolder.statusImageView.setImageResource(R.mipmap.return_icon); 
     ArrayList<Vagons> vagons = train.getVagons(); 
     for (int i = 0; i < vagons.size(); i++) { 
      if (vagons.get(i).getEnable().equals("1")) { 
       LayoutInflater layoutInflater = LayoutInflater.from(context); 
       final LinearLayout vagonView = (LinearLayout) layoutInflater.inflate(R.layout.trains_vagon_child, null); 
       genericViewHolder.vagoncontainer.setBackgroundColor(Color.WHITE); 
       RelativeLayout relativeLayout = (RelativeLayout) vagonView.findViewById(R.id.selected_layout); 

       if (statusString.equals("departure")) 
        relativeLayout.setBackgroundResource(R.drawable.recyclerview_selected_departure); 
       else 
        relativeLayout.setBackgroundResource(R.drawable.recyclerview_selected_returned); 

       TextView vagonClassName = (TextView) vagonView.findViewById(R.id.vagon_class_name); 
       TextView vagonClassPrice = (TextView) vagonView.findViewById(R.id.vagon_class_price); 
       vagonClassName.setText(vagons.get(i).getName()); 
       vagonClassPrice.setText(vagons.get(i).getAmount()); 

       genericViewHolder.vagoncontainer.addView(vagonView); 
      } 
     } 

     genericViewHolder.trainTime.setText(train.getDeparture()); 
    } 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 



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

public Train getItem(int position) { 
    return trains.get(position); 
} 



@Override 
public int getItemViewType(int position) { 
    return position; 
} 

} 我有两个问题。 1)当我运行recyclerView中的第一个元素时也会自动选择。为什么?我可以如何修复它? 2)是否可以更改选择器XML文件中的文本颜色? 我知道如何通过位置更改简单recyclerView的背景和文本颜色,但此刻每个位置的元素都有一些数组,我将它添加到了我的位置,并尝试更改孩子的视图背景和文本颜色(请参阅我的代码。意味着vagonView视图) 谢谢大家

回答

0

它可能是由于来运行应用程序时,因为在state_focus="true"选择。

+0

你是对的,但是当我写默认为false,然后集中在所有的时间不工作@Hasmukh kachhatiya – BekaKK

+0

那么你一定要试试'state_pressed =“真”' –

+0

我试过了,第一次自动选择第一个元素@Hasmukh kachhatiya – BekaKK

0
  1. 你怎么知道的第一个元素被选中?
  2. 只需设置android:textColor一些颜色状态列表
+0

1)当我运行第一个元素已被选中2)你是什么意思?你能解释更多吗? @Cesario – BekaKK

+0

for 2.假设你有一个TextView,将textColor设置为某种颜色状态列表(选择器xml),但是你需要为文本颜色创建另一个'xml' – Cesario

+0

我试过了但没有工作@Cesario – BekaKK