ExpandableList查看未使用ExpandableListActivity [英] ExpandableList View not using ExpandableListActivity

查看:122
本文介绍了ExpandableList查看未使用ExpandableListActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我没有足够的信誉分,previous问题发表意见呢,我不得不创建一个新的。我需要一个可扩展的列表视图并没有占据整个活动,所以我用这个例子来做到这一点不ExpandableListActivity:

ExpandableList查看不扩大

我稍微修改code:

 公共类主要扩展活动{

    ExpandableListView LV;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.expandable_list);
        LV =(ExpandableListView)this.findViewById(R.id.expandableListView1);
        MyExpandableListAdapter expandableAdapter =新MyExpandableListAdapter();
        lv.setAdapter(expandableAdapter);
    }

    类MyExpandableListAdapter扩展BaseExpandableListAdapter {

    //样本数据集。孩子们[I]包含儿童(字符串[])团体[1]。
    私有String []组= {人名称,狗名,名猫,鱼名};
    私有String [] []孩子= {
            {阿诺德,巴里,查克,大卫},
            {王牌,强盗,双查,平分},
            {蓬松,的Snuggles},
            {戈尔迪,泡}
    };


        公共对象getChild(INT groupPosition,诠释childPosition){
            返回儿[groupPosition] [childPosition]
        }

        众长getChildId(INT groupPosition,诠释childPosition){
            返回childPosition;
        }

        公众诠释getChildrenCount(INT groupPosition){
            返回儿[groupPosition] .length;
        }

        公众的TextView getGenericView(){
            //布局参数ExpandableListView
            AbsListView.LayoutParams LP =新AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,64);

            TextView中的TextView =新的TextView(Main.this);
            textView.setLayoutParams(LP);
            //中心的文本垂直
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            //设置文本的起始位置
            textView.setPadding(36,0,0,0);
            返回的TextView;
        }



        公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,
                查看convertView,ViewGroup中父){


            TextView中的TextView = getGenericView();
            textView.setText(getChild(groupPosition,childPosition)的ToString());
            返回的TextView;

        }

        公共对象getGroup(INT groupPosition){
            返回组[groupPosition]
        }

        公众诠释getGroupCount(){
            返回groups.length;
        }

        众长getGroupId(INT groupPosition){
            返回groupPosition;
        }

        公共查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,ViewGroup中父){
            TextView中的TextView = getGenericView();
            textView.setText(getGroup(groupPosition)的ToString());
            返回的TextView;

        }

        公共布尔hasStableIds(){
            返回true;
        }

        公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
            返回true;
        }
    }

}
 

这工作得很好,但要使其更加坚固,我想分开在code创建的XML文件(这是个好主意吗?)这是我碰到一些FC的问题textViews。

group_row.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < TextView的机器人:ID =@ + ID / row_name
         机器人:以下属性来=50像素
         机器人:TEXTSIZE =20像素
         机器人:TEXTSTYLE =正常
         机器人:layout_width =320px的
         机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

和我在java文件上面的改变这样的:

 公开查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,ViewGroup中父){

    TextView的parentRow =(TextView中)findViewById(R.id.row_name);
    parentRow.setText(getGroup(groupPosition)的ToString());
    返回parentRow;
}
 

我也试图消除的LinearLayout包装,但没有任何修复。谁能告诉我为什么我的XML视图不工作?谢谢你。

编辑: 由于弗洛,新的code表示正在使用教程:

group_row.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /组名
    机器人:以下属性来=50dp
    机器人:TEXTSIZE =30sp
    机器人:TEXTSTYLE =正常
    机器人:layout_height =WRAP_CONTENT/>
 

主营:

 公开查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,ViewGroup中父){


        查看parentView = convertView;
        如果(parentView == NULL){
            LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            parentView = vi.inflate(R.layout.group_row,NULL);
        }
                TextView的parentText =(TextView中)parentView.findViewById(R.id.group_name);
                如果(parentText!= NULL){
                      parentText.setText(getGroup(groupPosition)的ToString());
                }

       返回parentText;


    }
 

解决方案

您实现getGroupView()方法是错误的。目前,在活动的布局你在寻找一个元素与元素 R.id.row_name 不存在。我猜行 parentRow.setText(getGroup(groupPosition)的ToString()); 抛出一个异常,如 parentRow 是空值。

正确执行方法,从一个XML文件中的自定义行布局是夸大这种布局。请查看本教程。它是为正常列表视图和适配器,但膨胀的行的概念布局它是相同的。

Because I don't have enough reputation points to comment on previous questions yet, I had to create a new one. I needed an expandable list view that didn't take up the entire activity so I used this example to do it without ExpandableListActivity:

ExpandableList View don't expand

My slightly modified code:

public class Main extends Activity {

    ExpandableListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list);
        lv = (ExpandableListView) this.findViewById(R.id.expandableListView1);
        MyExpandableListAdapter expandableAdapter = new MyExpandableListAdapter();
        lv.setAdapter(expandableAdapter);
    }

    class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };


        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

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

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(Main.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }



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


            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;

        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;

        }

        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}

This works fine, but to make it more robust, I wanted to separate the textViews created in code to xml files (This is a good idea right?) This is where I run into some FC issues.

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/row_name"
         android:paddingLeft="50px"
         android:textSize="20px"
         android:textStyle="normal"
         android:layout_width="320px"
         android:layout_height="wrap_content"/>
</LinearLayout>

And I changed this in the java file above:

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

    TextView parentRow = (TextView) findViewById(R.id.row_name);
    parentRow.setText(getGroup(groupPosition).toString());
    return parentRow;
}

I also tried removing the LinearLayout wrapper but that didn't fix anything. Can anyone tell me why my xml view isn't working? Thanks.

EDIT: Thanks Flo, new code that is working using the tutorial:

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group_name"
    android:paddingLeft="50dp"
    android:textSize="30sp"
    android:textStyle="normal"
    android:layout_height="wrap_content"/>

Main:

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


        View parentView = convertView;
        if (parentView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            parentView = vi.inflate(R.layout.group_row, null);
        }
                TextView parentText = (TextView) parentView.findViewById(R.id.group_name);
                if (parentText != null) {
                      parentText.setText(getGroup(groupPosition).toString());                     
                }

       return parentText;


    }

解决方案

Your implementation of the getGroupView() method is wrong. At the moment your searching in the activity's layout for a element with the element R.id.row_name which doesn't exist. I guess the line parentRow.setText(getGroup(groupPosition).toString()); throws an exception as parentRow is null.

The correct implementation of the method with a custom row layout from a xml file is to inflate this layout. Check out this tutorial. It's for normal ListViews and adapter but the concept of inflating the row layouts it the same.

这篇关于ExpandableList查看未使用ExpandableListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