android中如何实现两个listview中的item可以互相拖拽移位?

发布于 2022-09-02 12:57:11 字数 169 浏览 31 评论 0

网上搜了搜,都是一个listview内部拖拽的,怎么实现两个listview之间的相互拖拽呢
图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2022-09-09 12:57:11

这两天自己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);
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文