我想在列表视图的每一行中显示 N 个图像视图。imageview 的数量取决于 json 解析值。每次我从服务器获取 json 时,它可以是 2 或 3 或 4。
所以我不能通过使用inflate来使用静态xml。所以我决定在getview方法中创建动态视图并将N个imageview添加到视图中
我编写了以下代码,但它仍然只显示一个图像视图并且水平滚动根本不起作用..
任何帮助表示赞赏
public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<String> data;
Context con;
public MyAdapter(Context context, ArrayList<String> 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;
}
}