如何在RecyclerView中查看不可见项

时间:2019-04-11 07:18:19

标签: android android-recyclerview

我在选择RecyclerView的{​​{1}}时遇到问题。我搜索了几个小时,但仍然找不到答案。这是我的代码:

itemview

我的 for (int i = 0; i < mAdapter.getItemCount(); i++) { View view = recyclerView.getChildAt(i); if (view != null) { TextView tvIdEnt = view.findViewById(R.id.goodID_ENT_GOOD); TextView tvName = view.findViewById(R.id.goodNAME_ENT_GOOD); TextView tvQuan = view.findViewById(R.id.goodQUANTITY_GOOD); if (!tvQuan.getText().toString().equals("")) { hasGood = true; String idEnt = tvIdEnt.getText().toString(); String name = tvName.getText().toString(); int qty = Integer.parseInt(tvQuan.getText().toString()); //insert good } } } 有10个元素。使用mAdapter时,我获得了10个完美的子视图。但是当我使用ListView时,它只返回可见的项目。例如,方法RecyclerView将返回屏幕上的第一个可见项,而不是真正的第一项-getChildAt(0)的第一项。如何获得mAdapter的全部10项物品?

2 个答案:

答案 0 :(得分:0)

您可以在ViewHolder中设置视图。样本RecyclerView适配器;

/**
* Created by beyazid on 11.04.2019.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private Context context;
private LayoutInflater inflater;
private ArrayList<String> list;

public MyAdapter(Context context, ArrayList<String> list) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    this.list = list;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.my_row_for_recycler_view, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.bindItem(position);
}

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

}


public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView tvDummy;
    public MyViewHolder(View itemView) {
        super(itemView);
        tvDummy = itemView.findViewById(R.id.text);
    }
    void bindItem(int pos) {
        String txt = list.get(pos);
        tvDummy.setText(txt);
    }
}

}

答案 1 :(得分:0)

我认为这是不可能的,因为Recyclerview为每个索引使用相同的View。 这是ListViewRecyclerView之间的正点。