在ListView中自定义行与自定义适配器 [英] Custom rows in ListView with custom Adapter

查看:185
本文介绍了在ListView中自定义行与自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下适配器,我以前用来创建属于同一所有的时间行。我删除了textviews和imageviews的创建。

我想实现的是创造取决于键不同的行。
一排可以包含文本和图像,而另一行只有文字。我怎么能这样做呢?

 公共类DetailsListAdapter扩展ArrayAdapter< ArrayList的<串GT;> {
    私人上下文的背景下;
    私人的ArrayList<串GT;键;    公共DetailsListAdapter(上下文的背景下,ArrayList的<串GT;键){
        超(背景下,R.layout.details);
        this.context =背景;
        this.keys =键;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        视图V = inflater.inflate(R.layout.details,NULL);
        返回伏;
    }    @覆盖
    公众诠释的getCount(){
        返回keys.size();
    }
}


解决方案

要为你需要重写行膨胀不同的布局 getViewItemType getViewTypeCount

您应该看看在链接的视频。

http://www.youtube.com/watch?v=wDBM6wVEO70

 私有静态最终诠释TYPE_ITEM1 = 0;
私有静态最终诠释TYPE_ITEM2 = 1;
私有静态最终诠释TYPE_ITEM3 = 2;

然后

  int型的;
@覆盖
公众诠释getItemViewType(INT位置){    如果(位置== 0){
        键入= TYPE_ITEM1;
    }否则如果(位置== 1){
        键入= TYPE_ITEM2;
    }
    其他
    {
         键入= TYPE_ITEM3;
    }
    返回类型;
} @覆盖
 公众诠释getViewTypeCount(){
        返回3;
 }
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
LayoutInflater吹气= NULL;
整型= getItemViewType(位置);
  //而不是其他,如果你可以使用情况
   如果(convertView == NULL){
    如果(类型== {TYPE_ITEM1
            // TYPE1的infalte布局
           convertView = mInflater.inflate(R.layout.layouttype1,
                         父母,假);
      }
    如果(类型== TYPE_ITEM2){
            // TYPE2的infalte布局
           convertView = mInflater.inflate(R.layout.layouttype2,
                         父母,假);
    }其他{
            // normaltype的infalte布局
            convertView = mInflater.inflate(R.layout.layouttype3,
                         父母,假);
 }
 ... //在code休息
    返回convertView;
}

I made the following adapter which I used before to create rows that are the same all the time. I removed the creation of the textviews and imageviews.

What I want to achieve is to create different rows depending on the key. A row could contain text and an image while another row only has text. How would I be able to do that?

public class DetailsListAdapter extends ArrayAdapter<ArrayList<String>> {
    private Context context;
    private ArrayList<String> keys;

    public DetailsListAdapter(Context context, ArrayList<String> keys) {
        super(context,R.layout.details); 
        this.context = context;
        this.keys = keys;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.details, null);      
        return v;
    }

    @Override
    public int getCount(){
        return keys.size();
    }
}

解决方案

To inflate different layout for rows you need to override getViewItemType and getViewTypeCount.

You should have a look at the video in the link.

http://www.youtube.com/watch?v=wDBM6wVEO70

private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2; 

Then

int type;
@Override
public int getItemViewType(int position) {

    if (position== 0){
        type = TYPE_ITEM1;
    } else if  (position == 1){
        type = TYPE_ITEM2;
    }
    else
    {
         type= TYPE_ITEM3 ;
    }
    return type;
}

 @Override
 public int getViewTypeCount() {
        return 3; 
 }
@Override  
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
  // instead of if else you can use a case
   if (convertView == null) {
    if (type == TYPE_ITEM1 {
            //infalte layout of type1
           convertView = mInflater.inflate(R.layout.layouttype1, 
                         parent, false);
      }
    if (type == TYPE_ITEM2) {
            //infalte layout of type2
           convertView = mInflater.inflate(R.layout.layouttype2, 
                         parent, false);
    }  else {
            //infalte layout of normaltype
            convertView = mInflater.inflate(R.layout.layouttype3, 
                         parent, false);
 }
 ...// rest of the code
    return convertView;
} 

这篇关于在ListView中自定义行与自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