簡體   English   中英

如何在ListView中膨脹三個布局?

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

我有一個自定義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時出現錯誤

getItemViewType必須返回從0到getViewTypeCount

嘗試這個 :

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

您可以優化,使您的滾動順利通過定義視圖持有人:嘗試

在getItemViewType中,您可以設置要在哪里顯示哪種視圖類型的條件。

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

您可以檢查條件並為所需的布局充氣。 這是例子。 我根據它分為三個類別,分別為不同的布局充氣。

   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