当我滚动RecycerView时,它正在复制项,并且我已经调用adapter.notifydatasetchanged()
。
所以,可能我在错误的地方调用了数据集更新,但我找不到它是如何工作的。
下面是一些代码:
RecyclerView packageRecyclerView;
Intent intent;
ChecklistAdapter recyclerViewAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
intent = getIntent();
size = intent.getIntExtra("size", 0);
Log.d(TAG, "onCreate - Qtd Questões: " + size);
packageRecyclerView = findViewById(R.id.package_lst);
LinearLayoutManager recyclerLayoutManager = new LinearLayoutManager(this);
packageRecyclerView.setLayoutManager(recyclerLayoutManager);
DividerItemDecoration dividerItemDecoration =
new DividerItemDecoration(packageRecyclerView.getContext(),
recyclerLayoutManager.getOrientation());
packageRecyclerView.addItemDecoration(dividerItemDecoration);
List<Checklist> modelList = new ArrayList<>();
recyclerViewAdapter = new ChecklistAdapter(modelList,this);
modelList = getPackages();
recyclerViewAdapter = new ChecklistAdapter(modelList,this);
// recyclerViewAdapter = new ChecklistAdapter(getPackages(),this);
packageRecyclerView.setAdapter(recyclerViewAdapter);
}
private List<Checklist> getPackages() {
List<Checklist> modelList = new ArrayList<>();
Log.d(TAG, "getPackages: " + size);
for (int i=0; i<size;i++) {
List<String> priceList = new ArrayList<>();
priceList.add("Sim");
priceList.add("Não");
priceList.add("Não se Aplica");
modelList.add(new Checklist(intent.getStringExtra("q"+i), priceList));
}
recyclerViewAdapter.notifyDataSetChanged();
return modelList;
}
public class ChecklistAdapter extends
RecyclerView.Adapter<ChecklistAdapter.ViewHolder> {
private List<Checklist> packageList;
private Context context;
public ChecklistAdapter(List<Checklist> packageListIn
, Context ctx) {
packageList = packageListIn;
context = ctx;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(hasStableIds);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public ChecklistAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_checklistlines, parent, false);
ChecklistAdapter.ViewHolder viewHolder =
new ChecklistAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
super.onBindViewHolder(holder, position, payloads);
}
@Override
public void onBindViewHolder(ChecklistAdapter.ViewHolder holder,
int position) {
Checklist packageModel = packageList.get(position);
holder.packageName.setText(packageModel.getTitle());
int id = (position+1)*100;
for(String price : packageModel.getQuestions()){
RadioButton rb = new RadioButton(ChecklistAdapter.this.context);
rb.setId(id++);
rb.setText(price);
holder.priceGroup.addView(rb);
}
}
@Override
public int getItemCount() {
return packageList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView packageName;
public RadioGroup priceGroup;
public ViewHolder(View view) {
super(view);
packageName = view.findViewById(R.id.package_name);
priceGroup = view.findViewById(R.id.price_grp);
priceGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
Toast.makeText(ChecklistAdapter.this.context,
"Radio button clicked " + radioGroup.getCheckedRadioButtonId(),
Toast.LENGTH_SHORT).show();
}
});
}
}
}
import java.util.List;
public class Checklist {
String title;
List<String> questions;
public Checklist(String title, List<String> questions) {
this.title = title;
this.questions = questions;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getQuestions() {
return questions;
}
public void setQuestions(List<String> questions) {
this.questions = questions;
}
在oncreateviewholder()
中使用holder.setisrecolcable(false)
对我有效。
如何避免flex列中的项目拉伸到全宽?我希望它是创建时的原始大小。
我有一个,它具有和滚动。因为,这是一个带有的网格。为什么?因为它比中的简单要优化得多。 这一行需要与列表项滚动。 有人知道怎么做吗?我可以在项目上方添加一个新层吗?
这是我的项的代码。问题是,每个项目都显示在一整页的应用程序中。请帮我解决这个问题:
所以我有这样的问题 这是我的RecycerView适配器: 我怎么才能修好这个?谢了。
有人能帮我做那项任务吗?
公共类DbHelper扩展了SQLiteOpenHelper{ }