2

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!

Ashok
  • 839
  • 10
  • 21
  • 1
    Looking for [ContextMenu](http://developer.android.com/reference/android/view/ContextMenu.html) **(0_o)** – nobalG Nov 14 '14 at 07:04

5 Answers5

1

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.

Community
  • 1
  • 1
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
  • whether MotionEvent.ACION_DOWN is equal to longpress of anitem in the listview.....or whethers MotionEvent is works fine for customized listview ah – Ashok Nov 14 '14 at 07:52
  • i am having two classes MainActivity contains listview xml page and adapter class which maintains the row items in the listview then where i need to changes in mainactivity or adapater class. – Ashok Nov 14 '14 at 08:16
  • i was implement OnTouchListener which overrides only one method onTouch(View arg0, MotionEvent arg1) – Ashok Nov 14 '14 at 08:17
  • i am not getting onTouchEvent() – Ashok Nov 14 '14 at 08:18
  • u shud implement it for ur listview..i ll edit my ans. – M S Gadag Nov 14 '14 at 08:28
  • kk i am waiting for your reply. please clearly where i want implements listener in MainActivity class or Adapter class. – Ashok Nov 14 '14 at 08:37
  • u have to implemet in ur mainactivty where your listview is located.. ex if u want to settext for perticular position use like this `TextView t= (TextView)arg1.findViewById(R.id.your_adapter_textview); t.setText("hi");` in switch. – M S Gadag Nov 14 '14 at 08:43
  • thanks alot ...Finally i got the answer what i expected – Ashok Nov 14 '14 at 10:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64986/discussion-between-user3208981-and-m-s-gadag). – Ashok Nov 15 '14 at 07:56
  • First of all i am using customized Listview...i separtely write code setOnItemClickListener() for listview and setOnItemLongClickListener() for child RelativeLayout in rowvalue.xml file for the listview. Now current what happening when i click or longpress only setOnItemLongClickListener only working ...but whatever i write in setOnItemClickListener() is not working when i click on item in the listview ,setOnItemClickListener() is not working – Ashok Nov 15 '14 at 09:40
  • what u want to do in `setOnItemClickListener()` write that code in ACTION_UP in `setOnTouchListener()` and try i hope it will work as ur requirement. – M S Gadag Nov 15 '14 at 10:09
0

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);
Kirill Volkov
  • 942
  • 1
  • 9
  • 18
  • What i want is when user long pressed any particular item, on that time only i want hide the particular layout or view and display other view or layout ....without listener how do i know that user is long pressing on particular item – Ashok Nov 14 '14 at 07:18
  • use OnItemLongClickListener() ;) – Kirill Volkov Nov 14 '14 at 07:26
  • i already tried this i can able to hide the particular item view based on second parameter in onItemLongClick(),but i cannot able to display a new view to that particular item. – Ashok Nov 14 '14 at 07:33
  • You dont need new view. Just switch views inside of FrameLayout with the code above. – Kirill Volkov Nov 16 '14 at 10:07
0

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;
        }
  }); 
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
  • i already tried this i can able to hide the particular item view based on second parameter in onItemLongClick(),but i cannot able to display a new view to that particular item. – Ashok Nov 14 '14 at 07:26
  • you have to define your list item view inside a frame layout and define your long clicked view with visibility:gone there. then in onItemLongClick function do thid : view.findViewById(R.id.normalListItemView).setVisibility(View.GONE); view.findViewById(R.id.longClickedView).setVisibility(View.VISIBLE); works like charm! ;) – Mohammad Rahchamani Nov 14 '14 at 11:35
0

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.

Shubhang Malviya
  • 1,525
  • 11
  • 17
  • i am having two classes MainActivity contains listview xml page and adapter class which maintains the row items in the listview then where i need to changes in mainactivity or adapater class – Ashok Nov 14 '14 at 07:48
0

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;
            }
        });
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36