dynamically add layout into getview method of adaptor in android(在android中将布局动态添加到适配器的getview方法中)
问题描述
I want to display N number of imageview in every row of list view. Number of imageview depends on json parsing value. it can be 2 or 3 or 4 everytime when i fetch json from server.
so I can not use static xml by using inflate.so i decided to create dynamic view in getview method and add N number of imageview into view
I write following code but it still display only one imageview and horizontal scroll wont work at all..
any help is appreciated
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;
}
}
Check this is what you want I guess. Just set random value for each line in images
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中将布局动态添加到适配器的getview方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在android中将布局动态添加到适配器的getview方法中
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01