核心思想知识点:
1)、自定义HorizontalScrollView控件
2)、getScrollX()方法的理解
效果图如下
功能实现过程
1、Utils.java(测量屏幕宽度的工具类)
public class Utils {
/**
* 获得屏幕宽度
*/
public static int getScreenWidth(Context context) {
//获取窗口管理服务
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
//屏幕参数对象
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
}
2、SlidingButtonView.java(滑动按钮自定义控件)
/**
* 滑动按钮自定义控件 Created by Administrator on 2017/2/3.
*/
public class SlidingButtonView extends HorizontalScrollView {
private TextView lTextView_Delete; //删除按钮
private int lScrollWidth; //横向滑动的范围
private Boolean first = false; //标记第一次进入获取删除按钮控件
//记录是否显示删除按钮
public Boolean isShowDelButton = false;
public SlidingButtonView(Context context) {
this(context, null);
}
public SlidingButtonView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOverScrollMode(OVER_SCROLL_NEVER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//第一次进入获取删除按钮控件
if(!first){
lTextView_Delete = (TextView) findViewById(R.id.tv_delete);
first = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//默认隐藏删除按钮
if(changed){
this.scrollTo(0,0);
//获取水平滚动条可以滑动的范围,即右侧按钮的宽度
lScrollWidth = lTextView_Delete.getWidth();
}
}
/**
*滑动手指抬起时的手势判断
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
changeScrollx(); //根据滑动距离判断是否显示删除按钮
return true;
default:
break;
}
return super.onTouchEvent(event);
}
/**
* 根据滑动距离判断是否显示删除按钮
*/
public void changeScrollx(){
//触摸滑动的距离大于删除按钮宽度的一半
if(getScrollX() >= (lScrollWidth/2)){
//显示删除按钮
this.smoothScrollTo(lScrollWidth, 0);
isShowDelButton = true;
}else{
//隐藏删除按钮
this.smoothScrollTo(0, 0);
isShowDelButton = false;
}
}
/**
* 恢复原状
*/
public void reset(){
//隐藏删除按钮
this.smoothScrollTo(0, 0);
}
}
3、layout_item.xml(滑动按钮自定义控件布局)
<?xml version="1.0" encoding="utf-8"?>
<com.example.slidedeletemenu.view.SlidingButtonView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sb_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="1dp"
android:background="@android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--删除按钮-->
<TextView
android:id="@+id/tv_delete"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/layout_content"
android:background="@drawable/btn_click_red_bg"
android:gravity="center"
android:text="删 除"
android:textColor="#DDFFFFFF" />
<RelativeLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<!--图标-->
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/img"
android:orientation="vertical"
android:gravity="center_vertical">
<!--名称-->
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:textSize="15dp"
/>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"
android:textSize="10dp"
android:text="123"
android:singleLine="true"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</com.example.slidedeletemenu.view.SlidingButtonView>
4、Adapter.java(RecyclerView适配器)
/**
* 适配器 Created by Administrator on 2017/2/3.
*/
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private static final String TAG = Adapter.class.getSimpleName();
//图标数组
private int[] icons = {
R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3,
R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6,
R.drawable.icon_7, R.drawable.icon_8, R.drawable.icon_9,
R.drawable.icon_10, R.drawable.icon_11
};
//名字数组
private int[] names = {
R.string.name1, R.string.name2, R.string.name3,
R.string.name4, R.string.name5, R.string.name6,
R.string.name7, R.string.name8, R.string.name9,
R.string.name10, R.string.name11
};
//信息数组
private int[] infos = {
R.string.info1, R.string.info2, R.string.info3,
R.string.info4, R.string.info5, R.string.info6,
R.string.info7, R.string.info8, R.string.info9,
R.string.info10, R.string.info11
};
private Context lContext; //上下文
//图标集合
private List<Integer> listIcon = new ArrayList<Integer>();
//名称集合
private List<Integer> listName = new ArrayList<Integer>();
//信息集合
private List<Integer> listInfo = new ArrayList<Integer>();
//MyViewHolder集合
private List<MyViewHolder> sbViews = new ArrayList<MyViewHolder>();
public Adapter(Context context) {
lContext = context;
//设置菜单行数与行内图标、名称、信息
for (int i = 0; i < 11; i++) {
listIcon.add(icons[i]);
listName.add(names[i]);
listInfo.add(infos[i]);
}
}
/**
* 返回数据集中的项目总数
*/
@Override
public int getItemCount() {
return listIcon.size();
}
/**
* 设置列表菜单中子项所显示的内容
*/
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
sbViews.add(holder);
Log.d(TAG,"onBindViewHolder====================");
//设置图标
holder.img.setBackgroundResource(listIcon.get(position));
//设置名称
holder.name.setText(listName.get(position));
//设置信息
holder.info.setText(listInfo.get(position));
//设置内容布局的宽为屏幕宽度
holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(lContext);
//删除按钮的事件,单击后删除整行
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n = holder.getLayoutPosition(); //获取要删除行的位置
removeData(n); //删除列表中的子项
}
});
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
Log.d(TAG,"onCreateViewHolder====================");
//获取列表中,每行的布局文件
View view = LayoutInflater.from(lContext).inflate(R.layout.layout_item, arg0, false);
MyViewHolder holder = new MyViewHolder(view); //
return holder;
}
class MyViewHolder extends RecyclerView.ViewHolder {
public SlidingButtonView sbView; //SlidingButtonView 自定义控件
public TextView btn_Delete; //删除按钮
public TextView name, info; //编号文字
public ImageView img; //图标
public ViewGroup layout_content; //图标与编号的布局
//获取相关控件
public MyViewHolder(View itemView) {
super(itemView);
sbView = (SlidingButtonView) itemView.findViewById(R.id.sb_view);
btn_Delete = (TextView) itemView.findViewById(R.id.tv_delete);
name = (TextView) itemView.findViewById(R.id.name);
info = (TextView) itemView.findViewById(R.id.info);
img = (ImageView) itemView.findViewById(R.id.img);
layout_content = (ViewGroup) itemView.findViewById(R.id.layout_content);
Log.d(TAG,"MyViewHolder====================");
}
}
/**
* 删除列表中子项
*/
public void removeData(int position) {
listIcon.remove(position); //删除子项中显示图标
listName.remove(position); //删除名字
listInfo.remove(position); //删除信息
notifyItemRemoved(position); //删除列表
}
}
5、activity_main.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"
>
<!--列表控件-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:background="#EEEEEE"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:overScrollMode="never"
/>
</RelativeLayout>
6、MainActivity.java(主界面逻辑)
public class MainActivity extends AppCompatActivity {
private RecyclerView lRecyclerView; //列表控件
private Adapter lAdapter; //适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); //初始化控件
setAdapter(); //设置适配器
}
/**
* 初始化控件方法
*/
private void initView(){
//获取列表控件
lRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
}
/**
* 设置适配器方法
*/
private void setAdapter(){
//设置列表布局管理
lRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置适配器
lRecyclerView.setAdapter(lAdapter = new Adapter(this));
//设置列表中子项的动画
lRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
}
```![GIF.gif](https://upload-images.jianshu.io/upload_images/2108792-28965fa5f0481a47.gif?imageMogr2/auto-orient/strip)