栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android仿QQ微信侧滑删除效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap

Android仿QQ微信侧滑删除效果

仿QQ侧滑删除效果图

1.自定义listview

public class DragDelListView extends ListView {

  private boolean moveable=false;
  private boolean closed=true;
  private float mDownX,mDownY;
  private int mTouchPosition,oldPosition=-1;
  private DragDelItem mTouchView,oldView;
  private Context context;
  public DragDelListView(Context context) {
    super(context);
    init(context);
  }
  public DragDelListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
  }
  public DragDelListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }
  private void init(Context context) {
    this.context=context;
  }


  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
      mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
      mTouchView=(DragDelItem)getChildAt(mTouchPosition - getFirstVisiblePosition());
      mDownX = ev.getX();
      mDownY=ev.getY();
      if(oldPosition==mTouchPosition ||closed)
      {
 moveable=true;
 mTouchView.mDownX =(int)mDownX;
      }else
      {
 moveable=false;
 if(oldView!=null)
 {
   oldView.smoothCloseMenu();
 }
      }
      oldPosition=mTouchPosition;
      oldView=mTouchView;
      break;
    case MotionEvent.ACTION_MOVE:
      if (Math.abs(mDownX-ev.getX()) < Math.abs(mDownY-ev.getY()) * dp2px(2)) {
 break; 
      } 
      if (moveable) 
      {
 int dis = (int) (mTouchView.mDownX -ev.getX());
 if(mTouchView.state==mTouchView.STATE_OPEN)
   dis+=mTouchView.mMenuView.getWidth();
 mTouchView.swipe(dis);
 ev.setAction(MotionEvent.ACTION_CANCEL);
      }

      break;
    case MotionEvent.ACTION_UP:

      if (moveable) 
      {
 if ((mTouchView.mDownX -ev.getX()) > (mTouchView.mMenuView.getWidth()/2)) {
   // open
   mTouchView.smoothOpenMenu();
   closed=false;
 } else {
   // close
   mTouchView.smoothCloseMenu();
   closed=true;
 }
 ev.setAction(MotionEvent.ACTION_CANCEL);
      }
      break;
    }
    return super.onTouchEvent(ev);
  }

  private int dp2px(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
 getContext().getResources().getDisplayMetrics());
  }


}

2.自定义滑动条目

public class DragDelItem extends LinearLayout {

  public static final int STATE_CLOSE = 0;
  public static final int STATE_OPEN = 1;
  private View mContentView;
  public View mMenuView;
  public int mDownX;
  public int state = STATE_CLOSE;
  public boolean isFling;
  private int mbaseX;
  private Scroller scroll;

  public DragDelItem(View contentView, View menuView) {
    super(contentView.getContext());
    scroll=new Scroller(getContext());
    mContentView = contentView;
    mMenuView = menuView;
    init();
  }

  private DragDelItem(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  private DragDelItem(Context context) {
    super(context);
  }


  private void init() {
    setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
 LayoutParams.WRAP_CONTENT));
    LayoutParams contentParams = new LayoutParams(
 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mContentView.setLayoutParams(contentParams);

    mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
 LayoutParams.WRAP_CONTENT));

    addView(mContentView);
    addView(mMenuView);

  }


  public void swipe(int dis) {
    if (dis > mMenuView.getWidth()) {
      dis = mMenuView.getWidth();
    }
    if (dis < 0) {
      dis = 0;
    }
    mContentView.layout(-dis, mContentView.getTop(),
 mContentView.getWidth() - dis, getMeasuredHeight());
    mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),
 mContentView.getWidth() + mMenuView.getWidth() - dis,
 mMenuView.getBottom());
  }

  @Override
  public void computeScroll() {
    if (state == STATE_OPEN) {
      if (scroll.computeScrollOffset()) {
 swipe(scroll.getCurrX());
 postInvalidate();
      }
    } else {
      if (scroll.computeScrollOffset()) {
 swipe(mbaseX - scroll.getCurrX());
 postInvalidate();
      }
    }
  }

  public void smoothCloseMenu() {
    state = STATE_CLOSE;
    mbaseX = -mContentView.getLeft();
    scroll.startScroll(0, 0, mbaseX, 0, 350);
    postInvalidate();
  }

  public void smoothOpenMenu() {

    state = STATE_OPEN;
    scroll.startScroll(-mContentView.getLeft(), 0,
 mMenuView.getWidth()/2, 0, 350);
    postInvalidate();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    mMenuView.measure(MeasureSpec.makeMeasureSpec(0,
 MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(
 getMeasuredHeight(), MeasureSpec.EXACTLY));
    mContentView.measure(MeasureSpec.makeMeasureSpec(0,
 MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(
 getMeasuredHeight(), MeasureSpec.EXACTLY));

  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mContentView.layout(0, 0, getMeasuredWidth(),
 mContentView.getMeasuredHeight());
    mMenuView.layout(getMeasuredWidth(), 0,
 getMeasuredWidth() + mMenuView.getMeasuredWidth(),
 mContentView.getMeasuredHeight());

  }

}

3.所用到的布局文件

—swipecontent.xml代码


    

    


—swipemenu.xml代码



  
  


4.主界面代码

public class MainActivity extends Activity {
  private List mAppList;
  private DragDelListView mListView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    mAppList = getPackageManager().getInstalledApplications(0);
    mListView = (DragDelListView) findViewById(R.id.listView);
    mListView.setAdapter(new AppAdapter(mAppList));
  }
  class AppAdapter extends baseAdapter {
    private List mAppList;
    public AppAdapter(List appList)
    {
      mAppList=appList;
    }
    @Override
    public int getCount() {
      return mAppList.size();
    }

    @Override
    public ApplicationInfo getItem(int position) {
      return mAppList.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

      ViewHolder holder=null;
      View menuView=null;
      if (convertView == null) {
 convertView = View.inflate(getApplicationContext(),
     R.layout.swipecontent, null);
 menuView = View.inflate(getApplicationContext(),
     R.layout.swipemenu, null);
 convertView = new DragDelItem(convertView,menuView);
 holder=new ViewHolder(convertView);
      } else {
 holder = (ViewHolder) convertView.getTag();
      }
 ApplicationInfo item = getItem(position);
 holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));
 holder.tv_name.setText(item.loadLabel(getPackageManager()));
 holder.tv_open.setonClickListener(new View.onClickListener() {
   @Override
   public void onClick(View arg0) {
     Toast.makeText(MainActivity.this, "置顶:"+position, Toast.LENGTH_SHORT).show();
   }
 });
 holder.tv_del.setonClickListener(new View.onClickListener() {

   @Override
   public void onClick(View arg0) {
     Toast.makeText(MainActivity.this, "删除:"+position, Toast.LENGTH_SHORT).show();
   }
 });
      return convertView;
    }

    class ViewHolder {
      ImageView iv_icon;
      TextView tv_name;
      TextView tv_open,tv_del;
      RelativeLayout relativeLayout;
      public ViewHolder(View view) {
 iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
 tv_name = (TextView) view.findViewById(R.id.tv_name);
 tv_open=(TextView)view.findViewById(R.id.tv_open);
 tv_del=(TextView)view.findViewById(R.id.tv_del);
 relativeLayout = (RelativeLayout) view.findViewById(R.id.rl_layout);
 //改变relativeLayout宽度
 WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
 int width = wm.getDefaultDisplay().getWidth();
 relativeLayout.setMinimumWidth(width-60);
 view.setTag(this);
      }
    }
  }
}

主界面布局代码



    



以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号