How to Display a new view while long pressing an item in the ListView?

1.1k views Asked by At

I am having customized ListView which contains 30 items, when i long press the particular item i want to hide that particular item and display the some xml view in that particular item till upto long pressed and when i release my finger from that item it must show his old item (i.e. i want display previous item which was hide).

Can anyone suggest any ideas to done this task!

5

There are 5 answers

11
M S Gadag On BEST ANSWER

try this

reference:Detecting a long press with Android

    @Override
public boolean onTouchEvent(MotionEvent event, MapView mapView){
    if(event.getAction() == MotionEvent.ACTION_DOWN)
        //hide wat u want to hide
    if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
        //show want to show
    return super.onTouchEvent(event, mapView);
}

note:link may destroy so code is pasted. in your switch of onitemlongclick or onitemclick

    yourlistview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {

        switch (position) {

        case 0:

            final TextView t = (TextView)arg1.findViewById(R.id.tview_homeoptions);
            arg0.setOnTouchListener(new OnTouchListener() {//arg0 is the view of selected position

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN)
                        {t.setTextColor(Color.parseColor("#ff0000"));}
                    if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
                        {t.setText("up");}
                    return false;
                }
            });
            break;
        case 1:
            //todo
            break;
         case 2:
            //todo
            break;
        //so on

        default:
            break;
        }

hope it works.

0
Mohammad Rahchamani On

here is complete code for you : your listItem.xml :

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/longClickedView"
        android:text="long click"
        android:background="#99cc00"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
    <TextView
    android:id="@+id/normalView"
    android:text="normal"
    android:background="#cc0011"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</FrameLayout>

and in your activity/fragment :

list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                view.findViewById(R.id.normal).setVisibility(View.GONE);
                view.findViewById(R.id.longClicked).setVisibility(View.VISIBLE);
                return true;
            }
        });
4
Kirill Volkov On

You could do the following.

For a ListView item create a FrameLayout, which includes list item layout (layout1) and layout with the view that should be displayed upon a long press (layout2) and set it's visibility to gone.

No create a long press listener and make

layout1.setVisibility(View.GONE);
layout2.setVisibility(View.Visible);

and to revert back

layout1.setVisibility(View.Visible);
layout2.setVisibility(View.GONE);
2
Mohammad Rahchamani On

you have to do the following:

set longclick listener to list items and in onItemLongClick function change your view

  listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                int index, long arg3) {
            // TODO change you view at index
            return true;
        }
  }); 
1
Shubhang Malviya On

First use onTouch(View view, MotionEvent motionEvent) of OnTouchListener() via setOnTouchListener() on listview reference to get the MotionEvent.ACTION_UP and MotionEvent.ACTION_DOWN events (also use return false in that otherwise you would block the event flow for normal listview events).

Then You can include the view in xml layout for list item and use your adapter to set the clicked item position which will set hidden layout to visible and call notifyDataSetchanged(); on adapter reference,

Edit

Also you can use listview's setOnItemLongPressListener() to get long press event , and then capture ACTION_UP event.