Android ExpandableListView同时显示多种不同类型字列表

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ExpandableListView mExpandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mExpandableListView = findViewById(R.id.expandablelistview);

        List groupList = new ArrayList<>();
        groupList.add("      图形");
        groupList.add("      文字");
        groupList.add("      MPAndroidChart");

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap bitmap = BitmapFactory.decodeFile("mnt/sdcard/test.png", options);

        List> childList = new ArrayList<>();
        List childList1 = new ArrayList<>();
        childList1.add(bitmap);
        List childList2 = new ArrayList<>();
        childList2.add("1234567890");
        List childList3 = new ArrayList<>();
        childList3.add("自定义View");
        childList3.add("");

        childList.add(childList1);
        childList.add(childList2);
        childList.add(childList3);

        DemoAdapter demoAdapter = new DemoAdapter(groupList, childList);
        mExpandableListView.setAdapter(demoAdapter);

        mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {//一级点击监听
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                //如果你处理了并且消费了点击返回true,这是一个基本的防止onTouch事件向下或者向上传递的返回机制
                Log.d("onGroupClick", "onGroupClick");
                return false;
            }
        });

        mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {//二级点击监听
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Log.d("onChildClick", "onChildClick");
                //如果你处理了并且消费了点击返回true
                return false;
            }
        });
    }
} 
  

DemoAdapter.java

public class DemoAdapter extends BaseExpandableListAdapter {

    List mGroupList;
    List> mChildList;
    private LineChart mChartWeekHeart;


    public DemoAdapter(List groupList, List> childList) {
        mGroupList = groupList;
        mChildList = childList;
    }

