0

This doesn't seem like a typical view layout which I'm having trouble with.

I have two listViews.

Each listView has touchListener
(whose purpose is to synchronize scrolling by calling dispatchTouchEvent() to another listView)

ListView also has onItemClickListener to handle clicks on the row of listView.

Everything works as intended up to here.

I'd like to add another clickListener to subview-group of the listView's row to handle click event on the subview.

After attaching this clickListener, I see listView's scroll doesn't always work.

I suspect its because the clickListener of this child view is inspecting touch events (to see if its indeed a click) before the parent(listView)'s touchListener.

I can think of two workarounds to this problem.

  1. attach touchListener instead of clickListener to child, and make it return false for all touch event except FINGER_UP event.
    on FINGER_UP event, I execute the method which I initially had in onClickListener

  2. public boolean onInterceptTouchEvent (MotionEvent ev)

    Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
    (ok... I'm confused, I thought touch events goes to child views first and propagate to parents if children don't handle the touches..)

.. How do I implement the method 1?
.. Please help me to figure out #2 as well and to grasp the touch delivery mechanism.

  • EDIT - This is where I add OnClickListener to my subview.

            viewHolder.layout_author.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent profileIntent = new Intent(ImageListAdapter.this.activity, ProfileActivity.class);
                        profileIntent.putExtra("JsonUser", jsonAlbumImage.jsonUser);
                        ImageListAdapter.this.activity.startActivity(profileIntent);
                    }
                });
    
eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

0

I'd like to add another clickListener to subview-group of the listView's row to handle click event on the subview.

You simply need to write a custom adapter and assign the OnClickListener in getView(). You can look at the question: Android GridView Button Click Handler for example code.

Also awhile back I answered Android. Scrolling 2 listviews together did you use a similar approach to synchronize your ListViews? When I combine both of the answers, my app functions the way you want. I hope this helps.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • thanks for the detailed answer. I replaced my code to follow your strategies, but it's having the same problem. row's onclicklistener is disturbing scroll of list-view(parent) – eugene Jan 03 '13 at 03:10
  • I think setting onClickListener on a subview of listview prevents the subview from passing touch event to the parent(list view) – eugene Jan 03 '13 at 04:19
  • Hmm, I didn't seem to have that problem. Post your code where you set up the OnClickListener so I can see what is happening. – Sam Jan 03 '13 at 04:41
  • Ok, I recreated what you are seeing. I tried this and that, soon my code seemed way too long and didn't really work yet... But I had the idea of faking to ListViews. Why not build a clever row layout that makes one ListView look like two? The ListViews will always be "synchronized" and you can set as many sub-group click listeners as you want. Right now, I think this approach is the simplest and best tactic. – Sam Jan 03 '13 at 20:41
  • actually my rows are not the same height. I'm trying to mimic the `pinterest` layout – eugene Jan 04 '13 at 01:35
  • Have you read this: [Android heterogeneous gridview like pinterest?](http://stackoverflow.com/q/11736658/1267661) (I haven't tried the answers out myself.) You can also search for "android pinterest" to dig up another few questions. – Sam Jan 05 '13 at 17:49
  • Yes, I got the idea of using two listViews from there. I tried other options as well. not easy! anyway Thanks! – eugene Jan 06 '13 at 04:41