【问题标题】:Android: a spinner in a list view, Where is the OnClick/OnItemClick/OnItemSelected event?Android:列表视图中的微调器,OnClick/OnItemClick/OnItemSelected 事件在哪里?
【发布时间】:2011-01-29 09:34:40
【问题描述】:

我有N“书”,每本书都有一些章节。所以我创建了一个ListViewListView 中的每个项目都有两部分:TextView 用于名称,Spinner 用于列出章节。

我为ListView - BookAdapt 创建了一个自定义适配器,并为Spinner - ChapAdapt 创建了一个自定义适配器。我有它显示和工作,但我不知道如何添加点击/选定事件。

这里是列表资源

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Load Chapter" />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button
        android:text="Load"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
     <Button
        android:text="Cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

<ListView
    android:id="@+id/chapview" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>      

这是每本书的展示

<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/viewbook"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<Spinner
    android:id="@+id/chapspinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/chapter_prompt" 
    android:layout_weight="1"/>

这里是来自BookAdapt的关键部分

class BookAdapt extends ArrayAdapter {
       BookAdapt( Context context, int txtres, BookHolder[] bks) {

     super( context, txtres );
       mContext = context;
       books = bks;
       mInflater = LayoutInflater.from(context);
      }

      public View getView( int pos, View convert, ViewGroup parent) {
       BookHolder book = books[ pos];
       String bkname = book.getName();
       if (convert == null) {
        convert = mInflater.inflate(R.layout.bookview, null);
        book.text = (TextView) convert.findViewById(R.id.viewbook);
        Spinner sp = (Spinner)convert.findViewById( R.id.chapspinner);
        ChapAdapt ch = new ChapAdapt( book, mContext);
              sp.setAdapter( ch);
        //convert.setClickable(true);
        convert.setTag( book);
       } else {
        book = (BookHolder)convert.getTag();
       }
       book.text.setText( bkname );
       return convert;  
      }

ChapAdapt 只需分配一个TexTView

【问题讨论】:

    标签: android android-layout onclick spinner android-listview


    【解决方案1】:

    Spinner 本身实现了OnClicklistener,因此它可以弹出并允许您选择相应的项目。选择后Spinner.onClick()应该会被系统调用,所以我想这就是你要找的地方。

    对我来说,您正在尝试做的事情听起来很像 ExpandableListView 的用例 - 也许这也很有趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      相关资源
      最近更新 更多