1.主页布局xml:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.deletedemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
2.主页代码:
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.list)
RecyclerView list;
private DeleteDemoAdapter adapter;
private ArrayList<String> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
datas=new ArrayList<>();
for(int i=0;i<10;i++){
datas.add("第 "+i+"行");
adapter=new DeleteDemoAdapter(MainActivity.this,R.layout.item_layout,datas);
list.setAdapter(adapter);
list.setLayoutManager(new LinearLayoutManager(this));//这里使RecylerView的使用,大家应该不陌生
3.适配器代码:
注解:适配器代码中用到BaseQuickAdapter,这个在之前的一篇文章有介绍过:
https://blog.csdn.net/zuo_er_lyf/article/details/79207150 点击打开链接
public class DeleteDemoAdapter extends BaseQuickAdapter<String,BaseViewHolder> {
private ArrayList<String> datas;
private HorizontalScrollView scrollView;
private Context context;
public DeleteDemoAdapter(Context context,@LayoutRes int layoutResId, @Nullable ArrayList<String> data) {
super(layoutResId, data);
this.datas=data;
this.context=context;
@Override
protected void convert(final BaseViewHolder helper, String item) {
//这段代码,重新设置宽度,必须要设置,否则会出现删除按钮在屏幕以内,
//原因是使用HorizontalScrollView,xml中match_parent属性为wrap_parent
LinearLayout layout=helper.getView(R.id.layout);
int width= ScreenUtil.getInstance(context).getScreenWidth();
ViewGroup.LayoutParams layoutParams=layout.getLayoutParams();
layoutParams.width=width;
layout.setLayoutParams(layoutParams);
helper.setText(R.id.tv_content,item);
TextView tv_delete=helper.getView(R.id.tv_delete);
tv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView=helper.getView(R.id.scrollView);
scrollView.scrollTo(0,0);//删除之后,横向滑动到最初位置
datas.remove(helper.getPosition());
notifyDataSetChanged();
4.屏幕工具类:
public class ScreenUtil {
public static int height;
public static int width;
private static ScreenUtil instance;
private Context context;
private ScreenUtil(Context context) {
this.context = context;
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
public static ScreenUtil getInstance(Context context) {
if (instance == null) {
instance = new ScreenUtil(context);
return instance;
* 得到手机屏幕的宽度, pix单位
* 获得通知栏的高度
* @return
public static int getStatusHeight(Context context){
int resid = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if(resid > 0){
return context.getResources().getDimensionPixelSize(resid);
return -1;
* 得到手机屏幕的宽度, pix单位
public int getScreenWidth() {
return width;
//获取屏幕的宽度
public static int getScreenWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getWidth();
//获取屏幕的高度
public static int getScreenHeight(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getHeight();
//px转dp
public static int dp2px(Context context,float dp) {
return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
5.效果图:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
适配...
实现原理其实很简单,主要是利用自定义HorizontalScrollView平移的方式实现,判断左滑的距离是否大于左滑显示的布局的宽度,如果是的话,则进行左滑操作,否则隐藏。下面开始直接上这个大佬写的代码,核心是这个自定义View。好吧,这个文章我只是转载的,为了方便自己以后参考转的。然后adapter中。
首先定义一个自定义组件:import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.Log;import android.util.TypedValue;import android.view.MotionEvent;im...
最近碰到一个控件冲突的问题,参考网址:https://blog.csdn.net/qq_37011271/article/details/80039837
解决思路:对HorizontalScrollView进行滑动监听,当向左活动并且滑动到尾部时,将滑动事件分发至子控件,否则将滑动事件拦截:
1、自定义HorizontalScrollView,实现对HorizontalScrollV...
项目中要实现的功能,之前找了很久发现网上大部分的侧滑删除和列表全选都是ListView的实现,而对RecyclerView的实现却是少之又少,所以花了很多时间实现了一个还比较满意的版本,
效果如下:
侧滑删除(带自动校位滑动效果):
右滑出现选择框:
一键编辑(全选):
关于RecyclerView的介绍跟优点网上已经有很多了,这里我就不在啰嗦。效果图1.配置gradle:compile 'com.jakewharton:butterknife:5.1.1'compile 'com.android.support:recyclerview-v7:23.1.0'2.Activity中的xml:activity_recycler_view_demo.xmlandroi...
public class SlidButtonView extends HorizontalScrollView {
private static final String TAG = "SlidButtonView";
private TextView lTextView_...