欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ListView和ExpandableListView的侧滑删除操作 ListViewExpandableListView侧滑删除ListView侧滑删除ExpandableListView侧滑删除 

程序员文章站 2022-05-22 22:49:46
...

本案例主要实现了ListViewExpandableListView的侧滑删除操作功能

效果图:


         ListView和ExpandableListView的侧滑删除操作 
            
    
    
        ListViewExpandableListView侧滑删除ListView侧滑删除ExpandableListView侧滑删除 

 

代码:

 

自定义ListView
public class ZQListView extends ListView {
 
   private static final String TAG ="ZQListView ";
 
   private ZQView mFocusedItemView;
 
   float mX = 0;
   float mY = 0;
   private int mPosition = -1;
   boolean isSlider = false;
 
   public ZQListView(Contextcontext) {
      super(context);
   }
 
   public ZQListView(Contextcontext, AttributeSet attrs) {
      super(context,attrs);
   }
 
   public ZQListView(Contextcontext, AttributeSet attrs,int defStyle) {
      super(context,attrs, defStyle);
   }
 
   @Override
   public boolean onTouchEvent(MotionEvent event) {
      float x =event.getX();
      float y =event.getY();
      switch(event.getAction()) {
      case MotionEvent.ACTION_DOWN:
         isSlider = false;
         mX = x;
         mY = y;
         int position= pointToPosition((int) x, (int) y);
         if (mPosition !=position) {
            mPosition =position;
            if (mFocusedItemView !=null) {
                mFocusedItemView.reset();
            }
         }
         break;
      case MotionEvent.ACTION_MOVE:
         if (mPosition != -1) {
            if (Math.abs(mY - y) <30 && Math.abs(mX - x) > 20) {
                int first =this.getFirstVisiblePosition();
                int index =mPosition - first;
                mFocusedItemView =(ZQView) getChildAt(index);
                mFocusedItemView.onTouchEvent(event);
                isSlider = true;
                return true;
            }
         }
         break;
      case MotionEvent.ACTION_UP:
         if (isSlider) {
            isSlider = false;
            if (mFocusedItemView !=null) {
                mFocusedItemView.adjust(mX - x >0);
                return true;
            }
         }
         break;
      }
      return super.onTouchEvent(event);
   }
}

 

 

 

侧滑布局类
public class ZQView extends LinearLayout {
   private static final String TAG ="SlideView";
   private static final int TAN = 2;
   private int mHolderWidth = 120;
   private float mLastX = 0;
   private float mLastY = 0;
   private Context mContext;
   private LinearLayout mViewContent;
   private Scroller mScroller;
 
   public ZQView(Contextcontext, Resources resources) {
      super(context);
      initView();
   }
 
   public ZQView(Contextcontext) {
      super(context);
      initView();
   }
 
   public ZQView(Contextcontext, AttributeSet attrs) {
      super(context,attrs);
      initView();
   }
 
   private void initView() {
      setOrientation(LinearLayout.HORIZONTAL);
      mContext = getContext();
      mScroller = new Scroller(mContext);
      View.inflate(mContext,R.layout.delete_view,this);
      mViewContent = (LinearLayout)findViewById(R.id.view_content);
      mHolderWidth = Math.round(TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,mHolderWidth,getResources()
                   .getDisplayMetrics()));
   }
 
   public void setContentView(View view) {
      mViewContent.addView(view);
   }
 
   public void shrink(){
      int offset =getScrollX();
      if (offset== 0) {
         return;
      }
      scrollTo(0, 0);
   }
 
   public void reset() {
      int offset =getScrollX();
      if (offset== 0) {
         return;
      }
      smoothScrollTo(0, 0);
   }
 
   public void adjust(boolean left) {
      int offset =getScrollX();
      if (offset== 0) {
         return;
      }
      if (offset< 20) {
         this.smoothScrollTo(0,0);
      } else if (offset< mHolderWidth - 20) {
         if (left) {
            this.smoothScrollTo(mHolderWidth, 0);
         } else {
            this.smoothScrollTo(0,0);
         }
      } else {
         this.smoothScrollTo(mHolderWidth, 0);
      }
   }
 
   @Override
   public boolean onTouchEvent(MotionEvent event) {
      switch(event.getAction()) {
      case MotionEvent.ACTION_MOVE:
         float x =event.getX();
         float y =event.getY();
         float deltaX =x -mLastX;
         float delatY =y -mLastY;
         mLastX = x;
         mLastY = y;
         if (Math.abs(deltaX)< Math.abs(delatY) *TAN) {
            break;
         }
         if (deltaX!= 0) {
            float newScrollX = getScrollX() - deltaX;
            if(newScrollX < 0) {
                newScrollX = 0;
            } else if(newScrollX > mHolderWidth) {
                newScrollX = mHolderWidth;
            }
            this.scrollTo((int)newScrollX, 0);
         }
         break;
      }
      return super.onTouchEvent(event);
   }
 
   private void smoothScrollTo(int destX,int destY) {
      int scrollX =getScrollX();
      int delta =destX - scrollX;
      mScroller.startScroll(scrollX,0, delta, 0, Math.abs(delta) * 3);
      invalidate();
   }
 
   @Override
   public void computeScroll() {
      if (mScroller.computeScrollOffset()){
         scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
         postInvalidate();
      }
   }



 

 

