只有可扩展的列表视图显示在活动中

Posted

技术标签:

【中文标题】只有可扩展的列表视图显示在活动中【英文标题】:Only expandable listview showing up in activity 【发布时间】:2015-04-29 08:11:45 【问题描述】:

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

这是活动的代码:

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

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

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

    <TextView
        android:layout_
        android:layout_
        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;
    


【问题讨论】:

如果你不使用weights,为什么还要有android:weightSum="1" 【参考方案1】:

将固定高度与重量混合是行不通的。 您可以做什么:将所有元素的高度设置为 0 并为其分配权重。 我会做什么:使用RelativeLayout,将Button 放在顶部,将TextView 放在底部(alignParentBottom="true")和ListView 之间

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

    <Button
        android:layout_
        android:layout_
        android:text="Next"
        android:id="@+id/button" />

    <TextView
        android:layout_alignParentBottom="true"
        android:layout_
        android:layout_
        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_
        android:layout_
        android:groupIndicator="@null"
        android:divider="#A4C739"
        android:dividerHeight="0.5dp" />


</RelativeLayout>

结果:

编辑:您必须实际使用此布局,而您目前不这样做:

  // 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)

【讨论】:

它显示在预览中,但是当我实际将它上传到我的手机进行测试时它没有显示 文本视图和按钮不显示。只显示列表视图 我将内容视图设置为什么?我将它设置为具有可扩展列表视图的活动,但它给了我一个空指针错误 嗯,那是另一个问题了。

以上是关于只有可扩展的列表视图显示在活动中的主要内容,如果未能解决你的问题,请参考以下文章

如何将可扩展列表视图的选定子视图数据从片段发送到父活动?

可扩展列表视图在 android 的片段活动中不起作用

我希望整个活动都是可滚动的,但只有列表视图是可滚动的。我怎么做?

可扩展列表视图默认选择

如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图

如果可扩展列表视图android中的孩子为空,如何单击组