博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 用HorizontalScrollView实现ListView的Item滑动删除 ,滑动错乱 冲突
阅读量:5221 次
发布时间:2019-06-14

本文共 7543 字,大约阅读时间需要 25 分钟。

测试于:Android2.2+

对于Android来说按键操作已经在减少,越来越多的手势操作层出不穷,今天介绍一款LIstView的Item滑动删除的实现方式。

原理:

利用HorizontalScrollView可以横向滚动的特点实现滑动,根据动态的设置要展示的区域宽度,使得其余选项操作被挤出屏幕外面,从而实现单条滑动菜单的样式。

实现步骤:

  1. 实现Listview的Item布局文件item_list.layout
       
           
               
                   
               
               
               
               
  2. 准备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);    }}
  3. 继承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,这样效果看起来比较圆滑,不生硬                        // 如果水平方向的移动值

效果图:

 Android ListView滑动删除用HorizontalScrollView实现

还有很多的实现方式,希望大家多多指教哦,附件里面是源码本人测试通过的。

下载:

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;        }    }}

       

转载于:https://www.cnblogs.com/sharecenter/p/5620975.html

你可能感兴趣的文章
sql常见面试题
查看>>
jQuery总结第一天
查看>>
Java -- Swing 组件使用
查看>>
Software--Architecture--DesignPattern IoC, Factory Method, Source Locator
查看>>
poj1936---subsequence(判断子串)
查看>>
黑马程序员_Java基础枚举类型
查看>>
[ python ] 练习作业 - 2
查看>>
一位90后程序员的自述:如何从年薪3w到30w!
查看>>
在.net core上使用Entity FramWork(Db first)
查看>>
System.Net.WebException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集...
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
MongoDB的数据库、集合的基本操作
查看>>
ajax向后台传递数组
查看>>
疯狂JAVA16课之对象与内存控制
查看>>
[转载]树、森林和二叉树的转换
查看>>
WPF移动Window窗体(鼠标点击左键移动窗体自定义行为)
查看>>
软件测试-----Graph Coverage作业
查看>>
django ORM创建数据库方法
查看>>
创建Oracle synonym 详解
查看>>
php7 新特性整理
查看>>