刚刚步入Android开发领域的时候,一次一个项目需要用一个GridView实现多种不同的布局样式效果,当时看到这个需求直接就懵了,感觉挺不可思议的想法。下面就来看下这个多重布局在同一个GridView中如何实现,实现是一种方式,具体的到项目中要做适应性修改。
效果图如下:
具体UI的只是变换了下不同的位置,有兴趣的同行可以自己去调试。
1、activity_more_type_grid_view.xml
2、Item第一种样式布局:adapter_grid_item_one.xml
3、Item第二种样式布局:adapter_grid_item_two.xml
4、Item第三种样式布局:adapter_grid_item_three.xml
5、Item第四种样式布局:adapter_grid_item_four.xml
样式基本相同,就是TextView与ImageView所处的位置关系变化了。
6、为每种模式定义一个常量
public class Constants {
public static final int TYPENUM_CONTENT = 5;
public static final int NUMFIRST = 1;
public static final int NUMSECONd = 2;
public static final int NUMTHREE = 3;
public static final int NUMFOURTH = 4;
}
常量TYPENUM_CONTENT是定义总共有多少种样式布局,这个值要在总体样式布局的基础上加一。
7、Activity界面效果实现:
public class MoreTypeGridView extends Activity {
private GridView gv;
private MoreTypeGridAdapter adapter;
private List
8、适配器Adapter:
public class MoreTypeGridAdapter extends BaseAdapter {
private Context con;
private List> list;
private LayoutInflater inflater;
public MoreTypeGridAdapter(Context context,List> list){
this.con = context;
this.list = list;
inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getItemViewType(int position) {
return Integer.parseInt(list.get(position).get("Type").toString());
}
@Override
public int getViewTypeCount() {
return Constants.TYPENUM_CONTENT;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
if(position>=getCount()){
return null;
}
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
viewHolder vh = null;
Map map = list.get(arg0);
if(convertView == null){
vh = new viewHolder();
switch(Integer.parseInt(map.get("Type").toString())){
case Constants.NUMFIRST:
convertView = inflater.inflate(R.layout.adapter_grid_item_one, null);
vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemOneText);
vh.iv = (ImageView) convertView.findViewById(R.id.AdapterGridItemOneImage);
break;
case Constants.NUMSECONd:
convertView = inflater.inflate(R.layout.adapter_grid_item_two, null);
vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemTwoText);
vh.iv = (ImageView) convertView.findViewById(R.id.AdapterGridItemTwoImage);
break;
case Constants.NUMTHREE:
convertView = inflater.inflate(R.layout.adapter_grid_item_three, null);
vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemThreeText);
vh.iv = (ImageView) convertView.findViewById(R.id.AdapterGridItemThreeImage);
break;
case Constants.NUMFOURTH:
convertView = inflater.inflate(R.layout.adapter_grid_item_four, null);
vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemFourText);
vh.iv = (ImageView) convertView.findViewById(R.id.AdapterGridItemFourImage);
break;
}
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
getDipConversionPx(con,180));
convertView.setLayoutParams(lp);
convertView.setTag(vh);
}else{
vh = (viewHolder) convertView.getTag();
}
vh.tv.setText(map.get("text").toString());
vh.iv.setImageResource(Integer.parseInt(map.get("image").toString()));
return convertView;
}
public class viewHolder{
public TextView tv;
public ImageView iv;
}
public int getDipConversionPx(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
总体上来说没有多少技术难点,基本就是API多读两遍,里面很多出人意料的方法,学习android还是比较建议多去外网转转,看不看得懂无所谓,先混个脸熟嘛!