【问题标题】:Custom AdapterView自定义适配器视图
【发布时间】:2012-10-05 02:09:15
【问题描述】:

目标

我想在滚动视图中有一个列表(不是 ListView)

问题

ScrollView 中的ListView 不起作用

可能的解决方案

我认为解决方案如下:

  1. 创建一个扩展 AdapterView 的类
  2. 在 XML 布局中设置类
  3. 用适配器填充adapterView。

解决方案的问题

如果我将扩展的 AdapterView 放在我的 xml 布局中,应用程序就会崩溃。

错误:10-04 16:02:14.396:W/ActivityManager(2119):ActivityRecord{422c1838 package/.activities.SelectWorkplaceActivity}的活动暂停超时

问题

这里出了什么问题?

有没有更好的方法来使用适配器创建不可滚动的列表?

代码

观点:

public class BasicListView extends AdapterView<ListAdapter> {

private String tag = "BasicListView";

private ListAdapter mAdapter;

private DataSetObserver mDataSetObserver;

private Context mContext;

public BasicListView(Context context) {
    super(context);
    mContext = context;

}

public BasicListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
}

public BasicListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
}

@Override
public ListAdapter getAdapter() {
    return mAdapter;
}
@Override
public View getSelectedView() {
    Log.i(tag, "getSelectedView not available");
    return null;
}
@Override
public void setAdapter(ListAdapter adapter) {
    if(mAdapter != null)
    {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }

    mAdapter = adapter; 

    requestLayout();
}

@Override
public void setSelection(int position) {
    Log.i(tag, "setSelection not available");
}
}

还有 XML 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Code scannen" />

        <Button
            android:id="@+id/btn_scan_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="Scan werkplek" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Code handmatig invoeren" />

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


            <EditText
                android:id="@+id/et_type_code"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" >
            </EditText>

            <Button
                android:id="@+id/btn_send_code"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Verzenden" />
        </LinearLayout>

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_current_sessions"
            />

        <package.views.BasicListView
            android:id="@+id/current_sessions"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </package.views.BasicListView>

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="@string/label_favorite_workplaces"
            />

        <ListView
            android:id="@+id/favorite_workplaces"
            android:layout_width="match_parent"
            android:layout_height="193dp"
            />

    </LinearLayout>
</ScrollView>


<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/actionbar" />

如果您想了解更多信息,请询问:)

【问题讨论】:

  • 解决方案:不要将列表视图放在滚动视图中。你不应该这样做,它不应该工作
  • 总是有错误。发布您的 logcat 输出。
  • “我想在滚动视图中有一个列表”不是目标。它是针对您的目标的解决方案的技术视图。这可能是让顶部滚动为列表视图提供更多空间。您可以为此使用列表视图中的标题视图
  • @njzk2 我知道滚动视图中的列表视图不应该工作,这就是我问这个问题的原因。我只想有一个列表(不可滚动)顺便说一句我发现一个错误:10-04 16:02:14.396: W/ActivityManager(2119): ActivityRecord{422c1838 package/.activities.SelectWorkplaceActivity}的活动暂停超时跨度>
  • 你能做到吗?我也在尝试创建一个扩展 AdapterView 的不可滚动列表视图,但我无法让子视图正确膨胀。

标签: android android-adapterview


【解决方案1】:

来自 Romain Guy 的回答解释了为什么您应该为此使用 LinearLayout 而不是 ListView:https://stackoverflow.com/a/3496042/1717225

【讨论】:

  • 感谢您的回答,但在此示例中只有列表视图滚动,而不是容器视图本身。你明白吗?
  • 对不起,我误会了。检查我的新答案。
猜你喜欢
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 2012-02-21
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多