拖放事件在我的ListView项目中没有反应

时间:2014-10-06 10:28:43

标签: android android-listview drag-and-drop touch listviewitem

我已经搜索过,而且我没有找到类似的内容,所以如果这篇文章重复,请原谅。

我尝试通过拖动ListView项目

来实现一项菜单

即。这是我在拖动项目之前所拥有的 Image before gesture

如果我拖动“Schallermaier-b”项目,我将拥有这个。

“删除”图标,以便删除该项目。

我想说,click事件将用于其他目的,long click也是如此,所以我必须使用drag&drop event

It appears a delete icon

我已经实现了我的Adapter(内置了持有者),我可以将其设置为我的ListView而没有任何问题,但我无法连接onDragListener和我的ListView

这是我的适配器代码

package com.test.adapter;

...

public class SortAdapter extends ArrayAdapter<Sorting> {

private Context context;
private int resourceId;
private SortingHolder holder = null;
private Sorting[] data; 


public SortAdapter(Context context, int resourceId, Sorting[] data) {
    super(context, resourceId, data);

    this.context    = context;
    this.resourceId = resourceId;
    this.data       = data;


}

/*
 * we are overriding the getView method here - this is what defines how each
 * list item will look.
 */
public View getView(int position, View convertView, ViewGroup parent){

    // assign the view we are converting to a local variable
    View view            = convertView;
    holder               = null;

    // first check to see if the view is null. if so, we have to inflate it.
    // to inflate it basically means to render, or show, the view.
    if ( view == null ) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        view = inflater.inflate(resourceId, parent, false);

        //holder = new SortingHolder();
        holder.description = (TextView) view.findViewById(R.id.item_description);
        holder.image       = (ImageView) view.findViewById(R.id.img_view_waste);

        view.setTag(holder);

    } else {
        holder = (SortingHolder)view.getTag();
    }


    Sorting sorting = data[position];
    holder.description.setText(sorting.getSorting());


    // the view must be returned to our activity
    return view;

}

// Inner Class SortingHolder
static class SortingHolder  {
    TextView description;
    ImageView image;
}   

@Override
public int getCount() {
    return data.length;
}

@Override
public Sorting getItem(int location) {
    return data[location];
}

@Override
public long getItemId(int position) {
    return position;
}    

public void setItem(int position, Sorting sorting) {
    data[position] = sorting;
}

}

这是我的Activity package com.test.dialog; ...

public class CompletedTasksActivity extends Activity {

private PopupMenu popupMenu;

private List<String> list = new ArrayList<String>();

private List<String> void_list = new ArrayList<String>();
private List<String> null_th_list = new ArrayList<String>();
private List<String> first_list = new ArrayList<String>();
private List<String> second_list = new ArrayList<String>();
private List<String> third_list = new ArrayList<String>();

private ArrayAdapter array_adapter;
private ListView list_view;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.another_activity_main); 

    initializeLists();

    initializeSpinner();

    initializeListView();

}

private void initializeListView() {
    list_view     = (ListView)findViewById(R.id.listView1);
    array_adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, void_list){

        // Here we override "isEnabled" Method to disable Click on 0th position if it has no data
        public boolean isEnabled(int position){
            boolean result = true;
            String item_0 = (String)getItem(0);

            // Just disable click on 0th Item if it has no data 
            if ( item_0.equalsIgnoreCase("No Data") &&( position == 0 ))
                result = false;

            return result;
        }           
    };


    list_view.setAdapter(array_adapter);    


    // Click Section
    list_view.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
            Context context = getApplicationContext();
            Toast.makeText(context, "Item "+id+" clicked", Toast.LENGTH_SHORT).show();
            Log.i("TAG", "CompletedTasksActivity::initializeSorting::onItemClick: ");

        }
    });


    // Drag Section
    list_view.setOnDragListener(new OnDragListener(){

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;
                case DragEvent.ACTION_DRAG_ENTERED:

                break;
                case DragEvent.ACTION_DRAG_EXITED:        

                break;
                case DragEvent.ACTION_DROP:
                    // I try to do something here, it's not reached
                    // Toast is not be shown                    
                break;
                case DragEvent.ACTION_DRAG_ENDED:
                    // I try to do something here, it's also not reached
                    // Toast is not be shown    
                break;

            }
            return true;
        }

    });

}

private void initializeSpinner() {
    Spinner client_spinner = (Spinner)findViewById(R.id.client_spinner);

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    client_spinner.setAdapter(dataAdapter);     
    client_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub

            //array_adapter.clear();
            //Toast.makeText(getApplicationContext(), "id: "+id, Toast.LENGTH_SHORT);
            int myId = (int) id;
            switch (myId) {
                case 0: 
                    array_adapter.clear();
                    array_adapter.addAll(null_th_list);
                break;
                case 1:
                    array_adapter.clear();
                    array_adapter.addAll(first_list);
                break;
                case 2:                     
                    array_adapter.clear();
                    array_adapter.addAll(second_list);
                break;
                case 3:                     
                    array_adapter.clear();
                    array_adapter.addAll(third_list);
                break;

            }

            array_adapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }

    });
}

private void initializeLists(){
    list.add(" ");
    list.add("Hinterau");
    list.add("Musterunternehmen");
    list.add("Schallermaier");


    null_th_list.add("No Data");

    first_list.add("Hinterau 1");

    second_list.add("Musterunternehmen_1");
    second_list.add("Musterunternehmen_2");

    third_list.add("Schallermaier-a ");
    third_list.add("Schallermaier-b");      
    third_list.add("Schallermaier-c");

}

}

有人可以说我应该做什么,以便Drag&Drop事件做出反应吗?

非常感谢

0 个答案:

没有答案