www.yzc261.comandroid中怎样完成两个listview中的item可以互相拖拽移位?

( www.yzc261.com )

网上搜了搜,都是一个listview外部拖拽的,怎样完成两个listview之间的互相拖拽呢

这两天自己baidu+google,复杂处理了一些题目,临时先贴下去。
由于是我的一个作业,以是另有一些按钮的点击事变,大家可以忽略。
这些源码仅仅完成了复杂的”将订定控件拖拽到某个地区“,还没有完成”拖拽到指定地位“,后续处理了之后会在贴下去

package com.example.alpha.app2_4;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends Activity implements OnClickListener {
    ListView lv1;
    ListView lv2;
    LinearLayout targetLayout,fromLayout;
    ArrayAdapter<String> arr_Adapter;
    ArrayAdapter<String>arr_Adapter2;
    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;
    Button bt5;
    Button bt6;
    static int po_l = 0,po_r=0;
    ArrayList<String>arr_data1=new ArrayList<String>(Arrays.asList("上海",
            "南京", "武汉", "北京", "重庆", "深圳", "兰州", "沈阳") );
    ArrayList<String> arr_data2;
    static Boolean flag=true;//定义左右标记
    static int longClick_Flag;//定义长按左右标记

    //实例化拖拽项目监听器
    MyDragEventListener myDragEventListener=new MyDragEventListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = (Button) findViewById(R.id.button1);
        bt2 = (Button) findViewById(R.id.button2);
        bt3 = (Button) findViewById(R.id.button3);
        bt4 = (Button) findViewById(R.id.button4);
        bt5 = (Button) findViewById(R.id.button5);
        bt6 = (Button) findViewById(R.id.button6);
        lv1 = (ListView) findViewById(R.id.listView1);
        lv2 = (ListView) findViewById(R.id.listView2);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);

        arr_Adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, arr_data1);
        lv1.setAdapter(arr_Adapter);
        lv1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                po_l=position;
                flag=true;//true即点击了lv1
                Log.d("tag", "po_l="+po_l);
            }
        });
        arr_data2=new ArrayList<String>();
        arr_Adapter2=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr_data2);
        lv2.setAdapter(arr_Adapter2);
        lv2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                po_r=position;
                flag=false;//false即点击了lv2
            }
        });


        /**
         * Drag_Start
         */
        targetLayout= (LinearLayout) findViewById(R.id.TargetLayout);
        fromLayout= (LinearLayout) findViewById(R.id.FromLayout);
        lv1.setOnItemLongClickListener(lv1LongClickListener);
        lv2.setOnItemLongClickListener(lv2LongClickListener);

        lv1.setOnDragListener(myDragEventListener);
        targetLayout.setOnDragListener(myDragEventListener);
        fromLayout.setOnDragListener(myDragEventListener);
        lv2.setOnDragListener(myDragEventListener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //左移
            case R.id.button1:
                if (lv2.getChildCount()>=po_r+1) {
                    String cu=(String)lv2.getItemAtPosition(po_r);
                    Log.d("tag", "count="+lv2.getChildCount()+"po_l"+po_r);
                    arr_data2.remove(po_r);
                    arr_data1.add(cu);
                    arr_Adapter.notifyDataSetChanged();
                }
                else {
                    Toast.makeText(MainActivity.this,"以后地位已无内容",Toast.LENGTH_SHORT).show();
                }
                arr_Adapter2.notifyDataSetChanged();
                break;
            //右移
            case R.id.button2:
                if (lv1.getChildCount()>=po_l+1) {
                    String cu=(String)lv1.getItemAtPosition(po_l);
                    Log.d("tag", "count="+lv1.getChildCount()+"po_l"+po_l);
                    arr_data1.remove(po_l);
                    arr_data2.add(cu);
                    arr_Adapter2.notifyDataSetChanged();
                }
                else {
                    Toast.makeText(MainActivity.this,"以后地位已无内容",Toast.LENGTH_SHORT).show();
                }
                arr_Adapter.notifyDataSetChanged();
                break;
            //全部左移
            case R.id.button3:
                arr_data1.addAll(arr_data2);
                arr_data2.clear();
                arr_Adapter.notifyDataSetChanged();
                arr_Adapter2.notifyDataSetChanged();
                break;
            //全部右移
            case R.id.button4:
                arr_data2.addAll(arr_data1);
                arr_data1.clear();
                arr_Adapter.notifyDataSetChanged();
                arr_Adapter2.notifyDataSetChanged();
                break;
            //上移
            case R.id.button5:
                if(flag&&po_l>0){
                    String ch=arr_data1.get(po_l);
                    arr_data1.remove(po_l);
                    arr_data1.add(po_l-1,ch);
                    arr_Adapter.notifyDataSetChanged();
                    po_l--;
                }
                else if(!flag&&po_r>0){
                    String ch=arr_data2.get(po_r);
                    arr_data2.remove(po_r);
                    arr_data2.add(po_r-1,ch);
                    arr_Adapter2.notifyDataSetChanged();
                    po_r--;
                }
                break;
            //下移
            case R.id.button6:
                if(flag&&po_l<lv1.getChildCount()-1){
                    String ch=arr_data1.get(po_l);
                    arr_data1.remove(po_l);
                    arr_data1.add(po_l+1,ch);
                    arr_Adapter.notifyDataSetChanged();
                    po_l++;
                }
                else if(!flag&&po_r<lv2.getChildCount()-1){
                    String ch=arr_data2.get(po_r);
                    arr_data2.remove(po_r);
                    arr_data2.add(po_r+1,ch);
                    arr_Adapter2.notifyDataSetChanged();
                    po_r++;
                }
                break;

            default:
                break;
        }
    }


    /**
     * DragStart_to_End
     */


    //listView1长按事变
    AdapterView.OnItemLongClickListener lv1LongClickListener=new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            longClick_Flag=1;
            //po_l=position;
            // 将被拖动item转达到clipData
            ClipData.Item item=new ClipData.Item(arr_data1.get(position));
            String[] clipDescription={ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData=new ClipData((CharSequence) view.getTag(),clipDescription,item);
            //制造拖动影像
            View.DragShadowBuilder myShadow=new MyDragShadowBuilder(view);
            view.startDrag(dragData,myShadow,arr_data1.get(position),0);
            return true;
        }
    };
    //listView2长按事变
    AdapterView.OnItemLongClickListener lv2LongClickListener=new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            longClick_Flag=2;
            //po_r=position;
            // 将被拖动item转达到clipData
            ClipData.Item item=new ClipData.Item(arr_data2.get(position));
            String[] clipDescription={ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData=new ClipData((CharSequence) view.getTag(),clipDescription,item);
            //制造拖动影像
            View.DragShadowBuilder myShadow=new MyDragShadowBuilder(view);
            view.startDrag(dragData,myShadow,arr_data2.get(position),0);
            return true;
        }
    };

    //监听拖拽事变和形状
    protected class MyDragEventListener implements View.OnDragListener{

        @Override
        public boolean onDrag(View v, DragEvent event) {//v代表目标视图
            //定义拖拽形状
            final int action=event.getAction();
            switch (action){
                //鼠标长按下末尾拖动
                case DragEvent.ACTION_DRAG_STARTED:
                    // All involved view accept ACTION_DRAG_STARTED for
                    // MIMETYPE_TEXT_PLAIN
                    if (event.getClipDescription().hasMimeType(
                            ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                        return true; // Accept
                    } else {
                        return false; // reject
                    }
                case DragEvent.ACTION_DRAG_ENTERED:
                    return true;
                case DragEvent.ACTION_DRAG_LOCATION:
                    return true;
                case DragEvent.ACTION_DRAG_EXITED:
                    return true;
                case DragEvent.ACTION_DROP:
                    // Gets the item containing the dragged data
                    //获取被拖动的item
                    ClipData.Item item = event.getClipData().getItemAt(0);

                    // If apply only if drop on buttonTarget
                    //由左向右拖动
                    if (v == targetLayout||v==lv2) {
                        String droppedItem = item.getText().toString();

                        arr_data2.add(droppedItem);
                        arr_Adapter2.notifyDataSetChanged();

                        arr_data1.remove(droppedItem);
                        arr_Adapter.notifyDataSetChanged();

                        return true;
                    }
                    //由右向左拖动
                    if (v == fromLayout||v==lv1) {
                        String droppedItem = item.getText().toString();

                        arr_data1.add(droppedItem);
                        arr_Adapter.notifyDataSetChanged();

                        arr_data2.remove(droppedItem);
                        arr_Adapter2.notifyDataSetChanged();

                        return true;
                    }
                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    return true;
                default: // unknown case
                    return false;
            }
        }
    }

    //构造拖动影像
    private static class MyDragShadowBuilder extends View.DragShadowBuilder{
        private static Drawable shadow;
        //天生影像
        public MyDragShadowBuilder(View view) {
            super(view);
            shadow=new ColorDrawable(Color.LTGRAY);
        }
        //设置影像参数
        @Override
        public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
            super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
            int width=getView().getWidth();
            int height=getView().getHeight();
            shadow.setBounds(0,0,width,height);
            shadowSize.set(width,height);
            shadowTouchPoint.set(width/2,height/2);
        }
        //画影像
        @Override
        public void onDrawShadow(Canvas canvas) {
            //super.onDrawShadow(canvas);
            shadow.draw(canvas);
        }
    }

}

(看完/读完)这篇文章有何感想! www.yzc261.com的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注