亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android ListView滑動(dòng)刪除操作(SwipeListView)

 更新時(shí)間:2016年08月31日 11:38:44   作者:ly513782705  
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動(dòng)刪除操作,主要是學(xué)習(xí)SwipeListView開源框架。感興趣的小伙伴們可以參考一下

新版本的微信和QQ上引入的滑動(dòng)刪除功能是現(xiàn)在比較流行的一個(gè)功能。其實(shí)這個(gè)滑動(dòng)刪除的控件,github上已經(jīng)有了,是一個(gè)熱門的開源框架SwipeListView。不過,這個(gè)SwipeListView是一個(gè)framelayout,即是一個(gè)兩層的布局,上面的布局front覆蓋了下面的布局back,滑動(dòng)的時(shí)候則會(huì)滑開front,這樣下面的back就顯示出來了。但是看了一下微信的滑動(dòng)刪除好像不是這樣的,感覺更像是一個(gè)超出了屏幕的單層布局,滑動(dòng)的時(shí)候是右邊超出屏幕的button進(jìn)入屏幕,猜測(cè)應(yīng)該不是使用SwipeListView控件。QQ的滑動(dòng)刪除則是在ListView的item右邊隱藏一個(gè)button,但檢測(cè)到滑動(dòng)事件的時(shí)候,給button一個(gè)出現(xiàn)的動(dòng)畫,使其可見,這個(gè)方案應(yīng)該是最好實(shí)現(xiàn)的了。

本篇主要是學(xué)習(xí)SwipeListView這個(gè)開源框架。

使用這個(gè)框架有兩種方式,一種是導(dǎo)入SwipeListViewLibrary這個(gè)工程,將其作為一個(gè)android工程的依賴庫(kù)。由于SwipeListViewLibrary庫(kù)工程自身也依賴另外一個(gè)熱門的開源框架NineOldAndroids,這個(gè)也很容易就能網(wǎng)上或者github上搜到。

導(dǎo)入這兩個(gè)庫(kù)工程,對(duì)于NineOldAndroids,做如下設(shè)置,其實(shí)主要就是勾選Is Library這個(gè)選項(xiàng),這樣就能是NineOldAndroids工程作為別的工程的依賴庫(kù)使用: 

對(duì)于SwipeListViewLibrary,除了要勾選Is Library選項(xiàng),記得在旁邊的Add里面,加上上面的NineOldAndroids作為本庫(kù)的依賴庫(kù):

下面就是使用這個(gè)庫(kù)了,先clean一下上面兩個(gè)庫(kù)工程,很多時(shí)候工程的錯(cuò)誤,clean一下就好了。然后新建自己的工程,在Add選項(xiàng)里面添加SwipeListViewLibrary工程就行。這樣就能直接使用SwipeListView這個(gè)控件了,很簡(jiǎn)單,代碼如下: 

 <com.fortysevendeg.swipelistview.SwipeListView
  android:id="@+id/swipe_lv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:swipeMode="left"
  app:swipeAnimationTime="300"
  app:swipeOffsetLeft="200dp"
  app:swipeFrontView="@+id/front"
  app:swipeBackView="@+id/back"
  app:swipeActionLeft="reveal"/>

其中app:swipeFrontView屬性就是指定前面說的framelayout里面上面一層的view的id,app:swipeBackView則是指定下面一層的view的id,在下面自定義BaseAdatpter要使用的item的布局里面可以看到:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <RelativeLayout
    android:id="@+id/back"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
      android:id="@+id/close_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_toLeftOf="@+id/delete_btn"
      android:text="Close"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:focusable="false"
      android:focusableInTouchMode="false"/>
    <Button
      android:id="@+id/delete_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_alignParentRight="true"
      android:text="Delete"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:focusable="false"
      android:focusableInTouchMode="false"/>
  </RelativeLayout>
  <RelativeLayout
    android:id="@+id/front"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <TextView
      android:id="@+id/content_tv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:text="hello world"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textColor="@android:color/black"/>
  </RelativeLayout>
</FrameLayout>

在Activity里面初始化的代碼:

arrays = new ArrayList<String>(Arrays.asList(Util.arrays));

    mSwipeLv = (SwipeListView)findViewById(R.id.swipe_lv);
    mAdapter = new MyAdapter(this, arrays);
    mSwipeLv.setAdapter(mAdapter);
    mSwipeLv.setSwipeListViewListener(new BaseSwipeListViewListener() {

      @Override
      public void onClosed(int position, boolean fromRight) {
      }
    });

以及自定義BaseAdapter中的getView(): 

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

      if(convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(
            R.layout.layout_swipe_list_item, null);
        holder.mContentTv = (TextView)convertView.findViewById(R.id.content_tv);
        holder.mCloseBtn = (Button)convertView.findViewById(R.id.close_btn);
        holder.mDeleteBtn = (Button)convertView.findViewById(R.id.delete_btn);

        convertView.setTag(holder);
      } else {
        holder = (ViewHolder)convertView.getTag();
      }

      holder.mContentTv.setText(arrays.get(position));
      holder.mCloseBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          ((SwipeListView)parent).closeAnimate(position);
        }
      });
      holder.mDeleteBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          ((SwipeListView)parent).closeOpenedItems();
          mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
              mArrays.remove(position);
              mAdapter.notifyDataSetChanged();
            }
          }, 350);
        }
      });

      return convertView;
    }

然后就ok了,運(yùn)行工程的效果如下圖: 

另外一種是用SwipeListView控件的方法就是直接導(dǎo)入官方給出的兩個(gè)jar包,上面開篇的地址里可以看到,但是直接導(dǎo)入這兩個(gè)jar包,不代表可以立即使用了!首先先把這個(gè)包添加到新建工程的build path里面,如果你的工程沒有添加android的支持包android-support-v4.jar記得也添加以下,然后記得從前面已經(jīng)導(dǎo)入過的SwipeListViewLibrary庫(kù)工程中的res\values\swipelistview__attrs.xml文件復(fù)制到新建工程的res/values/目錄下,這個(gè)文件主要是申明SwipeListView控件里面的各項(xiàng)屬性的,直接導(dǎo)入的jar包是沒有包含申明這些屬性的文件的。然后就是向上面一樣在代碼里面引用了,不過需要注意兩點(diǎn):一,jar包里面SwipeListView的包名和庫(kù)工程里面的包名是不一樣的,引用的時(shí)候需要注意以下;二,準(zhǔn)備妥當(dāng),確認(rèn)前面步驟無誤后,有時(shí)在編譯工程時(shí)回報(bào)錯(cuò),說沒有申明swipeFrontView和swipeBackView兩個(gè)屬性,這個(gè)問題好像是SwipeListView框架的一個(gè)bug,stackoverflow上有人指出過,,大意就是在布局文件里面申明swipeFrontView和swipeBackView這兩個(gè)屬性的值得時(shí)候,最好不要自定義id的名稱,而是使用swipelistview_backview和swipelistview_frontview。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論