我正在尝试制作包含ListView
可滚动的整个布局。也就是说,整个布局应该拥有滚动条而不仅仅是ListView
。我正在尝试使用commonsguy的MergeAdapter
(https://github.com/commonsguy/cwac-merge),但我认为我没有正确实现它,因为我收到以下错误:
07-07 11:05:20.431: E/AndroidRuntime(24474): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.ListView.setupChild(ListView.java:1872)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.ListView.makeAndAddView(ListView.java:1841)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.ListView.fillDown(ListView.java:681)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.ListView.fillFromTop(ListView.java:742)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.ListView.layoutChildren(ListView.java:1659)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.AbsListView.onLayout(AbsListView.java:2431)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:690)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.View.layout(View.java:14482)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewGroup.layout(ViewGroup.java:4469)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2151)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1970)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1183)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4863)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.os.Handler.handleCallback(Handler.java:725)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.os.Looper.loop(Looper.java:137)
07-07 11:05:20.431: E/AndroidRuntime(24474): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-07 11:05:20.431: E/AndroidRuntime(24474): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 11:05:20.431: E/AndroidRuntime(24474): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 11:05:20.431: E/AndroidRuntime(24474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-07 11:05:20.431: E/AndroidRuntime(24474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-07 11:05:20.431: E/AndroidRuntime(24474): at dalvik.system.NativeStart.main(Native Method)
这是班级:
public class HomeFragment extends Fragment implements Navigable {
private ListView mHomeList;
private String[] mHomeListStrings = {"lorem","ipsum","loremm","ipsumm"};
private Integer[] mImageIds = {null, null, null, null};
private MergeAdapter mMergeAdapter;
public HomeFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
//Set string spannable
String s= "Name\nBusiness";
SpannableString ss1= new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(1.5f), 0,4, 0); // set size
TextView tv= (TextView) rootView.findViewById(R.id.home_header);
tv.setText(ss1);
//Set up list view
mHomeList = (ListView) rootView.findViewById(R.id.home_list);
ListAdapter adapter = new ListAdapter(getActivity(), mHomeListStrings, mImageIds);
//I'm doing something wrong either here, or with my xml layout (I think)
mMergeAdapter = new MergeAdapter();
mMergeAdapter.addView(rootView.findViewById(R.id.home_header));
mMergeAdapter.addAdapter(adapter);
mMergeAdapter.addView(rootView.findViewById(R.id.apply_button));
mHomeList.setAdapter(mMergeAdapter);
return rootView;
}
}
我的片段布局如下(我取出了项目属性以缩短它):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xxxx.core.HomeFragment"
android:background="@drawable/bg">
<TextView
android:id="@+id/home_header"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.5">
<ListView
android:id="@+id/home_list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2">
</ListView>
<Button
android:id="@+id/apply_button"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.30"/>
</LinearLayout>
答案 0 :(得分:1)
您应该尝试在页面中添加页眉和页脚,而不是将ListView
放在ScrollView
中。
http://www.vogella.com/tutorials/AndroidListView/article.html#miscellaneous_headerfooter
将列表顶部和列表底部的简单视图放到不同的xml布局中。
只将列表视图放在上面的布局和JAVA中,给这些布局充气并分别添加为页眉和页脚。
如果MergeAdapter
尝试将标题(TextView
)和页脚(Button
)放在不同的xml中并对其进行充气,而不是使用相同的xml。