gpt4 book ai didi

android - 如何通过在 Android 中滑动或滑动来删除回收站 View 行?

转载 作者:行者123 更新时间:2023-11-29 15:43:39 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序。在我的应用程序中,我使用的是 Recycler View。这是我第一次使用 Recycler View。我将它与 CardView 一起使用。我使用回收器 View 的原因是我想通过向左或向右滑动来删除行。但我无法在 Recycler View 中找到执行此操作的事件。我该如何设置它?它是否内置于 Recycler View 中?

我是这样用grandle安装的

 compile 'com.android.support:cardview-v7:23.0.+'

compile 'com.android.support:recyclerview-v7:23.0.+'

这是我的适配器

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyViewHolder> {

private ArrayList<CardData> values;

public CardAdapter(ArrayList<CardData> valuesPram)
{
this.values = valuesPram;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name,email;

public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.card_name);
email = (TextView)view.findViewById(R.id.card_email);
}
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CardData cardData = values.get(position);
holder.email.setText(cardData.getEmail());
holder.name.setText(cardData.getName());
}

@Override
public int getItemCount() {
return values.size();
}
}

这是我的行布局

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/colorAccent"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/card_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/card_email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>

这是我的 Activity

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Integer> removedItems;

private ArrayList<CardData> listItems;
private CardAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeData();
setUpRecyclerView();
}

private void setUpRecyclerView()
{
recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);


}

private void initializeData()
{
listItems = new ArrayList<CardData>();
CardData item1 = new CardData();
item1.setName("Name 1");
item1.setEmail("Email 1");
listItems.add(item1);
CardData item2 = new CardData();
item2.setName("Name 2");
item2.setEmail("Email 2");
listItems.add(item2);
CardData item3 = new CardData();
item3.setName("Name 3");
item3.setEmail("Email 3");
listItems.add(item3);
CardData item4 = new CardData();
item4.setName("Name 4");
item4.setEmail("Email 4");
listItems.add(item4);
adapter = new CardAdapter(listItems);
}

private void removeItem(View v)
{
int selectedItemPosition = recyclerView.getChildPosition(v);
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForPosition(selectedItemPosition);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

我的代码运行良好。我的问题是如何通过向左或向右滑动到回收站 View 的其中一行来实现移除项目的事件?

最佳答案

一个简单的解决方案是使用RecyclerView自身提供的ItemTouchHelper

//Swipe to Delete
ItemTouchHelper swipeToDismissTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction)
{

// Do Stuff


}

});
swipeToDismissTouchHelper.attachToRecyclerView(recyclerView);

关于android - 如何通过在 Android 中滑动或滑动来删除回收站 View 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com