修改TouchList视图以从列表拖动到另一个视图(图像视图)

时间:2011-10-25 01:33:39

标签: android commonsware-cwac

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

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

但我担心的是,这是一个相当愚蠢的解决方案,并且容易出错。有没有更好的方法来确定用户何时通过目标视图区域发布?在哪些情况下我不能简单地寻找负数?有没有办法真正看到一组给定的X / Y坐标下面有什么视图?

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

enter image description here

1 个答案:

答案 0 :(得分:1)

这是我想出的解决方案。

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