为了账号安全,请及时绑定邮箱和手机立即绑定

求助:android ListView里展现不同的布局

求助:android ListView里展现不同的布局

偶然的你 2018-08-14 11:19:42
android ListView里展现不同的布局,其子布局里嵌套GridView(也是不同的布局) 怎么给GridView传值啊(黄色框框).ListView(红框内)  GridView(黄色框框)  多谢
查看完整描述

1 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

package com.andbase.patrol.gtDemo.adapter.pagesAdapter;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.GridView;import android.widget.TextView;import com.andbase.R;import com.andbase.patrol.gtDemo.activity.pages.Page1Activity;import com.andbase.patrol.gtDemo.model.Page1FormDemo;import com.andbase.patrol.gtDemo.model.PointStateDemo;import java.util.ArrayList;import java.util.List;/**
 * Created by lmx on 2018/2/26. */public class Page1ListViewAdapter extends BaseAdapter {    private Page1Activity mContext;    private List<Page1FormDemo> lists;    private LayoutInflater layinf;    //GridView加载不同布局
    public List<PointStateDemo> listPoint = null;    public Page1ListViewAdapter(Page1Activity context, List<Page1FormDemo> lists){        this.mContext = context;        this.lists = lists;
        layinf = LayoutInflater.from(context);
    }

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

    @Override    public int getItemViewType(int position) {        //根据position返回指定的布局类型,比如0、1,根据这个返回值加载不同布局
        return lists.get(position).getPropertyType();
    }

    @Override    public int getViewTypeCount() {        //这里是adapter里有几种布局
        return 2;
    }

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

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

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

        Page1FormDemo form = lists.get(position);        //第二个布局:进行中
        ViewDoingHolder doingHolder = null;        //第三个布局:已完成
        ViewDoneHolder doneHolder = null;        if (form == null) {            return null;
        }        if (convertView == null) {            switch (form.getPropertyType()){                case 0:
                    convertView = layinf.inflate(R.layout.page1_item1_demo, parent, false);                    //使用减少findView的次数
                    doingHolder = new Page1ListViewAdapter.ViewDoingHolder();
                    doingHolder.taskName = (TextView) convertView.findViewById(R.id.taskNameDoing);
                    doingHolder.taskTime = (TextView) convertView.findViewById(R.id.taskTimeDoing);
                    doingHolder.taskState = (TextView) convertView.findViewById(R.id.taskStateDoing);
                    doingHolder.GridList = (GridView) convertView.findViewById(R.id.GridListDoing);
                    doingHolder.okBtn = (Button) convertView.findViewById(R.id.okBtn);
                    doingHolder.noBtn = (Button) convertView.findViewById(R.id.noBtn);                    //设置标记                    convertView.setTag(doingHolder);                    break;                case 1:
                    convertView = layinf.inflate(R.layout.page1_item2_demo, parent, false);                    //使用减少findView的次数
                    doneHolder = new Page1ListViewAdapter.ViewDoneHolder();
                    doneHolder.taskName = (TextView) convertView.findViewById(R.id.taskNameDone);
                    doneHolder.taskTime = (TextView) convertView.findViewById(R.id.taskTimeDone);
                    doneHolder.taskState = (TextView) convertView.findViewById(R.id.taskStateDone);
                    doneHolder.GridList = (GridView) convertView.findViewById(R.id.GridListDone);
                    doneHolder.okBtn = (Button) convertView.findViewById(R.id.okBtn);
                    doneHolder.noBtn = (Button) convertView.findViewById(R.id.noBtn);                    //设置标记                    convertView.setTag(doneHolder);                    break;

            }

        } else {            switch (form.getPropertyType()){                case 0:
                    doingHolder = (Page1ListViewAdapter.ViewDoingHolder) convertView.getTag();                    break;                case 1:
                    doneHolder = (Page1ListViewAdapter.ViewDoneHolder) convertView.getTag();                    break;
            }

        }        /**
         * 根据不同布局加载不同数据
         * */
        switch (form.getPropertyType()){            case 0:
                doingHolder.taskName.setText(form.getTaskName());
                doingHolder.taskTime.setText(form.getTaskTime());
                doingHolder.taskState.setText(form.getTaskState());                //GridView
                listPoint = new ArrayList<PointStateDemo>();                try {
                    PointStateDemo form1 = new PointStateDemo();
                    form1.setPatrolStatusType(0);
                    form1.setPatrolStatus("0");
                    form1.setPointName("1");
                    listPoint.add(form1);

                    PointStateDemo form2 = new PointStateDemo();
                    form2.setPatrolStatusType(0);
                    form2.setPatrolStatus("0");
                    form2.setPointName("2");
                    listPoint.add(form2);

                    PointStateDemo form3 = new PointStateDemo();
                    form3.setPatrolStatusType(0);
                    form3.setPatrolStatus("0");
                    form3.setPointName("3");
                    listPoint.add(form3);

                    PointStateDemo form4 = new PointStateDemo();
                    form4.setPatrolStatusType(0);
                    form4.setPatrolStatus("0");
                    form4.setPointName("4");
                    listPoint.add(form4);

                    PointStateDemo form5 = new PointStateDemo();
                    form5.setPatrolStatusType(0);
                    form5.setPatrolStatus("0");
                    form5.setPointName("5");
                    listPoint.add(form5);

                    PointStateDemo form6 = new PointStateDemo();
                    form6.setPatrolStatusType(1);
                    form6.setPatrolStatus("1");
                    form6.setPointName("6");
                    listPoint.add(form6);

                    PointStateDemo form7 = new PointStateDemo();
                    form7.setPatrolStatusType(1);
                    form7.setPatrolStatus("1");
                    form7.setPointName("7");
                    listPoint.add(form7);

                    PointStateDemo form8 = new PointStateDemo();
                    form8.setPatrolStatusType(1);
                    form8.setPatrolStatus("1");
                    form8.setPointName("8");
                    listPoint.add(form8);

                    PointStateDemo form9 = new PointStateDemo();
                    form9.setPatrolStatusType(1);
                    form9.setPatrolStatus("1");
                    form9.setPointName("9");
                    listPoint.add(form9);

                    PointStateDemo form10 = new PointStateDemo();
                    form10.setPatrolStatusType(1);
                    form10.setPatrolStatus("1");
                    form10.setPointName("10");
                    listPoint.add(form10);

                    PointStateDemo form11 = new PointStateDemo();
                    form11.setPatrolStatusType(1);
                    form11.setPatrolStatus("1");
                    form11.setPointName("11");
                    listPoint.add(form11);

                    PointStateDemo form12 = new PointStateDemo();
                    form12.setPatrolStatusType(1);
                    form12.setPatrolStatus("1");
                    form12.setPointName("12");
                    listPoint.add(form12);

                    PointStateDemo form13 = new PointStateDemo();
                    form13.setPatrolStatusType(1);
                    form13.setPatrolStatus("1");
                    form13.setPointName("13");
                    listPoint.add(form13);

                    PointStateDemo form14 = new PointStateDemo();
                    form14.setPatrolStatusType(1);
                    form14.setPatrolStatus("1");
                    form14.setPointName("14");
                    listPoint.add(form14);

                    PointStateDemo form15 = new PointStateDemo();
                    form15.setPatrolStatusType(1);
                    form15.setPatrolStatus("1");
                    form15.setPointName("15");
                    listPoint.add(form15);

                }catch(Exception  ex) {
                    Log.e("JSON Error: ", ex.toString());
                }

                Page1Adapter page1Adapter = new Page1Adapter(mContext,listPoint);
                doingHolder.GridList.setAdapter(page1Adapter);                break;            case 1:
                doneHolder.taskName.setText(form.getTaskName());
                doneHolder.taskTime.setText(form.getTaskTime());
                doneHolder.taskState.setText(form.getTaskState());                //GridView
                listPoint = new ArrayList<PointStateDemo>();                try {
                    PointStateDemo form1 = new PointStateDemo();
                    form1.setPatrolStatusType(0);
                    form1.setPatrolStatus("0");
                    form1.setPointName("1");
                    listPoint.add(form1);

                    PointStateDemo form2 = new PointStateDemo();
                    form2.setPatrolStatusType(0);
                    form2.setPatrolStatus("0");
                    form2.setPointName("2");
                    listPoint.add(form2);

                    PointStateDemo form3 = new PointStateDemo();
                    form3.setPatrolStatusType(0);
                    form3.setPatrolStatus("0");
                    form3.setPointName("3");
                    listPoint.add(form3);

                    PointStateDemo form4 = new PointStateDemo();
                    form4.setPatrolStatusType(0);
                    form4.setPatrolStatus("0");
                    form4.setPointName("4");
                    listPoint.add(form4);

                    PointStateDemo form5 = new PointStateDemo();
                    form5.setPatrolStatusType(0);
                    form5.setPatrolStatus("0");
                    form5.setPointName("5");
                    listPoint.add(form5);

                    PointStateDemo form6 = new PointStateDemo();
                    form6.setPatrolStatusType(0);
                    form6.setPatrolStatus("0");
                    form6.setPointName("6");
                    listPoint.add(form6);

                    PointStateDemo form7 = new PointStateDemo();
                    form7.setPatrolStatusType(0);
                    form7.setPatrolStatus("0");
                    form7.setPointName("7");
                    listPoint.add(form7);

                    PointStateDemo form8 = new PointStateDemo();
                    form8.setPatrolStatusType(0);
                    form8.setPatrolStatus("0");
                    form8.setPointName("8");
                    listPoint.add(form8);

                    PointStateDemo form9 = new PointStateDemo();
                    form9.setPatrolStatusType(0);
                    form9.setPatrolStatus("0");
                    form9.setPointName("9");
                    listPoint.add(form9);

                    PointStateDemo form10 = new PointStateDemo();
                    form10.setPatrolStatusType(0);
                    form10.setPatrolStatus("0");
                    form10.setPointName("10");
                    listPoint.add(form10);

                    PointStateDemo form11 = new PointStateDemo();
                    form11.setPatrolStatusType(0);
                    form11.setPatrolStatus("0");
                    form11.setPointName("11");
                    listPoint.add(form11);

                    PointStateDemo form12 = new PointStateDemo();
                    form12.setPatrolStatusType(0);
                    form12.setPatrolStatus("0");
                    form12.setPointName("12");
                    listPoint.add(form12);

                    PointStateDemo form13 = new PointStateDemo();
                    form13.setPatrolStatusType(0);
                    form13.setPatrolStatus("0");
                    form13.setPointName("13");
                    listPoint.add(form13);

                    PointStateDemo form14 = new PointStateDemo();
                    form14.setPatrolStatusType(0);
                    form14.setPatrolStatus("0");
                    form14.setPointName("14");
                    listPoint.add(form14);

                    PointStateDemo form15 = new PointStateDemo();
                    form15.setPatrolStatusType(0);
                    form15.setPatrolStatus("0");
                    form15.setPointName("15");
                    listPoint.add(form15);

                }catch(Exception  ex) {
                    Log.e("JSON Error: ", ex.toString());
                }

                Page1Adapter2 page1Adapter2 = new Page1Adapter2(mContext,listPoint);
                doneHolder.GridList.setAdapter(page1Adapter2);                break;
        }        return convertView;
    }    /**
     * ViewHolder类     */
    static class ViewDoingHolder {
        TextView taskName;
        TextView taskTime;
        TextView taskState;
        GridView GridList;
        Button noBtn;
        Button okBtn;
    }    static class ViewDoneHolder {
        TextView taskName;
        TextView taskTime;
        TextView taskState;
        GridView GridList;
        Button noBtn;
        Button okBtn;
    }

}


查看完整回答
反对 回复 2018-08-27
  • 1 回答
  • 0 关注
  • 794 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信