Android 滑动删除viewGroup
来源:互联网 发布:合工大网络视频课 编辑:程序博客网 时间:2024/06/05 20:20
MainActivity
package zenglei.com.myapplication;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { private ListView mLvContent; private ArrayList<String> mList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLvContent = (ListView) findViewById(R.id.lv_content); mList = new ArrayList<>(); for (int i = 0; i < 30; i++) { mList.add("item .. " + i); } mLvContent.setAdapter(new MyAdapter()); } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return mList.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if(convertView == null){ convertView = View.inflate(MainActivity.this,R.layout.item_delete,null); holder = new ViewHolder(convertView); //设置标记 convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } //设置数据 final String data = mList.get(position); holder.tvContent.setText(data); //删除事件 holder.tvDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //移除数据 mList.remove(data); //通知更新界面 notifyDataSetChanged(); //重新布局 if(holder.svSweep.isOpen()){ //重新布局 holder.svSweep.requestLayout(); } } }); return convertView; } class ViewHolder { TextView tvContent; TextView tvDelete; SweepView svSweep; public ViewHolder(View view){ tvContent = (TextView) view.findViewById(R.id.tv_content); tvDelete = (TextView) view.findViewById(R.id.tv_delete); svSweep = (SweepView) view.findViewById(R.id.sv_sweep); } } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } }}
SweepView
package zenglei.com.myapplication;import android.content.Context;import android.support.v4.widget.ViewDragHelper;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import static android.content.ContentValues.TAG;/** * Created by jonny on 2016/10/11 0011. */public class SweepView extends ViewGroup { private View mLeftView; private View mRightView; private ViewDragHelper mHelper; public SweepView(Context context) { // super(context); this(context, null); } public SweepView(Context context, AttributeSet attrs) { super(context, attrs); //处理触摸事件的工具类 mHelper = ViewDragHelper.create(this, mCb); } private ViewDragHelper.Callback mCb = new ViewDragHelper.Callback() { //让控件可以滑动 @Override public boolean tryCaptureView(View child, int pointerId) { return true; } //返回孩子的左侧位置 @Override public int clampViewPositionHorizontal(View child, int left, int dx) { if (child == mLeftView) { //左边临界点,边界 int maxLeft = 0; int minLeft = -mRightView.getWidth(); Log.d(TAG, "clampViewPositionHorizontal: " + left); if (left > maxLeft) { left = maxLeft; } else if (left < minLeft) { left = minLeft; } } return left; } //滑动后,布局孩子位置 @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { Log.d(TAG, "onViewPositionChanged: " + mLeftView.getLeft() + ".." + dx + ".." + mLeftView.getRight()); // 判断当前拖动的是哪个孩子,如果是左边,那么我们需要布局右边孩子, if (changedView == mLeftView) { //布局右侧 mRightView.layout(mRightView.getLeft() + dx, 0, mRightView.getRight() + dx, mRightView.getBottom()); } else if (changedView == mRightView) { //布局左侧孩子 mLeftView.layout(mLeftView.getLeft() + dx, 0, mLeftView.getRight() + dx, mLeftView.getBottom()); } // super.onViewPositionChanged(changedView, left, top, dx, dy); } //当手指松开的处理 @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { //计算临界点 int linX = mLeftView.getWidth() - mRightView.getWidth()/2; //当滑动的位置大于,左侧控件的右边x左边+ 右侧控件的一般,右侧控件出来 if(mRightView.getLeft() > linX){ // 显示右侧画出屏幕 int finalLeft = mLeftView.getWidth(); mHelper.smoothSlideViewTo(mRightView,finalLeft,0); //重新绘制 invalidate(); isOpen = false; }else{ // 让右侧先出来 int finaleft = mLeftView.getWidth() - mRightView.getWidth(); mHelper.smoothSlideViewTo(mRightView,finaleft,0); invalidate(); isOpen = true; } super.onViewReleased(releasedChild, xvel, yvel); } }; private boolean isOpen = false; public boolean isOpen(){ return isOpen; } @Override public void computeScroll() { if(mHelper.continueSettling(true)){ //重新绘制 invalidate(); } // super.computeScroll(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 测量孩子 measureChildren(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { //获取两个孩子 mLeftView = getChildAt(0); mRightView = getChildAt(1); //左边孩子宽度 int leftW = mLeftView.getMeasuredWidth(); Log.d(TAG, "onLayout: " + leftW + ".." + mLeftView.getMeasuredWidth()); //布局左边孩子 mLeftView.layout(0, 0, leftW, mLeftView.getMeasuredHeight()); //布局右边孩子 mRightView.layout(mLeftView.getMeasuredWidth(), 0, leftW + mRightView.getMeasuredWidth(), mRightView.getMeasuredHeight()); } @Override public boolean onTouchEvent(MotionEvent event) { mHelper.processTouchEvent(event); return true; //自己消费,自己处理 }}Xml-----item_delete
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <zenglei.com.myapplication.SweepView android:id="@+id/sv_sweep" android:layout_width="match_parent" android:layout_height="80dp" > <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#66000000"/> <TextView android:id="@+id/tv_delete" android:layout_width="100dp" android:layout_height="match_parent" android:background="#ff0000" android:gravity="center" android:text="删除" android:textSize="18sp"/> </zenglei.com.myapplication.SweepView></LinearLayout>activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zenglei.com.myapplication.MainActivity"> <ListView android:id="@+id/lv_content" android:layout_width="match_parent" android:layout_height="match_parent"></ListView></RelativeLayout>
0 0
- Android 滑动删除viewGroup
- ListView滑动删除之Viewgroup打造滑动控件(修正版)
- android viewGroup左右滑动方法1
- android使用ViewGroup实现滑动菜单
- android view 自定义viewgroup 实例--屏幕滑动
- Android:跟手滑动的布局ViewGroup
- Android ListView滑动删除
- android,listView, 滑动删除
- android listview滑动删除
- Android ListView 滑动删除
- Android滑动删除控件
- Android滑动删除ListView
- Android滑动删除activity
- SwipeListView滑动删除Android
- Android-滑动删除布局
- android滑动删除listview
- android-使用ViewGroup实现左右滑动切换界面的效果
- android 手势滑动(ViewGroup, Scroller, VelocityTracker, ViewConfiguration)
- 磁盘读写能力测试
- Android 调用webservice(ksoap-2-Android)手机号码归属地查询
- Linux下MySQL导入文件出错ERROR 1290 (HY000)
- c!#!远!程!关!闭!与!重!启!计!算!机
- 掌握 tar 命令让你秒变大牛
- Android 滑动删除viewGroup
- 《锋利的jQuery》第二章、jQuery选择器
- 数组,指针的移动
- 面向对象_接口的特点
- 1079. Total Sales of Supply Chain (25)
- 在工作方式0下,13位工作方式,计数器是由TH的全部8位和TL的5位组成,其计数范围是?
- "Missing or invalid signature. The bundle '' at bundle path '' is not signed using an Apple submiss
- 【条件概率】Headshot, ACM/ICPC NEERC 2009, UVa1636
- MATLAB数组的输入方式