Android 自定义适配器视图
目标 我想在滚动视图中有一个列表(不是列表视图) 问题 ScrollView中的ListView不工作 可能的解决方案 我认为解决办法是做以下几点:Android 自定义适配器视图,android,android-adapterview,Android,Android Adapterview,目标 我想在滚动视图中有一个列表(不是列表视图) 问题 ScrollView中的ListView不工作 可能的解决方案 我认为解决办法是做以下几点: 创建一个扩展AdapterView的类 在XML布局中设置类 用适配器填充适配器视图 解决方案的问题 如果在xml布局中放置扩展AdapterView,应用程序将崩溃 错误:10-04 16:02:14.396:W/ActivityManager(2119):ActivityRecord{422c1838 package/.activities.S
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");
}
}
公共类BasicListView扩展了AdapterView{
私有字符串tag=“BasicListView”;
私有列表适配器mAdapter;
私有DataSetObserver mDataSetObserver;
私有上下文;
公共基本视图(上下文){
超级(上下文);
mContext=上下文;
}
公共基本视图(上下文、属性集属性){
超级(上下文,attrs);
mContext=上下文;
}
public BasicListView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
mContext=上下文;
}
@凌驾
公共ListAdapter getAdapter(){
返回mAdapter;
}
@凌驾
公共视图getSelectedView(){
Log.i(标记“getSelectedView不可用”);
返回null;
}
@凌驾
public void setAdapter(ListAdapter适配器){
如果(mAdapter!=null)
{
mAdapter.unregisteredatasetobserver(mDataSetObserver);
}
mAdapter=适配器;
requestLayout();
}
@凌驾
公共选举(内部职位){
Log.i(标签“不可用”);
}
}
以及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" />
如果您想要更多信息,请询问:)Romain Guy的回答解释了为什么您应该使用线性布局而不是列表视图来实现此目的:解决方案:不要将列表视图放在滚动视图中。你不应该这样做,也不应该这样做——总是有错误的。发布logcat输出。“我想在scrollView中有一个列表”不是目标。它是实现您的目标的解决方案的技术视图。这可能是有顶部滚动给listview更多的空间。您可以在listview中使用headerview来that@njzk2我知道scrollview中的listview不应该工作,这就是我问这个问题的原因。我只想有一个列表(不可滚动),顺便说一句,我发现一个错误:10-04 16:02:14.396:W/ActivityManager(2119):ActivityRecord{422c1838 package/.activities.SelectWorkplaceActivity}的活动暂停超时。您是否能够使此工作正常?我还试图创建一个扩展AdapterView的不可滚动列表视图,但是我在让子视图正确膨胀时遇到了问题。感谢您的回答,但在本例中,只有listview滚动,而不是容器视图本身。你明白吗?对不起,我误解了。检查我的新答案。