Android 安卓如何实现类似谷歌play store的卡片界面
我希望实现与以下类似的UI: 我已经创建了一个drawable,在我的列表项XML文件中用作背景。然而,在这里,我相信他们使用的是Android 安卓如何实现类似谷歌play store的卡片界面,android,android-gridview,Android,Android Gridview,我希望实现与以下类似的UI: 我已经创建了一个drawable,在我的列表项XML文件中用作背景。然而,在这里,我相信他们使用的是GridView。我如何推断我在ListView中所做的工作,从而在GridView中实现同样的效果 下面是gridview适配器的getView() public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated metho
GridView
。我如何推断我在ListView
中所做的工作,从而在GridView
中实现同样的效果
下面是gridview适配器的getView()
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv;
if (convertView == null) { // if it's not recycled, initialize some attributes
iv = new ImageView(ctx);
iv.setLayoutParams(new GridView.LayoutParams(350,350));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
} else {
iv = (ImageView) convertView;
}
iv.setImageResource(pics[position]);
return iv;
}
ListView
和GridView
在幕后基本相同。它们都继承自AbsListView
,因此都有一个getView
方法
查看本教程,了解如何实现GridView
:
编辑:
在getView
方法中,您可以将物品充气如下:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView;
if (convertView == null) {
inflatedView = new View(context);
inflatedView = inflater.inflate(R.layout.mylistitem, null);
} else {
inflatedView = (View) convertView;
}
return inflatedView;
}
这与您在
列表视图中的操作没有什么不同,您需要做的是在自定义适配器的getView()中为不同的布局文件充气。我不明白。您能给我一些如何操作的代码示例/教程吗?有关教程的链接,请参阅下面的答案。另一个问题:但在gridview中,是否有类似于listview中的“列表项”的东西,我可以修改它?是的,您可以使用与listview
项相同的XML布局,但是布局的宽度应该设置为wrap\u content
,而不是match\u parent
或fill\u parent
@Darwind不需要将布局的宽度设置为wrap\u content
。是的,那么如何将特定的XML项文件链接到gridview。当前我的gridview适配器扩展了baseadapter,而listview适配器扩展了ArrayAdapter,我在其中创建了“级别”。我需要在那里做任何更改吗?@vikram不,您也可以将其设置为特定的宽度;-)