欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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代码

下一篇:详解android是如何管理内存的

本文标题:Android 滑动监听RecyclerView线性流+左右划删除+上下移动

本文地址:http://www.codeinn.net/misctech/197118.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有