核心思想知识点:
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(滑动按钮自定义控件布局)
4、Adapter.java(RecyclerView适配器)
/**
* 适配器 Created by Administrator on 2017/2/3.
*/
public class Adapter extends RecyclerView.Adapter {
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 listIcon = new ArrayList();
//名称集合
private List listName = new ArrayList();
//信息集合
private List listInfo = new ArrayList();
//MyViewHolder集合
private List sbViews = new ArrayList();
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(主界面布局)
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)