我已经在这篇文章的帮助下以某种方式实现了回收视图,现在我的要求是在此回收视图中从运行时删除一行。
link - http://treyrobinson.net/blog/android-l-tutorials-part-3-recyclerview-and-cardview/
请帮帮我。
感谢。
答案 0 :(得分:11)
由于我们不知道你的adaper如何,并且在该链接中给出了信息,你可以在适配器中创建一个删除对象的方法:
public void removeItem(int position) {
mData.remove(position);
notifyItemRemoved(position);
}
您可以通过以下活动完成所有这些:
((MyRecyclerAdapter)myRecyclerView.getAdapter()).removeItem(position);
答案 1 :(得分:4)
我能够让它像这样工作。
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
//Remove swiped item from list and notify the RecyclerView
datalist.remove(viewHolder.getAdapterPosition());
// this line animates what happens after delete
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
Snackbar.make(recyclerView, "delete successful", Snackbar.LENGTH_LONG).show();
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
};
并将上述内容设置为这样的回收站视图。
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
答案 2 :(得分:1)
我在过去的两天里尝试过同样的事情,最终让它发挥作用。
我的主要问题是如何获取点击视图的位置,以便我们可以将其从适配器中移除?
并且解决方案是在holder.itemView.setTag(model.get(position));
中使用onBindViewHolder
来存储视图的位置,并使用ViewModel model = (ViewModel) v.getTag();
中的onClick()
在运行时从RecyclerView
删除行的步骤:
创建MainActivity.java
public class MainActivity extends Activity {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private String[] liststring;
private TypedArray listImages;
MyRecyclerView adapter;
List<ViewModel> model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
liststring=getResources().getStringArray(R.array.listString);
listImages=getResources().obtainTypedArray(R.array.listImages);
recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter=new MyRecyclerView(createMockList(),this);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new RecyclerViewItemClickInterface() {
@Override
public void onItemclick(View v,ViewModel vModel) {
// TODO Auto-generated method stub
Log.i("inflatedlayout", v.getId()+"");
adapter.remove(vModel);
}
});
}
private List<ViewModel> createMockList() {
List<ViewModel> items = new ArrayList<ViewModel>();
for (int i = 0; i < 9; i++) {
items.add(new ViewModel("Item " + (i + 1),listImages.getResourceId(i, -1)));
}
return items;
}
}
创建MyRecyclerView.java
public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerView.MyViewHolder> implements View.OnClickListener {
String data[];
TypedArray images;
MyRecyclerView mv;
Activity act_context;
RecyclerViewItemClickInterface listener;
List<ViewModel> model;
public MyRecyclerView(List<ViewModel> model,Activity act_context) {
// TODO Auto-generated constructor stub
this.model=model;
this.act_context=act_context;
}
public static class MyViewHolder extends ViewHolder{
private ImageView imageView1;
private TextView textView1;
//private IMyViewHolderClicks listener;
public MyViewHolder(View inflatedView) {
super(inflatedView);
// TODO Auto-generated constructor stub
textView1=(TextView) inflatedView.findViewById(R.id.textView1);
imageView1=(ImageView) inflatedView.findViewById(R.id.imageView1);
}
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
Log.i("getIemViewTypePosition", ""+position);
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return model.size();
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// TODO Auto-generated method stub
holder.itemView.setTag(model.get(position));
holder.imageView1.setImageResource(model.get(position).getImage());
holder.textView1.setText(model.get(position).gettest());
Log.i("onBindViewHolder", "called by layoutmanager");
}
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
// TODO Auto-generated method stub
View v=LayoutInflater.from(act_context).inflate(R.layout.row_layout, parent, false);
v.setOnClickListener(this);
MyRecyclerView.MyViewHolder vh = new MyViewHolder(v);
Log.i("onCreateViewHolder", "ViewHolder created");
return vh;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (listener != null) {
ViewModel model = (ViewModel) v.getTag();
listener.onItemclick(v,model);}
}
void setOnItemClickListener(RecyclerViewItemClickInterface listener){
this.listener=listener;
}
public void remove(ViewModel item) {
int position = model.indexOf(item);
model.remove(position);
notifyItemRemoved(position);
}
}
创建ViewModel.java
public class ViewModel {
String test;
int image;
public ViewModel(String test,int image) {
// TODO Auto-generated constructor stub
this.test=test;
this.image=image;
}
String gettest(){
return test;
}
int getImage(){
return image;
}}
4.创建interface
:
interface RecyclerViewItemClickInterface {
void onItemclick(View v,ViewModel viewModel);
}
答案 3 :(得分:1)
如果你想通过点击删除一行,我认为更简洁的方法是在ViewHolder上实现onClicks。然后在适配器上实现一个公共remove方法,然后通过onCreateViewHolder方法将适配器传递给ViewHolder。
会是这样的:
--- ---实施例
适配器:
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private List<Object> mDataset; // This is the list of items to be shown
//Here you would write the rest of your Adapter code
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = mInflater.inflate(R.layout.item_recyclerview_round, parent, false);
return new RoundViewHolder(itemView, this);
}
public void removeItem(int position) {
mDataSet.remove(position);
notifyItemRemoved(position);
// Add whatever you want to do when removing an Item
}
}
ViewHolder:
public class MyViewHolder RecyclerView.ViewHolder
implements View.OnClickListener {
private MyAdapter myAdapter;
// Here you would write the rest of your ViewHolder code
public MyViewHolder (View itemView, MyAdapter myAdapter){
super(itemView);
this.myAdapter = myAdapter;
// Here you would write the rest of your constructor
}
public void onClick(View view) {
myAdapter.removeItem(getAdapterPosition());
}
它对我有用。我不知道性能是否可以提高(可能),但这是我找到的最简单的方法。
我希望它有所帮助。
答案 4 :(得分:0)
我知道这是迟到的答案,但也许对其他人有帮助。 You don't need to add new delete method in your adapter class
。最好的方法是在add interface
课程中ViewHolder
开始{/ 1}}。
public class ApapterVH extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView ivDeleteItem;
ApapterVH(View itemView) {
super(itemView);
ivDeleteItem = itemView.findViewById(R.id.ivDelete);
itemView.setOnClickListener(this);
ivDeleteFoodItem.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == ivDeleteItem.getId()){
onItemClickListener.onDelete(v, getAdapterPosition());
}
if(onItemClickListener!=null){
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
public interface OnItemClickListener {
void onItemClick(View view , int position);
void onDelete(View view, int position);
}
public void SetOnItemClickListener(final OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
然后在你的Activity或Fragment中添加:
mAdapter.SetOnItemClickListener(new YourAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//Do something
}
@Override
public void onDelete(View view, int position) {
mList.remove(position);
mAdapter.notifyItemRemoved(position);
}
});
只有这两行代码才会删除您的项目:
mList.remove(position);
mAdapter.notifyItemRemoved(position);
答案 5 :(得分:0)
如果上述解决方案无效,请尝试
private fun removeItem(position: Int) {
items.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, itemCount - position)
}