热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android模仿QQ侧滑删除ListView功能示例

这篇文章主要介绍了Android模仿QQ侧滑删除ListView功能示例,非常不错,具有参考借鉴价值,需要的朋友可以参考下

需求:

1、listView可以侧滑item,展示删除按钮,点击删除按钮,删除当前的item

2、在删除按钮展示时,点击隐藏删除按钮,不响应item的点击事件

3、在删除按钮隐藏时,点击item响应点击事件

根据以上需求在网络上查找响应的例子,也有仿QQ侧滑代码,但不能满足2和3的要求,因此修改了一把,代码如下,共大家拍砖

第一步:重写ListView

public class SwipeListView extends ListView {
  private final static String TAG = "SwipeListView";
  private int mScreenWidth;  // 屏幕宽度
  private int mDownX;      // 按下点的x值
  private int mDownY;      // 按下点的y值
  private int mDeleteBtnWidth;// 删除按钮的宽度
  private boolean isDeleteShown = false;  // 删除按钮是否正在显示
  private boolean isOnClick= false;
  private ViewGroup mPointChild;  // 当前处理的item
  private LinearLayout.LayoutParams mLayoutParams;  // 当前处理的item的LayoutParams
  public SwipeListView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public SwipeListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // 获取屏幕宽度
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    mScreenWidth = dm.widthPixels;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
      case MotionEvent.ACTION_DOWN:
        performActionDown(ev);
        break;
      case MotionEvent.ACTION_MOVE:
        return performActionMove(ev);
      case MotionEvent.ACTION_UP:
        return performActionUp(ev);
//        break;
    }
    return super.onTouchEvent(ev);
  }
  // 处理action_down事件
  private void performActionDown(MotionEvent ev) {
//    Log.e(TAG,"performActiOnDown===="+isDeleteShown);
    if (isDeleteShown) {
      turnToNormal();
    }
    isOnClick= true;
    mDownX = (int) ev.getX();
    mDownY = (int) ev.getY();
    // 获取当前点的item
    int downPosition = pointToPosition(mDownX, mDownY);
    int firstPosition= getFirstVisiblePosition();
    Log.e(TAG,"performActiOnDown====downPosition:"+downPosition+"==firstPosition"+firstPosition);
    if(downPosition <0) return;
    mPointChild = (ViewGroup) getChildAt(downPosition-firstPosition);
    // 获取删除按钮的宽度
    mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;
    mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)
        .getLayoutParams();
    // 为什么要重新设置layout_width 等于屏幕宽度
    // 因为match_parent时,不管你怎么滑,都不会显示删除按钮
    // why? 因为match_parent时,ViewGroup就不去布局剩下的view
    mLayoutParams.width = mScreenWidth;
    mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
  }
  // 处理action_move事件
  private boolean performActionMove(MotionEvent ev) {
//    Log.e(TAG, "performActiOnMove====" + isDeleteShown);
    int nowX = (int) ev.getX();
    int nowY = (int) ev.getY();
    isOnClick= false;
    if (Math.abs(nowX - mDownX) > Math.abs(nowY - mDownY)) {
      // 如果向左滑动
      if (nowX = mDeleteBtnWidth) {
          scroll = -mDeleteBtnWidth;
        }
        // 重新设置leftMargin
        mLayoutParams.leftMargin = scroll;
        mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
      }
      return true;
    }
    return super.onTouchEvent(ev);
  }
  // 处理action_up事件
  private boolean performActionUp(MotionEvent ev) {
    boolean falg = false;
    if(isOnClick && !isDeleteShown)
    {
      falg = true;
    }
    // 偏移量大于button的一半,则显示button
    // 否则恢复默认
    if (-mLayoutParams.leftMargin >= mDeleteBtnWidth / 2) {
      mLayoutParams.leftMargin = -mDeleteBtnWidth;
      isDeleteShown = true;
    } else {
      turnToNormal();
      isDeleteShown = false;
    }
    mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
//    Log.e(TAG, "performActiOnUp====" + isDeleteShown);
    if(falg)
    {
      return super.onTouchEvent(ev);
    }
    return true;
  }
  /**
   * 变为正常状态
   */
  public void turnToNormal() {
    mLayoutParams.leftMargin = 0;
    mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
  }
  /**
   * 当前是否可点击
   *
   * @return 是否可点击
   */
  public boolean canClick() {
    return !isDeleteShown;
  }
}

第二步:适配器

class SwipeListAdapter extends BaseAdapter {
  @Override
  public int getCount() {
    return mData.size();
  }
  @Override
  public Object getItem(int position) {
    return mData.get(position);
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (null == convertView) {
      holder = new ViewHolder();
      cOnvertView= View.inflate(TestListViewActivity.this, R.layout.item_swipe_list, null);
      holder.tv = (LinearLayout) convertView.findViewById(R.id.tv);
      holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
      holder.delete = (TextView) convertView.findViewById(R.id.delete);
      convertView.setTag(holder);
    }
    else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.tvName.setText(mData.get(position));
    final int pos = position;
    holder.delete.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mData.remove(pos);
        notifyDataSetChanged();
        mListView.turnToNormal();
      }
    });
    return convertView;
  }
}
static class ViewHolder {
  LinearLayout tv;
  TextView tvName;
  TextView delete;
}

第三步:写一个TestListViewActivity

private SwipeListView mListView;
  private ArrayList mData = new ArrayList() {
    {
      for (int i = 0; i <20; i++) {
        add("hello world, hello android " + i);
      }
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_list_view);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();
      }
    });
    mListView = (SwipeListView) findViewById(R.id.list);
    mListView.setAdapter(new SwipeListAdapter());
//    mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
//      @Override
//      public boolean onItemLongClick(AdapterView<&#63;> parent, View view, int position, long id) {
//        Toast.makeText(TestListViewActivity.this, mData.get(position) + "被点击了",
//            Toast.LENGTH_SHORT).show();
//        return false;
//      }
//    });
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<&#63;> parent, View view, int position, long id) {
        Log.e("SwipeListView", "setOnItemClickListener====" + mListView.canClick());
//        Toast.makeText(TestListViewActivity.this, mData.get(position) + "被点击了",
//            Toast.LENGTH_SHORT).show();
      }
    });
  }

第四步:布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>

  
  

第五步:item的布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>

  
    
    
  
  

重点注意:

int downPosition = pointToPosition(mDownX, mDownY);

downPosition 在使用过程中得到-1,导致后面方法调用异常!

以上所述是小编给大家介绍的Android 模仿QQ侧滑删除ListView功能示例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


推荐阅读
author-avatar
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有