大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“怎么搭建recyclerView滑动删除”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么搭建recyclerView滑动删除”吧!
公司主营业务:成都网站制作、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出达日免费做网站回馈大家。
package com.example.demo; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.graphics.Canvas; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * @author Huang xudong * @date 2020/7/26 */ public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private List list=new ArrayList(); class MyAdpter extends RecyclerView.Adapter{ class ViewHolder extends RecyclerView.ViewHolder{ private TextView textView; private LinearLayout linearLayout; public ViewHolder(@NonNull View itemView) { super(itemView); linearLayout=itemView.findViewById(R.id.ll_main); textView=itemView.findViewById(R.id.tv_main); } } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_demo,parent, false); return new ViewHolder(inflate); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { } @Override public int getItemCount() { return list.size(); } } class CallBack extends ItemTouchHelper.Callback{ @Override public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { return makeMovementFlags(0,ItemTouchHelper.LEFT); } @Override public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { /** * call max distance start onSwiped call */ } @Override public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { if (actionState==ItemTouchHelper.ACTION_STATE_SWIPE){ /** * get {@link TextView#getWidth()} */ ViewGroup viewGroup= (ViewGroup) viewHolder.itemView; TextView textView = (TextView) viewGroup.getChildAt(1); ViewGroup.LayoutParams layoutParams = textView.getLayoutParams(); if (Math.abs(dX)<=layoutParams.width){ /** * move {@link RecyclerView.ViewHolder} distance */ viewHolder.itemView.scrollTo((int) -dX,0); /** * callAction or register click bind view */ } } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView=findViewById(R.id.rv_main); list.add(1); list.add("2"); MyAdpter myAdpter=new MyAdpter(); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getApplicationContext()); linearLayoutManager.setOrientation(RecyclerView.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(myAdpter); ItemTouchHelper itemTouchHelper=new ItemTouchHelper(new CallBack()); itemTouchHelper.attachToRecyclerView(recyclerView); } }
感谢各位的阅读,以上就是“怎么搭建recyclerView滑动删除”的内容了,经过本文的学习后,相信大家对怎么搭建recyclerView滑动删除这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!