将Gallery嵌入ListView?

时间:2012-04-13 05:16:46

标签: android listview android-gallery

有人可以告诉我如何将Gallery嵌入ListVview,以便我可以对ListView内的图片进行水平滚动吗?

2 个答案:

答案 0 :(得分:3)

查看this帖子,它对我有用。

我的设置如下:

  • 我有ListActivity使用ArrayAdapter填充数据。
  • 我的XML资源list_item包含ImageView,TextView和Gallery。
  • 我在行中添加OnClickListener(正如您通常处理列表商品点击一样。)
  • 然后我为Gallery
  • 设置了适配器
  • 我将GestureListener添加到处理滑动的图库
  • 我在图库中添加了OnItemClickListener来处理图库中的点击图片

在我的ArrayAdapter中,我执行以下操作:

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

    }

}

答案 1 :(得分:0)

您需要Listview吗?也许你可以在ScrollView中放置一个LinearLayout(垂直)?