在ViewHolder中,需要在每次视图被绑定时设置AutoCompleteTextView的适配器和数据源。
示例代码如下:
public class MyAdapter extends RecyclerView.Adapter
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_layout, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.autoCompleteTextView.setAdapter(new ArrayAdapter(holder.itemView.getContext(),
android.R.layout.simple_dropdown_item_1line, dataList));
}
@Override
public int getItemCount() {
return dataList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public AutoCompleteTextView autoCompleteTextView;
public MyViewHolder(View itemView) {
super(itemView);
autoCompleteTextView = itemView.findViewById(R.id.autoCompleteTextView);
}
}
}
在上面的示例中,onBindViewHolder方法在每次ViewHolder被绑定时设置适配器和数据源。注意AutoCompleteTextView需要在ViewHolder中声明为public,以便在外部使用。