2

In the ListView onItemLongClick is getting Triggered but onItemClick is not getting triggered. I want both onItemClick and onItemLongClick to trigger.

onItemClick will call item detail activity

onItemLongClick will favorite the item

Here is my code:

public class myClass extends ListFragment implements OnItemClickListener ,OnItemLongClickListener


public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //System.out.println("<<<<<<<<<<<<<<<<<<<<Boy in onCreateView>>>>>>>>>>>>>>>>>>>>");

        View v = inflater.inflate(R.layout.boy, container, false);
        lvBNames = (ListView) v.findViewById(android.R.id.list);


        searchView = (SearchView) v.findViewById(R.id.action_search);

        if (searchView == null )
        {
            //System.out.println("Unable to instantiate");
        }



        pop_data();
        adapter = new ListBNamesAdapter(getActivity(),mBNamesList);
        lvBNames.setSmoothScrollbarEnabled(false); //android:smoothScrollbar="true"
        lvBNames.setAdapter(adapter);
        //lvBNames.setOnItemClickListener(this);
        //lvBNames.setOnItemLongClickListener(this);

        //////////////////////////////////////// Test 28 Feb 17
        /// onItemClick is not triggering ............!
        lvBNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(activity, "Click", Toast.LENGTH_SHORT).show();
            }
        });


/// setOnItemLongClickListener is triggering ............! :)
            lvBNames.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                    Toast.makeText(activity, "Long Click", Toast.LENGTH_SHORT).show();
                    return true;
                }
            });

            lvBNames.setTextFilterEnabled(true);

            return v;
        }

Any Sample program will be appreciated

Carl Poole
  • 1,970
  • 2
  • 23
  • 28
Kaleel
  • 43
  • 7
  • 2
    Can you show us your adapter? Maybe You have `onClick` on some view in adapter and it cannot dispatch onclick to the next views – Michael Feb 28 '17 at 20:37
  • I don't understand why you're implementing OnItemClickListener and not adding your code inside your overriden function. – Maxim Kopecki Feb 28 '17 at 20:40
  • planning to implement code in both event different action, ut only long click event is getting triggered – Kaleel Feb 28 '17 at 21:17

2 Answers2

0

If you are using any layout inside the list view then use ...

android:descendantFocusability="blocksDescendants"  

Must be applied to first parent in the list. It will render click to the list items.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • I added to the item_list.xml , not working only long click triggering, even commented LongclickListener, single click is not triggering :-( – Kaleel Feb 28 '17 at 21:06
0

try to return false in onItemLongClick instead of true

Hikmat G.
  • 2,591
  • 1
  • 20
  • 40
  • This is an issue only working in one way. If you LongClick something you're triggering the normal click but if you click something you're not triggering the long click. – Maxim Kopecki Feb 28 '17 at 20:42