ListView多條目展示

ListView多條目展示非常的實用如果你不想單調,如果你想DIY出自己的界面,那麼請看。

ListView中的靈魂便是 適配器(佈局很簡單我就不多寫瞭):

——–在適配器中設置兩種不同的情況(也可以多設個)——–

private  final int  type_1=0;
private  final int  type_2=1;

-----通常適配器重寫四種方法,現在又多瞭兩個-----
public int getItemViewType(int position) {
    int p = position % 2;
    if (p == 0) {
        return type_1;
    } else if (p < 3) {
        return type_2;
    }
    return 0;
}

@Override
public int getViewTypeCount() {
    return 2;
}
-----------這是兩個佈局的寫法(以此類推)---------我用的imageloader來顯示圖片大傢也可以用別的好方法---------------------------------
ViewHoudler1 houdler1=null; ViewHoudler2 houdler2=null; public View getView(int position, View convertView, ViewGroup viewGroup) { houdler1 = new ViewHoudler1(); houdler2 = new ViewHoudler2(); int type = getItemViewType(position); if(convertView==null){ switch (type){ case type_1: convertView = View.inflate(context,R.layout.item1,null); houdler1.tv1 = (TextView) convertView.findViewById(R.id.tv1); houdler1.iv = (ImageView) convertView.findViewById(R.id.iv); convertView.setTag(houdler1); break; case type_2: convertView = View.inflate(context,R.layout.item2,null); houdler2.tv2 = (TextView) convertView.findViewById(R.id.tv2); convertView.setTag(houdler2); break; } } switch(type) { case type_1: houdler1= (ViewHoudler1) convertView.getTag(); houdler1.tv1.setText(lists.get(position).getName()); break; case type_2: houdler2= (ViewHoudler2) convertView.getTag(); houdler2.tv2.setText(lists.get(position).getCategoryName()); break; } if (type==type_1){ ImageLoaderConfiguration loader = ImageLoaderConfiguration.createDefault(context); ImageLoader instance = ImageLoader.getInstance(); instance.init(loader); DisplayImageOptions os=new DisplayImageOptions.Builder().build(); instance.displayImage(lists.get(position).getIconUrl(),houdler1.iv,os); } return convertView; } class ViewHoudler1{ private TextView tv1; private ImageView iv; } class ViewHoudler2{ private TextView tv2; }

————–在MainActivity中(本題是帶Gson的所以要用gson解析)——————————————————————–

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);

    Gson gson = new Gson();
    Mybean mybean = gson.fromJson(s, Mybean.class);//Mybean是Bean類
    List apk = mybean.getApk();
    Mybase base = new Mybase(MainActivity.this, apk);
    lv.setAdapter(base);
}
----效果圖--------------------------------------------------------------------------------------------------------------------------

You May Also Like