android 动态添加服务器上的布局,在android中动态地将布局添加到适配器的getview方法中...

我想在列表视图的每一行显示N个imageview.

imageview的数量取决于json解析值.每次从服务器获取json时,它可以是2或3或4.

所以我不能通过使用inflate使用静态xml.所以我决定在getview方法中创建动态视图并在视图中添加N个imageview

我写下面的代码,但它仍然只显示一个imageview和水平滚动根本不工作..

任何帮助表示赞赏

public class MyAdapter extends BaseAdapter{

private LayoutInflater inflater;

private ArrayList data;

Context con;

public MyAdapter(Context context, ArrayList data1){

// Caches the LayoutInflater for quicker use

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// Sets the events data

data= data1;

con=context;

}

public View getView(int position, View view, ViewGroup viewgroup) {

ViewHolder holder=new ViewHolder(); //our view holder of the row

if (view == null) {

HorizontalScrollView hr=new HorizontalScrollView(con);

LinearLayout layout=new LinearLayout(con);

layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

//layout set some properties

for(int i=1;i<2;i++)

{

holder.image =new ImageView(con);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100*i, 100*i);

holder.image.setLayoutParams(layoutParams);

layout.addView(holder.image);

}

//subtitle set some properties

//CREATING THE LAYOUT THROUGH CODE

hr.addView(layout);

view = hr; //INSTEAD OF INFLATING A LAYOUT FOR THE ROW I JUST BINDED IT TO THE RECENTLY CREATED LAYOUT

//bind the views of the holder to the views of the layout

view.setTag(holder);

Log.w("myapp", "new view");

}

else

{

holder = (ViewHolder) view.getTag();

Log.w("myapp", "in reuse");

}

//rest of implementation of the View

for(int i=0;i<2;i++)

{

holder.image.setImageResource(R.drawable.ic_launcher);

}

return view;

}

static class ViewHolder {

ImageView image;

TextView title;

TextView type;

HorizontalScrollView hr;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return 4;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 1;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值