拖拽排序ListViewdrag-sort-listview使用方法

最近要使用一种拖拽排序ListView,上网找了一下,貌似ListViewdrag-sort-listview不错,于是去看了下用法。先给出github地址:点击打开链接

不过自己用的时候各种麻烦,首先,我原本想用android studio的,复制文件夹到与app文件夹同源的目录,然后添加include和compile,syn一下,总是出现‘default’ not found的错,上网搜索了一下,才在一个英文论坛找到,是因为这个library没有build.gradle文件,因为作者没有更新,以前是用在eclipse上的,没办法,只能转战eclipse。


首先解压取出library文件夹,在eclipse中import它

新建一个工程,工程上右键properties->android,

再点击add添加引用library

这时候你去自己工程看gen文件夹,会多了一个com.mobeta.android.dslv文件夹


贴上activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:dslv="http://schemas.android.com/apk/res/com.example.dslvtest2"  
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.mobeta.android.dslv.DragSortListView 
        android:id="@+id/dslvlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        dslv:collapsed_height="1px"  
        dslv:drag_enabled="true"  
        dslv:drag_handle_id="@id/drag_handle"  
        dslv:drag_scroll_start="0.33"  
        dslv:drag_start_mode="onDown"  
        dslv:float_alpha="0.6"  
        dslv:remove_enabled="true"  
        dslv:remove_mode="clickRemove"  
        dslv:slide_shuffle_speed="0.3"
        />

</LinearLayout>

有些地方要注意,开头部分xmlns:要写自己的工程包,不是别人的

然后是不是注意到dslv:drag_handle_id="@id/drag_handle" 

一开始我也以为是在内容xml文件中声明的,然后去建一个item.xml,把某个的id设为

@+id/drag_handle,然后一直出错,我走进死胡同了,弄了好多方法都不行。算了,不说了

其实他们不在同一个xml里,不可以引用的


你只要打开gen目录下的R文件会发现有drag_handle这个东西的声明,然后在res/values下新建一个ids.xml,放两个id

<resources>
    <item type="id" name="drag_handle" />
    <item type="id" name="click_remove" />
</resources>
一个是点击拖拽的触发控件(可以设为imageviewj,textview,button...)

一个是触发点击删除的

算了,放代码好了

item.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="50dp"  
    android:background="#ffffff"  
    android:padding="10dp">  
      
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        >
        <TextView 
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="风吹麦浪"
            />
        <TextView 
            android:id="@+id/tv_artist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="李健"
            />
    </LinearLayout>
    <ImageView 
        android:id="@id/drag_handle"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drag_icon"
        />
    
</RelativeLayout>  



adapter

package com.example.dslvtest2;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AMDragRateAdapter extends BaseAdapter{

	LayoutInflater mInflater;
	List<body> items;
	public AMDragRateAdapter(Context context,List<body> list) {
		// TODO Auto-generated constructor stub
		this.items=list;
		mInflater=LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	public void remove(int position){
		items.remove(position);
		this.notifyDataSetChanged();
	}
	public void insert(body item,int position){
		items.add(position,item);
		this.notifyDataSetChanged();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder vh;
		if(convertView==null){
			vh=new ViewHolder();
			convertView=mInflater.inflate(R.layout.dlsv_item, null,false);
			vh.tv_title=(TextView) convertView.findViewById(R.id.tv_title);
			vh.tv_artist=(TextView) convertView.findViewById(R.id.tv_artist);
			convertView.setTag(vh);
		}else vh=(ViewHolder) convertView.getTag();
		body item=items.get(position);
		vh.tv_title.setText(item.getTitle());
		vh.tv_artist.setText(item.getArtist());
		
		return convertView;
	}

	static class ViewHolder{
		TextView tv_title;
		TextView tv_artist;
	}


}

main_activity.java
package com.example.dslvtest2;

import java.util.ArrayList;
import java.util.List;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	DragSortListView listview;
	AMDragRateAdapter adapter;
	List<body> data;
	private DropListener listener=new DropListener() {
		
		@Override
		public void drop(int from, int to) {
			// TODO Auto-generated method stub
			if(from!=to){
				body item=(body)adapter.getItem(from);
				adapter.remove(from);
				adapter.insert(item, to);
			}
		}
	};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        listview.setDropListener(listener);
        
        adapter=new AMDragRateAdapter(this, data);
        listview.setAdapter(adapter);
        listview.setDragEnabled(true);
    }
	private void initData() {
		// TODO Auto-generated method stub
		data =new ArrayList<body>();
		for(int i=0;i<30;i++){
			body b=new body();
			b.setArtist(i+" artist");
			b.setTitle(i+" title");
			data.add(b);
		}
		listview=(DragSortListView) findViewById(R.id.dslvlist);
	}
}
那个body的bean代码我就不放了,就两个属性和get,set方法。

那个,我是新手,如果有什么不对或者有更好的方法,请务必指出来,大家共同进步。谢谢

猜你喜欢

转载自blog.csdn.net/ccc905341846/article/details/50357706