Android中如何实现GridView的水平拖动,固定行数,有点类似Gallery的效果!
有点像实现现有GridView相反的效果,比如固定了是现实3行,然后实现列的水平滚动!
不知道是不是用GridView实现。最好能有例子代码! --------------------编程问答-------------------- --------------------编程问答-------------------- 没做过,不过能提供个思路说不定能用到。
方法1.得到数据后计算列数,动态指定列数,行数就定了。
方法2.试试用TableLayout
--------------------编程问答-------------------- 自定义一个view --------------------编程问答-------------------- 用Gallery加ImageSwitecher来实现
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_1);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(g);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(R.string.gallery_2_text);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
return true;
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
// The preferred Gallery item background
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
private Context mContext;
private Integer[] mImageIds = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8
};
} --------------------编程问答-------------------- 楼上我用了你的代码,在我这只能实现单行的。
还有不知道为什么
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
这几句代码报错。。 --------------------编程问答-------------------- 楼主,你的问题最后解决了吗?我现在也碰到了这个问题,解决了能否分享下代码呢? --------------------编程问答-------------------- 楼主的问题解决没有,现在也有这样的需求,解决了能不能共享一下代码? --------------------编程问答-------------------- 解决了不告诉我们。不厚道啊。。。怎么解决。。
补充:移动开发 , Android