【问题标题】:Inflating multiple layouts in list view ?在列表视图中膨胀多个布局?
【发布时间】:2015-08-25 07:05:29
【问题描述】:

有一个带有 listView 的片段,应该如下膨胀: 一个具有不同布局(自定义行),位置两个(滑块)和位置 3(item_layout)已经经历了网络上的大量资源和堆栈溢出,这并没有解决我寻找示例或教程的目的这种。

【问题讨论】:

  • 创建一个自定义适配器并基于索引在getView()方法中膨胀视图。

标签: android android-fragments android-listview


【解决方案1】:

最好使用header而不是不同类型的项目,因为你不会回收前两行。

注意:首次引入时,该方法只能在之前调用 使用 setAdapter(ListAdapter) 设置适配器。

例子:

    ListView listView = view.findViewById(R.id.list);

    View header = LayoutInflater.from(getActivity()).inflate(R.layout.your_first_two_rows, listView, false);
    listView.addHeaderView(header);

    listView.setAdapter(new YourCustomAdapter(...));

【讨论】:

  • 你是个天才 :) 这就是我要找的东西
【解决方案2】:
private static final int TYPE_ITEM0 = 0;
private static final int TYPE_ITEM1 = 1;
private static final int TYPE_ITEM2 = 2; 


@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {

    if(getItem(position) instanceof Type1List){
        type = TYPE_ITEM0;
    }
    else if(getItem(position) instanceof BingImageBean){
        type = TYPE_ITEM1;
    }
    else if(getItem(position) instanceof InuvoSearchType3Bean){
        type = TYPE_ITEM2;
    }

    return type;
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final int viewType = getItemViewType(position);

    if(viewType == 0){

        // InuvoSearch Type 1

        InuvoSearchType_1_ViewHolder holder;
        if(convertView == null || (Integer)convertView.getTag(R.id.view_type) != 0){

            holder = new InuvoSearchType_1_ViewHolder();
            convertView = mInflater.inflate(R.layout.inuvosearch_list_type1,parent,false);

            holder.txtInuvoTitle        = (TextView) convertView.findViewById(R.id.txtInuvoTitle);


            convertView.setTag(holder);
            convertView.setTag(R.id.view_type, 0);
        }
        else {              
            holder = (InuvoSearchType_1_ViewHolder) convertView.getTag();               
        }


        return convertView;

    }
    else if(viewType == 1)
    {

        // Bing Related Images

        ImageViewHolder holder;

        if(convertView == null || (Integer)convertView.getTag(R.id.view_type) != 1){
            holder = new ImageViewHolder();
            convertView = mInflater.inflate(R.layout.imagesearch_list,parent,false);                

            holder.imageView1   = (ImageView)convertView.findViewById(R.id.image1);


            convertView.setTag(holder);
            convertView.setTag(R.id.view_type, 1);
        }
        else{
            holder = (ImageViewHolder) convertView.getTag();
        }


        return convertView;

    }
    else if(viewType ==2)
    {

        // InuvoSearch Type 3

        InuvoSearchType_3_ViewHolder holder;
        if(convertView == null || (Integer)convertView.getTag(R.id.view_type) != 2){

            holder = new InuvoSearchType_3_ViewHolder();
            convertView = mInflater.inflate(R.layout.inuvosearch_list_type3,parent,false);



            convertView.setTag(holder);
            convertView.setTag(R.id.view_type, 2);

        }
        else{
            holder = (InuvoSearchType_3_ViewHolder) convertView.getTag();
        }

        holder.baseLayout.setId(position);

        return convertView;

    }
    return null;

}

静态类 ImageViewHolder{

    //Bing Related Images Holder:

    private ImageView imageView1,imageView2,imageView3;
    private ProgressBar progressBariamgeView1,progressBariamgeView2,progressBariamgeView3;
    private RelativeLayout baseLayout;

}

 static class InuvoSearchType_1_ViewHolder{

    //InuvoSearchType_1_ViewHolder:
    private TextView txtInuvoTitle,txtInuvoDescription,txtInuvoLink;
    private ImageView icon;
    private RelativeLayout baseLayout;

 }

static class InuvoSearchType_3_ViewHolder{

    //InuvoSearchType_3_ViewHolder:     
    private TextView txtInuvoTitle,txtInuvoDescription,txtInuvoLink;
    private ImageView icon;     
    private RelativeLayout baseLayout;
}


   Like I shown in the above code, we can create different layout for listview using getItemViewType in getView method. Based on the type value, set the xml  layout. The above code must be used in Adapterclass of the listview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多