有人可以告诉我如何将Gallery
嵌入ListVview
,以便我可以对ListView
内的图片进行水平滚动吗?
答案 0 :(得分:3)
查看this帖子,它对我有用。
我的设置如下:
ListActivity
使用ArrayAdapter
填充数据。list_item
包含ImageView,TextView和Gallery。OnClickListener
(正如您通常处理列表商品点击一样。)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(垂直)?