gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:49 25 4
gpt4 key购买 nike

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

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

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

最佳答案

我猜这就是您想要的。只需为图像中的每一行设置随机值

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

关于android - 在android中动态地将布局添加到适配器的getview方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17363967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com