    @Override
    public int getGroupCount() {//返回第一级List长度
        return mGroupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {//返回指定groupPosition的第二级List长度
        return mChildList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {//返回一级List里的内容
        return mGroupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {//返回二级List的内容
        return mChildList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {//返回一级View的id 保证id唯一
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition + childPosition;
    }

    /**
     * 指示在对基础数据进行更改时子ID和组ID是否稳定
     *
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 返回一级父View
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_simple_spinner_dropdown_item, parent, false);
        ((TextView) convertView).setText((String) getGroup(groupPosition));
        return convertView;
    }

    /**
     * 返回二级子View
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        if (groupPosition == 0) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view1, parent, false);
            ((ImageView) convertView).setImageBitmap((Bitmap) getChild(groupPosition, childPosition));
            return convertView;
        } else if (groupPosition == 1) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view2, parent, false);
            ((TextView) convertView).setText((String) getChild(groupPosition, childPosition));
            return convertView;
        } else if (groupPosition == 2) {
            if (childPosition == 0) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_simple_spinner_dropdown_item, parent, false);
                ((TextView) convertView).setGravity(Gravity.CENTER);
                ((TextView) convertView).setText((String) getChild(groupPosition, childPosition));
            } else {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
                mChartWeekHeart = (LineChart) convertView;
                setData();
                ((LineChart) convertView).invalidate();
            }
            return convertView;
        }
        return convertView;
    }

    private void setData() {
        // グラフを表示する
        mChartWeekHeart.getDescription().setEnabled(false);         // グラフのタイトル
        mChartWeekHeart.setTouchEnabled(false);                   // グラフへのタッチジェスチャー
        mChartWeekHeart.setScaleEnabled(false);                  // グラフのスケーリング
        mChartWeekHeart.setDragEnabled(false);                    // グラフのドラッギング
        mChartWeekHeart.setPinchZoom(false);                    // グラフのピンチ/ズーム
        mChartWeekHeart.setBackgroundColor(Color.TRANSPARENT);  // グラフの背景色(透過)
        mChartWeekHeart.getLegend().setEnabled(false);

        List labels = new ArrayList();
        labels.add("");                                      // 0
        labels.add("");                                      // 1
        labels.add("");                                      // 2
        labels.add("");                                      // 3
        labels.add("5");                                     // 4
        labels.add("");                                      // 5
        labels.add("");                                      // 6
        labels.add("4");                                     // 7
        labels.add("");                                      // 8
        labels.add("3");                                     // 9
        labels.add("");                                      // 10
        labels.add("2");                                     // 11
        labels.add("");                                      // 12
        labels.add("");                                      // 13
        labels.add("1");                                     // 14
        labels.add("");                                      // 15

        YAxis leftYAxis = mChartWeekHeart.getAxisLeft();            // Y軸(左)の取得
        leftYAxis.setAxisMaximum(16f);                                // Y軸(左)の最大値
        leftYAxis.setAxisMinimum(1f);                                 // Y軸(左)の最小値
        leftYAxis.setLabelCount(15);                                 // Y軸(左)の目盛りの数
        IndexAxisValueFormatter formatter = new IndexAxisValueFormatter(labels);
        leftYAxis.setValueFormatter(formatter); // Y軸(左)の目盛りラベルを設定
        leftYAxis.setDrawGridLines(false);
        leftYAxis.setInverted(true);                                // Y軸の上下反転
        mChartWeekHeart.getAxisRight().setEnabled(false);               // Y軸(右)は表示しない

        leftYAxis.removeAllLimitLines();
        LimitLine line6 = new LimitLine(6f);
        line6.setLineWidth(1f);
        line6.enableDashedLine(25f, 25f, 0f);
        line6.setLineColor(Color.BLACK);
        leftYAxis.addLimitLine(line6);

        LimitLine line8 = new LimitLine(8f);
        line8.setLineWidth(1f);
        line8.enableDashedLine(25f, 25f, 0f);
        line8.setLineColor(Color.BLACK);
        leftYAxis.addLimitLine(line8);

        LimitLine line10 = new LimitLine(10f);
        line10.setLineWidth(1f);
        line10.enableDashedLine(25f, 25f, 0f);
        line10.setLineColor(Color.BLACK);
        leftYAxis.addLimitLine(line10);

        LimitLine line12 = new LimitLine(12f);
        line12.setLineWidth(1f);
        line12.enableDashedLine(25f, 25f, 0f);
        line12.setLineColor(Color.BLACK);
        leftYAxis.addLimitLine(line12);


        XAxis xAxis = mChartWeekHeart.getXAxis();    // X軸の設定
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // X軸のラベルをグラフ下部に設定
        xAxis.setAxisMaximum(8f);                      // X軸の最大値
        xAxis.setAxisMinimum(0f);                      // X軸の最小値
        xAxis.setDrawGridLines(false);               // X軸の目盛り非表示

        List labelsX = new ArrayList();
        labelsX.add("");
        labelsX.add("月");
        labelsX.add("火");
        labelsX.add("水");
        labelsX.add("木");
        labelsX.add("金");
        labelsX.add("土");
        labelsX.add("日");
        xAxis.setValueFormatter(new IndexAxisValueFormatter(labelsX)); // X軸(下)の目盛りラベルを設定
        xAxis.setLabelCount(8);                                    // X軸(下)の目盛りの数

        List values = new ArrayList();
        for (int i = 0; i < 5; i++) {
            values.add(new Entry(1f + i, 2, null, null));
        }

        List dataSets = new ArrayList();
        if (values.size() > 0) {
            LineDataSet set1 = new LineDataSet(values, "");     // グラフ下部のプロット説明
            set1.setDrawIcons(false);
            set1.setColor(Color.BLACK);                 // 折れ線の色
            set1.setCircleColor(Color.BLACK);          // グラフ上のプロットの色
            set1.setLineWidth(1f);
            set1.setCircleRadius(3f);
            set1.setDrawCircleHole(false);
            set1.setValueTextSize(0f);
            set1.setDrawFilled(false);               // 折れ線下部の塗りつぶし
            set1.setFormLineWidth(1f);
            float data[] = new float[2];
            data[0] = 10f;
            data[1] = 5f;
            set1.setFormLineDashEffect(new DashPathEffect(data, 0f));
            dataSets.add(set1);
        }
        LineData lineData = new LineData(dataSets);
        mChartWeekHeart.setData(lineData);
        mChartWeekHeart.getData().notifyDataChanged();
    }

    /**
     * 指定位置的子项是否可选
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

布局文件

activity_main.xml




    
    

view1.xml



view2.xml




layout.xml

需要事先将MPAndroidChartv_3.1.0.jar包存入到工程的libs目录下面

你可能感兴趣的:(Android,android)