【问题标题】:Androids ExpandableListView - where to put the button listener for buttons that are childrenAndroid ExpandableListView - 放置子按钮的按钮侦听器的位置
【发布时间】:2012-07-05 21:12:47
【问题描述】:

我一直在玩 ExpandableListView,但我不知道在哪里为将成为视图中的子按钮的按钮添加按钮侦听器。我确实设法让一个使用下面的 getChildView() 的按钮侦听器工作,但它似乎是所有按钮的同一个侦听器。

最好的情况是我能够在实例化 ExpandableListAdapter 类的类中实现按钮侦听器,而不必将侦听器放在实际的 ExpandableListAdapter 类中。在这一点上,我什至不知道这是否可能

我一直在试验这个教程/代码:HERE

getChildView()

@Override
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1)
{
    ChildHolder childHolder;
    if (view1 == null)
    {

        view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null);

        childHolder = new ChildHolder();

        childHolder.section_btn = (Button)view1.findViewById(R.id.item_title);
        view1.setTag(childHolder);
        childHolder.section_btn.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show();

            }
        });



    }else {
        childHolder = (ChildHolder) view1.getTag();
    }
        childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section);
        Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF");

        childHolder.section_btn.setTypeface(tf);
        return view1;
}

任何帮助将不胜感激。谢谢你,我会一直待着的。

【问题讨论】:

    标签: java android expandablelistview onclicklistener expandablelistadapter


    【解决方案1】:

    如果按钮在 ExpandableListView 中,它们的监听器需要在适配器中。

    我不确定你问题的主旨,但如果你问如何将按钮与子行的内容联系起来,我可以回答。 :p

    出于演示目的,我将假设一个稍微简单的子行布局。

    child_row.xml

    <?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="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/ListItem1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left|center_vertical"
        android:paddingLeft="7dip"
        android:paddingRight="7dip"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/ListItem2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right|center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/button"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
    

    然后,要在按下按钮时获取行的内容,请使用按钮回溯到父视图,然后获取必要的子视图及其内容:

        childHolder.section_btn.setOnClickListener(new OnClickListener()    
        {    
            @Override    
            public void onClick(View v)    
            {    
                LinearLayout ll = (LinearLayout) v.getParent();    // get the view containing the button
                TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1);  // get the reference to the first widget
                TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2);  // get the reference to the second widget
                String text1 = tv1.getText.toString();  // Get the contents of the first widget to a string
                String text2 = tv2.getText.toString();  // Get the contents of the second widget to a string
            }    
        });  
    

    如果这不是您要寻找的内容,请澄清您的问题,我会再试一次。

    【讨论】:

    • 非常感谢您从实际小部件中获取字符串的好主意,不知道为什么我没有想到这一点。
    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2013-01-24
    • 2013-05-18
    • 1970-01-01
    • 2011-11-27
    相关资源
    最近更新 更多