2015年4月13日 星期一

Android DragSortListView 拖動 自訂 排序

實作像facebook Messenger的貼圖自訂排序


1.下載檔案https://github.com/terryyamg/DragSortListViewTest
如果要看所有demo,請前往https://github.com/bauerca/drag-sort-listview下載

這裡舉demo裡有刪除的例子-簡化版
匯入library

File -> improt Existing Android Code Into Workspace -> Next

Browse...選擇下載的library -> 打勾Copy projects into workspace-> Finish


建立專案->在專案點右鍵-> Properties-> 點選Android ->Add

選擇library ->OK

出現綠色打勾,點選OK完成匯入

2./res/values/string.xml


    DragSortListViewTest

    
        Orioles
        Blue Jays
        Red Sox
        Yankees
        Tigers
        Indians
    


3./res/values/ids.xml

    
    

4./res/values/main.xml --主畫面(注意xmlns:dslv="http://schemas.android.com/apk/res/tw.android的package名稱要與自己的app相同)



5./res/values/list_item_checkable.xml --項目選項(注意tw.android.CheckableLinearLayout的package名稱要與自己的app相同)


    
    

    
    

    
    


6./res/drawable加入兩張圖片delete_x.pngdrag.9.png

7.Main.java
package tw.android;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.mobeta.android.dslv.DragSortListView;
import com.mobeta.android.dslv.DragSortListView.RemoveListener;

public class Main extends ListActivity {
 private ArrayAdapter<String> adapter;

 /*拖拉選項*/
 private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
  public void drop(int from, int to) {
   Log.i("from", from+"");
   Log.i("to", to+"");
   if (from != to) { //選項有移動位置
    DragSortListView list = getListView();
    String item = adapter.getItem(from);
    adapter.remove(item); //移除初始A的位置
    adapter.insert(item, to); //將A加入到B的位置
    list.moveCheckState(from, to);
   }
  }
 };

 /*移除選項*/
 private RemoveListener onRemove = new DragSortListView.RemoveListener() {
  public void remove(int which) {
   Log.i("which", which+"");
   DragSortListView list = getListView();
   String item = adapter.getItem(which);
   adapter.remove(item); //移除選中位置
   list.removeCheckState(which);
  }
 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  /*建立選單*/
  String[] array = getResources().getStringArray(R.array.mlb_team); //在res/valuse/string.xml 建立選單
  ArrayList<String> arrayList = new ArrayList<String>(
    Arrays.asList(array));

  adapter = new ArrayAdapter<String>(this, R.layout.list_item_checkable, //選單layout樣式
    R.id.text, arrayList);

  setListAdapter(adapter);

  DragSortListView list = getListView();
  list.setDropListener(onDrop);
  list.setRemoveListener(onRemove);
 }

 @Override
 public DragSortListView getListView() {
  return (DragSortListView) super.getListView();
 }

}
8.CheckableLinearLayout.java
package tw.android;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.LinearLayout;

public class CheckableLinearLayout extends LinearLayout implements Checkable {

    private static final int CHECKABLE_CHILD_INDEX = 1;
    private Checkable child;

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        child = (Checkable) getChildAt(CHECKABLE_CHILD_INDEX);
    }

    public boolean isChecked() {
        return child.isChecked(); 
    }

    public void setChecked(boolean checked) {
        child.setChecked(checked);
    }

    public void toggle() {
        child.toggle();
    }
    
}
移除

檔案下載:
https://github.com/terryyamg/DragSortListViewTest
參考來源:
https://github.com/bauerca/drag-sort-listview

沒有留言 :

張貼留言