Android 滑动监听RecyclerView线性流+左右划删除+上下移动
时间:2022-03-24 11:35:41|栏目:Android代码|点击: 次
废话不多说了,直接给大家贴代码了。具体代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="c.example.jreduch10.Recycler1Activity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv" ></android.support.v7.widget.RecyclerView> </RelativeLayout> <span style="font-size:18px;">package c.example.jreduch10; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import c.example.jreduch10.entity.Video; import jp.wasabeef.recyclerview.adapters.AlphaInAnimationAdapter; import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter; public class Recycler1Activity extends AppCompatActivity { private RecyclerView rv; private List<Video> list; private Adapter adapter; private ItemTouchHelper itemTouchHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler1); rv=(RecyclerView)findViewById(R.id.rv); list=new ArrayList<>(); initData(); adapter=new Adapter(list); LinearLayoutManager llm=new LinearLayoutManager(this); llm.setOrientation(LinearLayoutManager.VERTICAL); rv.setLayoutManager(llm); // rv.setAdapter(adapter); //动画效果 AlphaInAnimationAdapter alphaInAnimationAdapter=new AlphaInAnimationAdapter(adapter); rv.setAdapter(new ScaleInAnimationAdapter(alphaInAnimationAdapter)); //默认动画效果 // rv.setItemAnimator(new DefaultItemAnimator()); itemTouchHelper=createItemTouchHelper(); itemTouchHelper.attachToRecyclerView(rv); ////监听事件某一行 // RecyclerViewItemListener listener=new RecyclerViewItemListener(this, new RecyclerViewItemListener.OnItemClickListener() { // @Override // public void OnItemClick(View item, int adapterPosition) { // Toast.makeText(getBaseContext(),adapterPosition+"",Toast.LENGTH_SHORT).show(); // } // }); // rv.addOnItemTouchListener(listener); } public ItemTouchHelper createItemTouchHelper(){ itemTouchHelper=new ItemTouchHelper(new ItemTouchHelper.Callback() { @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { //拖拽时支持的方向向上向下 int dragFlags=ItemTouchHelper.UP|itemTouchHelper.DOWN; //滑动的时候支持的方向为左右 int swipeFlags=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT; //必须调用makeMovementFlags()方法通知 TouchHelper支持的种类 return makeMovementFlags(dragFlags,swipeFlags); } //上下拖动回调次方法。 @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { adapter.move(viewHolder.getAdapterPosition(),target.getAdapterPosition()); return true; } //左右滑动回调此方法。 @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { adapter.delete(viewHolder.getAdapterPosition()); } @Override public boolean isItemViewSwipeEnabled() { return super.isItemViewSwipeEnabled(); } @Override public boolean isLongPressDragEnabled() { return super.isLongPressDragEnabled(); } }); return itemTouchHelper; } public void initData(){ Video video=new Video(R.mipmap.zyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyfzyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyfzyfzyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyf,"国产","........"); list.add(video); video=new Video(R.mipmap.g,"国产","........"); list.add(video); video=new Video(R.mipmap.ic_launcher,"国产","........"); list.add(video); video=new Video(R.mipmap.zyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyfzyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyfzyfzyf,"国产","........"); list.add(video); video=new Video(R.mipmap.zyf,"国产","........"); list.add(video); video=new Video(R.mipmap.g,"国产","........"); list.add(video); video=new Video(R.mipmap.ic_launcher,"国产","........"); list.add(video); video=new Video(R.mipmap.zyf,"国产","........"); list.add(video); } public class Adapter extends RecyclerView.Adapter<ViewHolder>{ private List<Video> mData; public Adapter(List<Video> mData){ this.mData=mData; } //左右滑动时调用此方法 public void delete(int position){ mData.remove(position); notifyItemRemoved(position); } //上下拖动时调用此方法 public void move(int from,int to){ Video video=mData.remove(from); mData.add(to>from?to-1:to,video); notifyItemMoved(from,to); } @Override public int getItemCount() { return mData.size(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=getLayoutInflater().inflate(R.layout.recycle1_linear_layout,parent,false); ViewHolder vh=new ViewHolder(view); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Video video=mData.get(position); holder.iv.setImageResource(video.getImg()); holder.title.setText(video.getTitle()); holder.content.setText(video.getContent()); } } public class ViewHolder extends RecyclerView.ViewHolder{ ImageView iv; TextView title; TextView content; public ViewHolder(View itemView) { super(itemView); iv= (ImageView) itemView.findViewById(R.id.iv); title=(TextView)itemView.findViewById(R.id.title); content=(TextView)itemView.findViewById(R.id.content); //监听事件某一项 itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int position=getAdapterPosition(); Log.d("9999999999","uuuuu"); Toast.makeText(getBaseContext(),title.getText()+":"+position,Toast.LENGTH_SHORT).show(); } }); } } } </span>
栏 目:Android代码
本文标题:Android 滑动监听RecyclerView线性流+左右划删除+上下移动
本文地址:http://www.codeinn.net/misctech/197118.html