得到RecyclerView的孩子

时间:2015-10-29 15:30:37

标签: android android-recyclerview

如何访问RecyclerView的孩子?我没有找onClickListener。我想知道如何更改TextViews中某些RecyclerView的背景。我真的不知道。任何建议将不胜感激。

请注意:直接子项是垂直的LinearLayout,其中包含TextView s。

public class MessdatenAdapter extends RecyclerView.Adapter<MessdatenAdapter.ViewHolder> {
private ArrayList<ArrayList<MessdatenColumn>> list = new ArrayList<ArrayList<MessdatenColumn>>();


public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public LinearLayout llVerticalContainer;
    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        llVerticalContainer = (LinearLayout) itemLayoutView.findViewById(R.id.layout_messdaten);
    }
}

public MessdatenAdapter(ArrayList<ArrayList<MessdatenColumn>> list) {
    this.list = list;
}

// Create new views (invoked by the layout manager)
@Override
public MessdatenAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                  int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_messdaten, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    ArrayList<MessdatenColumn> terminStringList = list.get(position);
    LayoutInflater inflater = LayoutInflater.from(MainActivity.getMyAppCompatActivity());
    holder.llVerticalContainer.removeAllViews();

    for(int i = 0; i < terminStringList.size();i++){
        TextView tv = (TextView) inflater.inflate(R.layout.item_messdaten_textview, null);

        if(terminStringList.get(i).getValue().equals("NaN")){
            tv.setText("-");
        } else {
            tv.setText(terminStringList.get(i).getValue());
        }

        if (i == 0) {
            tv.setTypeface(null, Typeface.BOLD);
        }
        holder.llVerticalContainer.addView(tv);
    }

}

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

}

1 个答案:

答案 0 :(得分:1)

一旦从适配器知道孩子的位置,就可以使用LinearLayoutManager.getChildAt(index)来获取子视图。

您可以在活动中使用此功能。但请注意,您始终查询存在的位置,否则您将面临Incosistency Detected错误。

完成更改后,请致电RecyclerView.Adapter.notifyItemChanged (int position)