我在选择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项物品?
答案 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。
这是ListView
与RecyclerView
之间的正点。