只显示活动中的可展开列表视图

时间:2015-02-26 19:28:22

标签: android listview android-listview expandablelistview expandablelistadapter

我正在制作一款自动沙拉机的Android应用程序。该应用程序允许您选择您的成分等,然后将其发送到机器。我设置了一个带有可扩展列表视图的活动来保存成分,但我还需要活动的下一个按钮和下面的营养信息框。我将它们添加到活动中,但是当我运行它时,只显示可展开的列表视图。

以下是活动的代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"
        android:layout_gravity="right" />

    <ExpandableListView
        android:id="@+id/list"
        android:layout_height="286dp"
        android:layout_width="match_parent"
        android:groupIndicator="@null"
        android:divider="#A4C739"
        android:dividerHeight="0.5dp"
        android:layout_weight="1.15" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="77dp"
        android:text="Nutrition Facts:"
        android:id="@+id/nutritionTV" />

</LinearLayout>

以下是与之相关的类的代码:

    package com.picknchew.companionapp;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;

public class ChooseIngredientsActivity extends ExpandableListActivity{

    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // this is not really  necessary as ExpandableListActivity contains an ExpandableList
        //setContentView(R.layout.main);

        ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)

        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        setGroupParents();
        setChildData();

        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);

        adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);
    }

    public void setGroupParents() {
        parentItems.add("Lettuce");
        parentItems.add("Protein");
        parentItems.add("Fruit");
        parentItems.add("Dressings");
    }

    public void setChildData() {

        // Android
        ArrayList<String> child = new ArrayList<String>();
        child.add("Romain");
        child.add("Iceberg");
        child.add("Arugula");
        child.add("Green");
        childItems.add(child);

        // Core Java
        child = new ArrayList<String>();
        child.add("Tofu");
        child.add("Shredded Cheddar");
        child.add("Sliced Eggs");
        child.add("Beans");
        child.add("Avocado");
        childItems.add(child);

        // Desktop Java
        child = new ArrayList<String>();
        child.add("Melon");
        child.add("Watermelon");
        child.add("Pineapple");
        child.add("Grapefruit");
        childItems.add(child);

        // Enterprise Java
        child = new ArrayList<String>();
        child.add("Honey Mustard");
        child.add("Ranch");
        child.add("Caesar");
        child.add("Vinaigrette");
        childItems.add(child);
    }

}

最后这里是可扩展列表视图适配器的代码,如果相关的话:

package com.picknchew.companionapp;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;

public class MyExpandableAdapter extends BaseExpandableListAdapter {

    private Activity activity;
    private ArrayList<Object> childtems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;

    public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
        this.parentItems = parents;
        this.childtems = childern;
    }

    public void setInflater(LayoutInflater inflater, Activity activity) {
        this.inflater = inflater;
        this.activity = activity;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        child = (ArrayList<String>) childtems.get(groupPosition);

        CheckBox checkbox = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group, null);
        }

       checkbox = (CheckBox) convertView.findViewById(R.id.checkBox2);
        checkbox.setText(child.get(childPosition));

        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(activity, child.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child, null);
        }

        ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);

        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return ((ArrayList<String>) childtems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return parentItems.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

将固定高度与重物混合不起作用。 你可以做什么:将所有元素的高度设置为0并为它们分配权重。 我会做什么:使用RelativeLayout,将Button放在顶部,将TextView置于底部(alignParentBottom="true"),将ListView放在中间,如< / p>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button" />

    <TextView
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="77dp"
        android:text="Nutrition Facts:"
        android:id="@+id/nutritionTV" />

    <ExpandableListView
        android:layout_above="@+id/nutritionTV"
        android:layout_below="@+id/button"
        android:id="@+id/list"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:groupIndicator="@null"
        android:divider="#A4C739"
        android:dividerHeight="0.5dp" />


</RelativeLayout>

结果:

enter image description here

编辑:你必须实际使用这种布局,而你现在却不这样做:

  // this is not really  necessary as ExpandableListActivity contains an ExpandableList
        //setContentView(R.layout.main);

取消注释setContentView行。不要忘记正确分配视图,即

 ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)

将成为

 ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.list)