动态添加布局到android中适配器的getview方法

时间:2013-06-28 11:44:38

标签: android imageview android-arrayadapter

我想在列表视图的每一行中显示N个imageview。 imageview的数量取决于json解析值。每当我从服务器获取json时,它可以是2或3或4。

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

我写下面的代码,但它仍然只显示一个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;
}
 }

3 个答案:

答案 0 :(得分:7)

检查这是你想要的。只需为图像中的每一行设置随机值

    public class MyAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private ArrayList<String> data;
    Context con;

    public MyAdapter(Context context, ArrayList<String> data1) {

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

        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));
            for (int i = 0; i < data.size(); i++) {
                holder.image = new ImageView(con);
                layout.addView(holder.image);
                holder.image.setImageResource(R.drawable.ic_launcher);
            }
            hr.addView(layout);
            view = hr;

            view.setTag(holder);

        }
        holder = (ViewHolder) view.getTag();

        return view;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @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;
    }
}

class ViewHolder {
    ImageView image;
    TextView title;
    TextView type;
    HorizontalScrollView hr;
}

答案 1 :(得分:2)

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

这意味着它只运行一次,因为i = 1,因为在下一个循环i == 2,因此i <1。 2假。

答案 2 :(得分:0)

嗯,就像你从xml布局中扩展视图项一样,你可以像往常一样以编程方式创建它们。只需要确保指定布局参数并将它们添加到布局中,以便它们可见:

例如,获取ViewHolder初始化链接的视图:

//That's a view taken from the xml
EditText editText = (EditText) view.findViewById(R.id.YOUR_EditText1);

同样,如果要在布局中动态添加视图,请使用getView()方法:

ImageView image1 = new ImageView();
...
...
view.addView(image1);

有关以编程方式添加视图的详情,请查看Android: Add two text views programmatically