0

Can anybody tell me how to embed a Gallery into a ListVview, so that I can do horizontal scrolling over images inside ListView?

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
KKC
  • 538
  • 1
  • 11
  • 27

2 Answers2

3

Check this post, it worked for me.

My setup is as follows:

  • I have a ListActivity that uses an ArrayAdapter to populate it with data.
  • My XML resource list_item contains an ImageView, TextView and a Gallery.
  • I add an OnClickListener to the row (as you normally would to handle list item clicks.)
  • Then I set the adapter for the Gallery
  • I add a GestureListener to the gallery that handles swiping
  • I add a OnItemClickListener to the gallery to handle clicking images in the gallery

In my ArrayAdapter I do the following:

private static final int SWIPE_MAX_OFF_PATH = 250;
private GestureDetector gestureDetector;
private OnTouchListener gestureListener;
private Gallery picGallery;

...

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;

    /* Holder pattern here, commented out for clarity */

    // We need to set the onClickListener here to make sure that
    // the row can also be clicked, in addition to the gallery photos
    row.setOnClickListener(new MyOnClickListener(context,position));

    // Set the adapter for the gallery
    picGallery = (Gallery) row.findViewById(R.id.gallery);
    picGallery.setAdapter(
               new MyGalleryAdapter(/* some input data here to populate the gallery */));

    // GestureDetector to detect swipes on the gallery
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    // Detect clicking an image
    picGallery.setOnItemClickListener(new MyOnItemClickListener(context));

    // Detect swipes
    picGallery.setOnTouchListener(gestureListener);

    return row;
 }

...

private class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

}

...

private class MyOnItemClickListener implements OnItemClickListener{    
    private Context context;

    public MyOnItemClickListener(Context context){
        this.context = context;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Intent intent = new Intent(context, PhotoDetailActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);

    }

}
Community
  • 1
  • 1
0

Do you require a Listview? Perhaps you could place a LinearLayout (vertical) inside a ScrollView?

Scott Naef
  • 165
  • 3
  • 9
  • I tried to use linear layout and scroll view,but its not useful for my requirement...actually i am taking data from feed,so mine every thing is dynamic. – KKC Apr 13 '12 at 08:46
  • I have never had much success using ListView for complex layout requirements. I find that using a ScrollView and LinearLayout to hold my data is more flexible. Using this approach you cant use Adapters like you normally would but once you implement your own data handling code layout issues become easier to deal with. – Scott Naef Apr 13 '12 at 17:33
  • Thank you for your suggestion what ever u told i am completely agree with you..but as my data are dynamic i don't know how much data will come..so i thought about listview,anyways will you give me any other solution to do the same. – KKC Apr 16 '12 at 06:30