android - 修改 TouchList View 以从列表拖动到另一个 View ( ImageView )

标签 android commonsware-cwac

我正在尝试修改 TouchListView 组件以允许我将项目从 ListView 拖到另一个 View 上,以创建拖放选择系统。用户将拖动一个列表项并释放它以指示选择。

到目前为止,我已经修改了 DropListener 类的 onDrop 方法以传递 MotionEvent。我正在使用运动的 getY() 方法来确定用户是否将拖动的 View 放在 ImageView 上。我使用的是 getY() 的原始值,如果它小于 -50,我接受它作为一个有效的下降。

但我担心,这是一个相当笨拙的解决方案,而且容易出错。有没有更好的方法来确定用户何时在目标 View 区域上释放?在某些情况下我不能简单地寻找负数吗?有没有办法实际查看给定的 X/Y 坐标集下方的 View ?

请注意所附的正在进行布局的图片。

enter image description here

最佳答案

这是我想出的有效解决方案。

package com.commonsware.cwac.tlv.demo;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.commonsware.cwac.tlv.TouchListView;

public class TouchListViewDemo extends ListActivity {
    private static String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
                                                                    "consectetuer", "adipiscing", "elit", "morbi", "vel",
                                                                    "ligula", "vitae", "arcu", "aliquet", "mollis",
                                                                    "etiam", "vel", "erat", "placerat", "ante",
                                                                    "porttitor", "sodales", "pellentesque", "augue", "purus"};
    private TouchListView tlv;
    private IconicAdapter adapter=null;
    private ArrayList<String> array=new ArrayList<String>(Arrays.asList(items));

    private TextView target_name_display;
    private ImageView target;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        tlv = (TouchListView)getListView();
        adapter=new IconicAdapter();
        setListAdapter(adapter);

        tlv.setDropListener(onDrop);
        tlv.setRemoveListener(onRemove);


        target = (ImageView) findViewById(R.id.drag_target_area);
        target_name_display = (TextView) findViewById(R.id.name_text);
        Log.d("TARGET", "Target:"+target);
        Log.d("TARGET", "TargeName Displayt:"+target_name_display);


        ViewTreeObserver vto = target.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                target.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                int height = target.getHeight();
                int top = target.getTop();
                int tlv_y_position = tlv.getTop();
                Log.d("ON LAYOUT", "Height:"+ height+ " top:"+top+" tlv_y_position:"+tlv_y_position);               
            }
        });

    }

    private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
        @Override
        public void drop(int from, int to, MotionEvent e) {
            Log.i("TOUCH VIEW DEMO", "From: "+from + "  TO: "+to );
            Log.i("TOUCH VIEW DEMO", "Event: "+e.getY());
                String item=adapter.getItem(from);

                //
                int[] target_coords = new int[2]; 
                int[] tlv_coords = new int[2]; 
                target.getLocationInWindow(target_coords);
                tlv.getLocationInWindow(tlv_coords);
                int height = target.getMeasuredHeight();
                int negative_top = -1*(tlv_coords[1]-target_coords[1]);
                Log.d("TOUCH VIEW DEMO", " negative_top:"+negative_top + " e.getY():"+e.getY() + " heigh:"+height);
                Log.d("TOUCH VIEW DEMO", "Target Coords:"+ target_coords[0] +","+ target_coords[1]);
                Log.d("TOUCH VIEW DEMO", "Tlv Coords:"+ tlv_coords[0] +","+ tlv_coords[1]);
                if(e.getY() > negative_top && e.getY() < negative_top + height){
                    target_name_display.setText(item.toString());
                    adapter.remove(item);
                }
                //adapter.remove(item);
                //adapter.insert(item, to);
        }
    };

    private TouchListView.RemoveListener onRemove=new TouchListView.RemoveListener() {
        @Override
        public void remove(int which) {
                adapter.remove(adapter.getItem(which));
        }
    };

    class IconicAdapter extends ArrayAdapter<String> {
        IconicAdapter() {
            super(TouchListViewDemo.this, R.layout.row2, array);
        }

        public View getView(int position, View convertView,
                                                ViewGroup parent) {
            View row=convertView;

            if (row==null) {                                                    
                LayoutInflater inflater=getLayoutInflater();

                row=inflater.inflate(R.layout.row2, parent, false);
            }

            TextView label=(TextView)row.findViewById(R.id.label);

            label.setText(array.get(position));

            return(row);
        }
    }
}

代码包含很多注释和每次计算的变量,我将重构它们。但您可以修改 TouchListView 演示以自行完成这项工作。

这是我使用的 main.xml 布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/drag_here_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1.0"
        android:background="@drawable/matches_current_match_bg"
        android:paddingBottom="30dp" >

        <TextView
            android:id="@+id/drag_here_text"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="15dp"
            android:text="DRAG HERE"
            android:textSize="14dp" />

        <ImageView
            android:id="@+id/drag_target_area"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/drag_here_text"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#00000000"
            android:scaleType="fitXY"
            android:src="@drawable/match_drag_here_target" />
        <TextView
            android:id="@+id/name_text"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignTop="@id/drag_target_area"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textSize="14dp" />
    </RelativeLayout>
<com.commonsware.cwac.tlv.TouchListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tlv="http://schemas.android.com/apk/res/com.commonsware.cwac.tlv.demo"

    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    tlv:normal_height="64dip"
    tlv:grabber="@+id/icon"
    tlv:remove_mode="slideRight"
    android:layout_weight="1.0"
/>
</LinearLayout>

再次感谢 CommonsWare 的优秀 TouchListView Widget .

关于android - 修改 TouchList View 以从列表拖动到另一个 View ( ImageView ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883766/

相关文章:

java - 空编辑文本给出 0 值错误

android - 处理 android 和 django 时使用什么日期格式?

android - Android 上的 SQLite 是否使用为 FTS 启用的 ICU 分词器构建?

java - 为什么我的 Android 应用程序会定期崩溃?

android - 来自 CWAC 相机的回调

rx-java - 关闭sqlite数据库时死锁

android - CWAC-Camera getVideoFilename 返回无效的文件名

java - Android Studio仅支持一个settings.gradle吗?

java - CWAC-相机在触摸屏上拍照

java - Android:BroadcastReceiver 或 Wakefulintentservice 应该以 finish() 结束吗?