Embedding a Gallery into a ListView?

谁都会走 提交于 2019-12-25 08:57:38

问题


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


回答1:


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);

    }

}



回答2:


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



来源:https://stackoverflow.com/questions/10135593/embedding-a-gallery-into-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!