Horizontal scrolling in a ListView?

I have a ListView element layout that uses a HorizontalScrollView in the center. I used the android attribute " android:descendantFocusability="blocksDescendants" " in the parent LinearLayout so that ListView items can still be selected.

The problem I'm having is that when I click on the ListView part, which is the HorizontalScrollView , the event event of the ListView element is not raised.

How can I get a click event for a HorizontalScrollView to trigger the event event of a ListView list item?

+6
source share
2 answers

HorizontalScrollView does not have "onClick ()" , see this http://developer.android.com/reference/android/widget/HorizontalScrollView.html

It supports gestures and has "onTouchEvent (MotionEvent ev)"

So you can use it as a click. See the next demo that I prepared.

 // Followin code will not work for HorizontalScrollView /*hsv1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(HorizontalListActivity.this, tvMiddle.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } });*/ hsv1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Toast.makeText(YourActivity.this, "Your Msg", Toast.LENGTH_SHORT).show(); return false; } }); 
+1
source

Adding the boolean variables touchDown and touchUp to the adapter class is as follows:

 private class MyListAdapter extends ArrayAdapter<MyObject>{ ... //touch down + touch up with no other motion events in between = click boolean touchDown = false; boolean touchUp = false; private int iHostViewID; ... public MyListAdapter(Context context,int viewResourceId, List<MyObject> objects) { super(context, textViewResourceId, objects); iHostViewID = viewResourceId; } @Override public View getView(int pos, View convertView, ViewGroup parent){ View itemView = convertView; //iff we cannot re-use a view if(itemView == null){ LayoutInflater inflater = ( (LayoutInflater)hContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); itemView = inflater.inflate(iHostViewID, null); } final View hItemView = itemView; final int hPosition = pos; ... final HorizontalScrollView textDataSV = (HorizontalScrollView)itemView.findViewById(R.id.widget_hsv); textDataSV.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ touchDown = true; } else if(event.getAction() == MotionEvent.ACTION_UP){ touchUp = true; } else{ touchDown = false; touchUp = false; } if(touchDown && touchUp){ //click //mMyListView is the reference to the list view //instantiated in the view controller class responsible //for setting this adapter class as the list view adapter mMyListView.performItemClick(hItemView, hPosition, hItemView.getId()); } return false; } }); } } 

This is far from ideal, but should work for most standard use cases.

0
source

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


All Articles