布局文件

 

delete.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:Android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:id="@+id/view_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    </LinearLayout>
 
    <RelativeLayout
        android:id="@+id/holder"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:background="@drawable/holder_bg"
        android:clickable="true">
 
        <TextView
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawableLeft="@drawable/del_icon_normal"
            android:gravity="center"
            android:text="删除"
            android:textColor="@color/floralwhite"/>
    </RelativeLayout>
 
</merge>


listview.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <com.example.util.ZQListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff4f7f9"
        android:cacheColorHint="#00000000"
        android:divider="#6c6c6c"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="false"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:scrollingCache="false"/>
 
</LinearLayout>



 

Activity和适配器类
public class ActivityListViewDelete extends Activity implements
      OnItemClickListener, OnClickListener {
 
   private ZQListView mListView;
 
   private List<MessageItem> mMessageItems = new ArrayList<ActivityListViewDelete.MessageItem>();
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initView();
   }
 
   public static int[] Img = {R.drawable.ssdk_oks_classic_qq,
         R.drawable.ssdk_oks_classic_qzone,
         R.drawable.ssdk_oks_classic_shortmessage,
         R.drawable.ssdk_oks_classic_sinaweibo,
         R.drawable.ssdk_oks_classic_tencentweibo,
         R.drawable.ssdk_oks_classic_vkontakte,
         R.drawable.ssdk_oks_classic_wechat,
         R.drawable.ssdk_oks_classic_wechatfavorite,
         R.drawable.ssdk_oks_classic_wechatmoments,
         R.drawable.ssdk_oks_classic_yixinmoments };
 
   private void initView() {
      mListView = (ZQListView)findViewById(R.id.list);
      int length = Img.length;
      for (int i = 0; i< length; i++) {
         MessageItem item = new MessageItem();
         item.iconRes = Img[i];
         item.title = "标题" + (i + 1);
         item.msg = "消息" + (i + 1);
         item.time = "2016-6-1" + i;
         mMessageItems.add(item);
      }
      mListView.setAdapter(newSlideAdapter());
      mListView.setOnItemClickListener(this);
   }
 
   private class SlideAdapter extends BaseAdapter {
 
      private LayoutInflater mInflater;
 
      SlideAdapter() {
         super();
         mInflater =getLayoutInflater();
      }
 
      @Override
      public int getCount() {
         return mMessageItems.size();
      }
 
      @Override
      public Object getItem(int position) {
         return mMessageItems.get(position);
      }
 
      @Override
      public long getItemId(int position) {
         return position;
      }
 
      @Override
      public View getView(int position, View convertView, ViewGroupparent) {
         ViewHolder holder;
         ZQView slideView = (ZQView)convertView;
         if(slideView == null) {
            View itemView = mInflater.inflate(R.layout.list_item,null);
 
            slideView = newZQView(ActivityListViewDelete.this);
            slideView.setContentView(itemView);
            holder = newViewHolder(slideView);
            slideView.setTag(holder);
         } else {
            holder = (ViewHolder) slideView.getTag();
         }
         MessageItem item = mMessageItems.get(position);
         slideView.shrink();
         holder.icon.setImageResource(item.iconRes);
         holder.title.setText(item.title);
         holder.time.setText(item.time);
         holder.deleteHolder.setOnClickListener(ActivityListViewDelete.this);
 
         return slideView;
      }
 
   }
 
   public class MessageItem {
      public int iconRes;
      public String title;
      public String msg;
      public String time;
   }
 
   private static class ViewHolder {
      public ImageViewicon;
      public TextView title;
      public TextView time;
      public ViewGroup deleteHolder;
 
      ViewHolder(View view) {
         icon =(ImageView) view.findViewById(R.id.icon);
         title =(TextView) view.findViewById(R.id.title);
         time =(TextView) view.findViewById(R.id.time);
         deleteHolder =(ViewGroup) view.findViewById(R.id.holder);
      }
   }
 
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
         long id) {
      Toast.makeText(this,"onItemClickposition=" + position, 1).show();
 
   }
 
   @Override
   public void onClick(View v) {
      if(v.getId() == R.id.holder) {
         Toast.makeText(this,"删除操作",1).show();

      }
   }
 
}



 

此代码是ListView的侧滑删除操作,ExpandableListView未贴出来,下载代码即可,两个都有。

 

 

源码下载:

 

(注:ExpandableListView的侧滑删除有点错误)

Eclipse下载:http://download.csdn.net/detail/dickyqie/9627652

AndroidStudio下载:https://github.com/DickyQie/ListViewExpandableListViewDelete