简体   繁体   English

如何在ListView中膨胀三个布局?

[英]How can i inflate three layouts in ListView?

I have a custom listView in which i want to inflate three different layouts. 我有一个自定义listView,我想在其中充气三种不同的布局。

I have seen many questions but they are all for odd and even position but in my listView the layout that should be inflated to the listView depends on other condition and its dynamic like 我看过很多问题,但是它们都是奇数和偶数位置,但是在我的listView中,应该夸大到listView的布局取决于其他条件及其动态变化,例如

if (i == 0) i get the first Layout to be inflated and if (i==1) second one and so on, 如果(i == 0)我得到第一个要放大的布局,如果(i == 1)我得到了第二个,依此类推,

The variable "i" is equal to the value I would be getting from my main Activity. 变量“ 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();
            }

I tried this code but dint work... 我尝试了此代码,但工作正常……

I was getting an error when i scroll through the listView arrayoutofbondexception 滚动listView arrayoutofbondexception时出现错误

getItemViewType必须返回从0到getViewTypeCount

Try this : 尝试这个 :

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

You can optimize and make your scrolling smooth by defining view holder : Try this also 您可以优化,使您的滚动顺利通过定义视图持有人:尝试

in getItemViewType you can set your condition where do you want to show which type of view. 在getItemViewType中,您可以设置要在哪里显示哪种视图类型的条件。

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

You can check the condition and inflate the layout that you want. 您可以检查条件并为所需的布局充气。 Here is the example. 这是例子。 I have three category depending on it I inflate different layouts. 我根据它分为三个类别,分别为不同的布局充气。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM