如何在Gridview案例中使用setOnItemClickListener和setOnTouchListener?

时间:2015-02-17 11:57:12

标签: android ontouchlistener onitemclicklistener

我有一个GridView我希望在点击它时访问SD卡。我使用setOnItemClickListener来访问存储卡文件夹。我还想访问元素setOnTouchListener进行拖放。

grid1.setOnItemClickListener(new OnItemClickListener() 
            {
                public void onItemClick(TwoWayAdapterView parent, View v, int position, long id)
                {
                    File F_new=listFile[position].getAbsoluteFile();
                    System.out.println("path"+F_new);

                    makefolder_method(MainActivity.mcontext, F_new);
                    Current_path_vault=F_new;
                }
            });


 grid1.setOnTouchListener(new MyClickListener());

公共类MyClickListener实现了OnTouchListener {

// called when the item is long-clicked


@Override
public boolean onTouch(View view, MotionEvent event) {
    // create it from the object's tag
    float currentXPosition = event.getX();
    float currentYPosition = event.getY();
    int position = FindPeopleFragment_next.grid1.pointToPosition((int) currentXPosition, (int) currentYPosition);
    View v = FindPeopleFragment_next.grid1.getChildAt(position);

     ClipData.Item item = new ClipData.Item("" + position); // here you send any data you want, I sent position
     String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_HTML};
     ClipData data = new ClipData("" + position, mimeTypes, item);
     View.DragShadowBuilder shadow = new View.DragShadowBuilder(v);
     view.startDrag(data, shadow, null, 0);
     v.setVisibility(View.INVISIBLE);



            return false;

}   

}

1 个答案:

答案 0 :(得分:0)

false中返回setOnTouchListener

您在MyClickListener中重写的触摸事件应返回false,如:

@Override
public boolean onTouch(View v, MotionEvent event) {

         //Your work

         return false;
}