如何让安卓GridView中每个组件既包括图片,也包括textview和button?
6个回答
展开全部
自定义gridview
1
2
3
4
5
6
static class ViewHolder
{
public ImageView image;
public TextView title;
public TextView time;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridItemAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List<GridItem> gridItemList;
public GridItemAdapter(String[] titles, int[] images,String[] description, Context context)
{
super();
gridItemList = new ArrayList<GridItem>();
inflater = LayoutInflater.from(context);
for (int i = 0; i < images.length; i++)
{
GridItem picture = new GridItem(titles[i], images[i],description[i]);
gridItemList.add(picture);
}
}
@Override
public int getCount( )
{
if (null != gridItemList)
{
return gridItemList.size();
}
else
{
return 0;
}
}
@Override
public Object getItem( int position )
{
return gridItemList.get(position);
}
@Override
public long getItemId( int position )
{
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
ViewHolder viewHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.grid_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.time = (TextView) convertView.findViewById(R.id.description);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(gridItemList.get(position).getTitle());
viewHolder.time.setText(gridItemList.get(position).getTime());
viewHolder.image.setImageResource(gridItemList.get(position).getImageId());
return convertView;
}
然后调用
1
2
3
gridView = (GridView) findViewById(R.id.gridview);
GridItemAdapter adapter = new GridItemAdapter(titles, images,description,this);
gridView.setAdapter(adapter);
1
2
3
4
5
6
static class ViewHolder
{
public ImageView image;
public TextView title;
public TextView time;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridItemAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List<GridItem> gridItemList;
public GridItemAdapter(String[] titles, int[] images,String[] description, Context context)
{
super();
gridItemList = new ArrayList<GridItem>();
inflater = LayoutInflater.from(context);
for (int i = 0; i < images.length; i++)
{
GridItem picture = new GridItem(titles[i], images[i],description[i]);
gridItemList.add(picture);
}
}
@Override
public int getCount( )
{
if (null != gridItemList)
{
return gridItemList.size();
}
else
{
return 0;
}
}
@Override
public Object getItem( int position )
{
return gridItemList.get(position);
}
@Override
public long getItemId( int position )
{
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
ViewHolder viewHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.grid_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.time = (TextView) convertView.findViewById(R.id.description);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(gridItemList.get(position).getTitle());
viewHolder.time.setText(gridItemList.get(position).getTime());
viewHolder.image.setImageResource(gridItemList.get(position).getImageId());
return convertView;
}
然后调用
1
2
3
gridView = (GridView) findViewById(R.id.gridview);
GridItemAdapter adapter = new GridItemAdapter(titles, images,description,this);
gridView.setAdapter(adapter);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询