ListView with horizontalScrollView OnItemClick not working

So very quickly I have a listView with a custom adapter, and it inflates the view that contains the horizontalScrollView, as well as the text view, etc. I have a problem: when I try to connect a listener to this list, it is not receiving any callbacks.

I believe the problem is with the fact that my list item contains a scroll that captures click events (although I thought it should only capture other gestures).

code ... (my xml list item)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearMain" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/grey" > <TextView android:id="@+id/headerName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:padding="4dp" android:textColor="@color/text_header" android:textStyle="bold" android:text="TextView" /> </RelativeLayout> <View android:background="@color/border" android:layout_width="fill_parent" android:layout_height="1px" /> <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_width="fill_parent" android:focusable="false" android:focusableInTouchMode="false" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/linearImages" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="4dp"> </LinearLayout> </HorizontalScrollView> <View android:background="@color/border" android:layout_width="fill_parent" android:layout_height="1px" /> </LinearLayout> 

and then in my onCreate ...

 lv.setAdapter(myobj.adapter); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Log.w("dsdsds", "sdsdsds"); }}); 

Any help or suggestions would be greatly appreciated.

+6
source share
4 answers

Add this to the top layout:

android:descendantFocusability="blocksDescendants"

It helped, now onItemClickListener is fired.

+2
source

You can call setOnClickListener for linearMain in the getView () method of your adapter.

+1
source

for ScrollView you should use OnTouchListener, for example:

 private class ScrollViewOnTouchListener implements OnTouchListener{ HorizontalScrollView view; float historicX = Float.NaN; static final int DELTA_ON_CLICK = 20; long historicTime; int position; public ScrollViewOnTouchListener(HorizontalScrollView view, int position){ this.view = view; this.position = position; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: historicX = event.getX(); historicTime = System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: if (Math.abs(event.getX() - historicX) < DELTA_ON_CLICK) // seems like onclick // do what you want for onClick break; default: return false; } return true; } } 

and in the getView method of the getView adapter:

 public View getView(int position, View convertView, ViewGroup parent) { ... HorizontalScrollView scrollItem; scrollItem =( HorizontalScrollView )itemView.findViewById(R.id.item_scroll_view); scrollItem .setOnTouchListener(new ScrollViewOnTouchListener(scrollItem , position)); } 
+1
source

Inside list_item.xml, mark your layout with some id:

 <?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> ... 

Open your user adapter, in getView(...) write:

 layout = view.findViewById(R.id.layout); layout.setTag(position); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (callback != null) { callback.showItem(getItem((int) v.getTag())); } } }); 

Write below in the adapter:

 public interface OnClickInterface { void showItem(YourClass item); } 

And in the adapter constructor, assign a callback:

 public CustomAdapter(Context context, List<YourClass> list, OnClickInterface callback, int resId) { this.callback = callback; ... 

In your activity / fragment that contains the ListView, create this adapter:

 CustomAdapter.OnClickInterface callback = new CustomAdapter.OnClickInterface() { @Override public void showItem(YourClass item) { // Do something with an item. } } adapter = new Customdapter(getActivity(), new ArrayList<YourClass>(), callback, R.layout.list_item); 
0
source

Source: https://habr.com/ru/post/907126/


All Articles