【问题标题】:getChildView (for BaseExpandableListAdapter) not working properlygetChildView(用于 BaseExpandableListAdapter)无法正常工作
【发布时间】:2012-09-25 20:28:44
【问题描述】:

我有一个ExpandableListView。子行的 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_child"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/number_input_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:gravity="right"
            android:inputType="numberDecimal" >

            <requestFocus />
        </EditText>

        <Spinner
            android:id="@+id/spinner_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/temp_txt" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/number_input_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:gravity="right"
            android:inputType="numberDecimal" />

        <Spinner
            android:id="@+id/spinner_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

基本上,我的getChildView 是这样的:

public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

    if (view == null) {
        view = inflater.inflate(R.layout.list_item_child, viewGroup, false);
    }      
    Spinner spinner = (Spinner) view.findViewById(R.id.spinner_1);
    spinner.setAdapter(mUnitsCA);
    spinner.setSelection(14);  // 14 is a test
    spinner.setOnItemSelectedListener(mSpinnerListener);

    return view;
}

这应该确保每次都选择 spinner_1 中的项目编号 14。有时这可行,但通常它会选择微调器中的第一项。我无法弄清楚这是在哪里发生的。有什么想法吗?

【问题讨论】:

  • 我发现的一个建议是将setSelection() 更改为以下内容:spinner.setSelection(14, true);。你试过这个吗?
  • 哇,是的,这成功了!谢谢你。顺便说一句,第二个参数可以是真或假,它仍然有效。似乎“setSelection”的单参数版本有问题。
  • 嗯,这个网站的新手,如何将上面的评论标记为已回答?
  • 哈,这很有趣...您不能接受 cmets,所以我将发表我的评论作为答案,以便您接受。只需单击绿色复选标记。谢谢。祝你好运。

标签: android spinner expandablelistview


【解决方案1】:

我发现的一个建议是将setSelection() 更改为以下内容:

spinner.setSelection(14, true);

编辑: (基于我对未来求职者的回答)

documentation 对此似乎很含糊,只是陈述如下:

setSelection(int position, boolean animate);
//Jump directly to a specific item in the adapter data.

在我的研究中,似乎很多人都遇到了同样的问题,据我所知,声明动画boolean 似乎是必要的,而不是必要的,无论是真还是假。也许只有自定义动画或声明的动画需要 trueboolean 值才能制作动画,但没有动画将接受 truefalse...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多