对于Android来说按键操作已经在减少,越来越多的手势操作层出不穷,今天介绍一款LIstView的Item滑动删除的实现方式。
原理:
利用HorizontalScrollView可以横向滚动的特点实现滑动,根据动态的设置要展示的区域宽度,使得其余选项操作被挤出屏幕外面,从而实现单条滑动菜单的样式。
实现步骤:
- 实现Listview的Item布局文件item_list.layout
- 准备MainActivity初始化Listview
package com.yzy.horizontalscrollview;import android.app.Activity;import android.os.Bundle;import android.util.DisplayMetrics;import android.widget.ListView;import com.yzy.horizontalscrollview.R;public class MainActivity extends Activity { private ListView mListView; private MyAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listView); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mAdapter = new MyAdapter(this, dm.widthPixels); mListView.setAdapter(mAdapter); }}
- 继承BaseAdapter实现自定义的适配器MyAdapter
package com.yzy.horizontalscrollview;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.HorizontalScrollView;import android.widget.TextView;import com.yzy.horizontalscrollview.R;public class MyAdapter extends BaseAdapter implements View.OnClickListener { // 数据源,用于存放颜色值的。 private List
colors; private Context mContext; // 屏幕宽度,由于我们用的是HorizontalScrollView,所以按钮选项应该在屏幕外 private int mScreentWidth; private View view; /** * 构造方法 * * @param context * @param screenWidth */ public MyAdapter(Context context, int screenWidth) { // 初始化 mContext = context; mScreentWidth = screenWidth; // 填充list的内容模拟数据,否则应该异步执行 colors = new ArrayList (); for (int i = 0; i < 15; i++) { colors.add(R.color.blue); } } @Override public int getCount() { return colors.size(); } @Override public Object getItem(int position) { return colors.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; // 如果没有设置过,初始化convertView if (convertView == null) { // 获得设置的view convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent, false); // 初始化holder holder = new ViewHolder(); holder.hSView = (HorizontalScrollView) convertView.findViewById(R.id.hsv); holder.action = convertView.findViewById(R.id.ll_action); holder.btOne = (Button) convertView.findViewById(R.id.button1); holder.btTwo = (Button) convertView.findViewById(R.id.button2); holder.btThree = (Button) convertView.findViewById(R.id.button3); holder.tvContent = (TextView) convertView.findViewById(R.id.tv); // 设置内容view的大小为屏幕宽度,这样按钮就正好被挤出屏幕外 holder.content = convertView.findViewById(R.id.ll_content); LayoutParams lp = holder.content.getLayoutParams(); lp.width = mScreentWidth; convertView.setTag(holder); } else { // 有直接获得ViewHolder holder = (ViewHolder) convertView.getTag(); } // 把位置放到view中,这样点击事件就可以知道点击的是哪一条item holder.btOne.setTag(position); holder.btTwo.setTag(position); holder.btThree.setTag(position); // 设置监听事件 convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (view != null) { ViewHolder viewHolder1 = (ViewHolder) view.getTag(); viewHolder1.hSView.smoothScrollTo(0, 0); } case MotionEvent.ACTION_UP: // 获得ViewHolder ViewHolder viewHolder = (ViewHolder) v.getTag(); view = v; // 获得HorizontalScrollView滑动的水平方向值. int scrollX = viewHolder.hSView.getScrollX(); // 获得操作区域的长度 int actionW = viewHolder.action.getWidth(); // 注意使用smoothScrollTo,这样效果看起来比较圆滑,不生硬 // 如果水平方向的移动值
效果图:
还有很多的实现方式,希望大家多多指教哦,附件里面是源码本人测试通过的。
下载:
import android.content.Context;import android.util.AttributeSet;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.View;import android.widget.HorizontalScrollView;public class CustomHScrollView extends HorizontalScrollView{ private GestureDetector mGestureDetector; private View.OnTouchListener mGestureListener; private static final String TAG = "CustomHScrollView"; /** * @function CustomHScrollView constructor * @param context Interface to global information about an application environment. * */ public CustomHScrollView(Context context) { super(context);// TODO Auto-generated constructor stub mGestureDetector = new GestureDetector(new HScrollDetector()); setFadingEdgeLength(0); } /** * @function CustomHScrollView constructor * @param context Interface to global information about an application environment. * @param attrs A collection of attributes, as found associated with a tag in an XML document. */ public CustomHScrollView(Context context, AttributeSet attrs) { super(context, attrs);// TODO Auto-generated constructor stub mGestureDetector = new GestureDetector(new HScrollDetector()); setFadingEdgeLength(0); } /** * @function CustomHScrollView constructor * @param context Interface to global information about an application environment. * @param attrs A collection of attributes, as found associated with a tag in an XML document. * @param defStyle style of view */ public CustomHScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);// TODO Auto-generated constructor stub mGestureDetector = new GestureDetector(new HScrollDetector()); setFadingEdgeLength(0); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } // Return false if we're scrolling in the y direction class HScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if(Math.abs(distanceX) > Math.abs(distanceY)) { return true; } return false; } }}