gpt4 book ai didi

java - 列表 fragment 中的页脚( ListView )

转载 作者:行者123 更新时间:2023-12-01 12:15:39 27 4
gpt4 key购买 nike

我正在尝试将静态页脚添加到 Android 的 ListView 中。我正在使用一个列表 fragment ,一个填充父级的 ListView ,并从主 Activity 加载它。我想向 ListView 添加页脚,因此我创建了一个页脚 xml 文件,但无法从列表 fragment 中加载它。它要么崩溃,要么什么也没有发生。

在列表 fragment 内的 ListView 中加载静态页脚的正确代码是什么?我引用的任何代码 fragment 在粘贴到 listfragment 扩展类中时都不起作用。

当它崩溃时,我收到此错误“viewpager布局参数无法转换为abslistviewlayoutparams”或类似的东西,viewpager是主要父级,每个选项卡都是一个 fragment ,其中一个 fragment 是我正在尝试的 ListView 添加页脚。

<!--This is the fragment with the list in it-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone"
android:layout_centerInParent="true"
/>

<include layout="@android:layout/list_content" />
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent" />

</RelativeLayout>

谢谢。下面的代码我刚刚尝试过,编译并运行,但什么也没做。它在里面onCreateView 方法,它是一个扩展 ListFragment 的类。

     lview = (ListView)view.findViewById(R.id.list);            

//nothing happens using this
lview.addFooterView(
inflater.inflate(R.layout.standalone_footer, null)
);

//crashes with cast exception using this
lview.addFooterView(
inflater.inflate(R.layout.standalone_footer, container, false)
);

//onCreate method, either crashes or runs but nothing happens when tweaking code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_tab1, container, false);

lv = (ListView)view.findViewById(R.id.list);

mItems = new ArrayList<ListViewCustom>();

//add static fixed footer
lv.addFooterView(
inflater.inflate(R.layout.footer, null, false) //also tried passing lv instead of null
);

//initialize and set the list adapter
setListAdapter(new ListViewAdapterCustom(getActivity(), mItems));

return view;
}

独立的页脚 xml(带有 xmlns 标记的 TableLayout 未在此处显示为代码...) 安卓:stretchColumns =“*” android:background="#000">

<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_span="1"
android:layout_weight="1"
android:gravity="center" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_span="1"
android:layout_weight="1"
android:gravity="center" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_span="1"
android:layout_weight="1"
android:gravity="center" />
</TableRow>

<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:layout_span="1"
android:layout_weight="1"
android:gravity="center" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:layout_span="1"
android:layout_weight="1"
android:gravity="center" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:layout_span="1"
android:layout_weight="1"
android:gravity="center" />
</TableRow>

</TableLayout>

PS Listfragment 和实际列表加载并渲染良好,一切正常。我也在使用自定义适配器,如果这很重要的话?

最佳答案

view = (ListView)view.findViewById(R.id.list);
我不明白这部分 - 您的基本 View 称为 view (如您调用 view.findViewById 时所见,但您也在调用 ListView 也是view

在您的 onCreateView() 方法中尝试一下:

ListView lv = (ListView) view.findViewById(R.id.list);
lv.addFooterView(inflater.inflate(R.layout.standalone_footer, null));

或者尝试 getListView().addFooterView(inflater.inflate(R.layout.standalone_footer, null))

关于java - 列表 fragment 中的页脚( ListView ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27024709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com