如何在 Recycler View 中插入元素?
How can I insert element in Recycler View?
我对 RecyclerView 有疑问。
每次我尝试管理 Rec View 以插入或删除或更新某些元素时,我都必须与 Recycler View 和邪恶的 notifyDataSetChange 作斗争。
我尝试使用 runOnUiThread,但每次此解决方案都无用。
我试图在适配器或 Activity Rec View 实例中调用 notifyDataSetChange。
我真的需要知道如何让这项工作每次都能成为更好的开发人员。
真心希望能彻底解决这个问题
这是我的新尝试
public class MainActivity extends AppCompatActivity {
private Button nextTurn;
private Button submit;
private EditText name;
private EditText number;
private CounterElementAdapter mAdapter;
private RecyclerView recV;
private List<CounterElement> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextTurn = (Button) findViewById(R.id.button2);
submit = (Button) findViewById(R.id.button);
name = (EditText) findViewById(R.id.editText);
number = (EditText) findViewById(R.id.editText2);
recV = (RecyclerView) findViewById(R.id.recV);
list = new ArrayList<CounterElement>();
mAdapter = new CounterElementAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recV.setLayoutManager(mLayoutManager);
recV.setItemAnimator(new DefaultItemAnimator());
recV.setAdapter(mAdapter);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(name.getText().toString().length() > 0 && number.getText().toString().length() > 0) {
mAdapter.addElement(new CounterElement(name.getText().toString(), Integer.parseInt(number.getText().toString())));
}
}
});
nextTurn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.nextTurn();
}
});
}
}
public class CounterElementAdapter extends RecyclerView.Adapter<CounterElementAdapter.MyViewHolder> {
private List<CounterElement> elementList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, counter;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
counter = view.findViewById(R.id.counter);
}
}
public CounterElementAdapter(List<CounterElement> elementList) {
this.elementList = elementList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.element, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
CounterElement ce = elementList.get(position);
holder.name.setText(ce.getName());
holder.counter.setText(ce.getDuration());
}
@Override
public int getItemCount() {
return elementList.size();
}
public void addElement(CounterElement ce){
elementList.add(ce);
notifyDataSetChanged();
}
public void nextTurn(){
for(CounterElement ce : elementList){
if(ce.getStatus() >= 1)
ce.setStatus(ce.getStatus() - 1);
else {
elementList.remove(elementList.indexOf(ce));
notifyDataSetChanged();
}
}
}
}
提前致谢
看了一会儿你的代码,好像没发现什么问题,你是用android模拟器测试还是用物理设备?有时模拟器往往有问题。
首先尝试使用一些虚拟值初始化数组列表,然后尝试添加或删除并查看是否有帮助
您的代码看起来不错。我建议你看看这个 article
同时尝试调试您的代码并检查您的 elementList
更新是否正确。
我对 RecyclerView 有疑问。 每次我尝试管理 Rec View 以插入或删除或更新某些元素时,我都必须与 Recycler View 和邪恶的 notifyDataSetChange 作斗争。
我尝试使用 runOnUiThread,但每次此解决方案都无用。 我试图在适配器或 Activity Rec View 实例中调用 notifyDataSetChange。
我真的需要知道如何让这项工作每次都能成为更好的开发人员。 真心希望能彻底解决这个问题
这是我的新尝试
public class MainActivity extends AppCompatActivity {
private Button nextTurn;
private Button submit;
private EditText name;
private EditText number;
private CounterElementAdapter mAdapter;
private RecyclerView recV;
private List<CounterElement> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextTurn = (Button) findViewById(R.id.button2);
submit = (Button) findViewById(R.id.button);
name = (EditText) findViewById(R.id.editText);
number = (EditText) findViewById(R.id.editText2);
recV = (RecyclerView) findViewById(R.id.recV);
list = new ArrayList<CounterElement>();
mAdapter = new CounterElementAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recV.setLayoutManager(mLayoutManager);
recV.setItemAnimator(new DefaultItemAnimator());
recV.setAdapter(mAdapter);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(name.getText().toString().length() > 0 && number.getText().toString().length() > 0) {
mAdapter.addElement(new CounterElement(name.getText().toString(), Integer.parseInt(number.getText().toString())));
}
}
});
nextTurn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.nextTurn();
}
});
}
}
public class CounterElementAdapter extends RecyclerView.Adapter<CounterElementAdapter.MyViewHolder> {
private List<CounterElement> elementList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, counter;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
counter = view.findViewById(R.id.counter);
}
}
public CounterElementAdapter(List<CounterElement> elementList) {
this.elementList = elementList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.element, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
CounterElement ce = elementList.get(position);
holder.name.setText(ce.getName());
holder.counter.setText(ce.getDuration());
}
@Override
public int getItemCount() {
return elementList.size();
}
public void addElement(CounterElement ce){
elementList.add(ce);
notifyDataSetChanged();
}
public void nextTurn(){
for(CounterElement ce : elementList){
if(ce.getStatus() >= 1)
ce.setStatus(ce.getStatus() - 1);
else {
elementList.remove(elementList.indexOf(ce));
notifyDataSetChanged();
}
}
}
}
提前致谢
看了一会儿你的代码,好像没发现什么问题,你是用android模拟器测试还是用物理设备?有时模拟器往往有问题。 首先尝试使用一些虚拟值初始化数组列表,然后尝试添加或删除并查看是否有帮助
您的代码看起来不错。我建议你看看这个 article
同时尝试调试您的代码并检查您的 elementList
更新是否正确。