【问题标题】:How can i inflate three layouts in ListView?如何在 ListView 中膨胀三个布局?
【发布时间】:2013-05-12 05:54:36
【问题描述】:

我有一个自定义 listView,我想在其中添加三种不同的布局。

我见过很多问题,但它们都是针对奇数和偶数位置的,但在我的 listView 中,应该膨胀到 listView 的布局取决于其他条件及其动态,例如

如果 (i == 0) 我得到第一个要膨胀的布局,如果 (i==1) 第二个等等,

变量“i”等于我从主活动中获得的值。

public class SocialListAdapter extends ArrayAdapter<Item> {


private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
private int i;


public SocialListAdapter(Activity act, int resource, List<Item> arrayList) {
    super(act, resource, arrayList);
    // TODO Auto-generated constructor stub
    this.activity= act;
    this.items = arrayList;
    this.row = resource;

}

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

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


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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    objBean = items.get(position);


        i = objBean.getI();


            if (view == null) {

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



        if(i == 0){

            view = inflater.inflate(R.layout.list, null);

        }  else if (i == 1){
            view = inflater.inflate(R.layout.row1, null);
        }
            holder = new ViewHolder();
            view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

我尝试了这段代码,但是没有用...

当我滚动浏览 listView arrayoutofbondexception 时出现错误

【问题讨论】:

  • 对于初学者来说,如果你想支持 3 种不同的布局,在 getViewTypeCount() 中返回 2 是没有意义的。此外,getItemViewType(int position) 应该返回一个您希望在position 给出的行中显示的类型唯一的值。换句话说:如果您的类型计数为 3,那么您应该只在该方法中返回 3 个不同的值,而不是 n。

标签: android listview layout row inflate


【解决方案1】:

试试这个:

public class CustomAdapter extends BaseAdapter {

    private ArrayList<String> comments; 
    Context mContext;

    public CustomAdapter(Context context, ArrayList<String> comments) {
        this.mContext = context;
        this.comments = comments;       
    }


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

        String item = comments.get(position);

        if (getItemViewType(position) == 0) {

            View v = convertView;
            if (v == null) {

                //GET View 1
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment2, null);

                v = viewGroup;
            } 

            //Fill Data for Ist view 
            TextView comm = (TextView) v.findViewById(R.id.comment);
            comm.setText(item);

            return v;


        } else if (getItemViewType(position) == 1) {


            View v = convertView;
            if (v == null) {

                //GET View 2
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment1, null);


                v = viewGroup;
            }

            //Fill Data for IInd view 
            TextView comm = (TextView) v.findViewById(R.id.comment);
            comm.setText(item);

            return v;

        } else {

            //GET View 3
            View v = convertView;
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment3, null);


                v = viewGroup;
            }

            //Fill Data for IInd view 
            TextView comm = (TextView) v.findViewById(R.id.comment);
            comm.setText(item);

            return v;

        }

    }

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

    @Override
    public Object getItem(int position) {       
        return comments.get(position);
    }

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

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

    @Override
    public int getItemViewType(int position) {

        if(position == 0)
            return 0;
        else if (position == 1)
            return 1;
        else 
            return 2;
    }
}

您可以通过定义视图持有者来优化并使滚动平滑: 也试试this

在 getItemViewType 中,您可以设置条件,您想在哪里显示哪种类型的视图。

【讨论】:

    【解决方案2】:

    getItemViewType 必须返回一个从 0 到 getViewTypeCount 的数字。

    【讨论】:

      【解决方案3】:

      在 getView 方法中使用“if(position==0)”

      【讨论】:

        【解决方案4】:

        您可以检查条件并扩展您想要的布局。 这是示例。我有三个类别,这取决于它我膨胀不同的布局。

           public class ApplicationAdapter extends BaseAdapter
            {
                private Context cntx;
        
                /**
                 * 0 - featured 1- Top rated 2- other
                 */
                private int whichCategory;
        
                /**
                 * Constructor
                 * 
                 * @param context
                 * @param iwhichCategory
                 *            0 - featured 1- Top rated 2- other
                 */
        
                public ApplicationAdapter(Context context, int iwhichCategory)
                    {
                        cntx = context;
        
                        whichCategory = iwhichCategory;
        
                    }
        
                        @Override
                public int getCount()
                    {
                        return entApplications.size();
                    }
        
                @Override
                public Object getItem(int position)
                    {
                        return null;
                    }
        
                @Override
                public long getItemId(int position)
                    {
                        return 0;
                    }
        
        
                @Override
                public View getView(int position, View convertView, ViewGroup parent)
                    {
                        if (convertView == null)
                            {
                                // This a new view we inflate the new layout
                                LayoutInflater inflater = (LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                if (whichCategory == 0)
                                    convertView = inflater.inflate(R.layout.featured_app_grid_item, parent, false);
                                if (whichCategory == 1)
                                    convertView = inflater.inflate(R.layout.other_app_grid_item, parent, false);
                                if (whichCategory == 2)
                                    convertView = inflater.inflate(R.layout.other_app_grid_item1, parent, false);
                            }
                    }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-11
          • 1970-01-01
          • 1970-01-01
          • 2011-08-29
          • 1970-01-01
          相关资源
          最近更新 更多