ListView中动态显示和隐藏Header&Footer
发布时间:2020-12-14 05:10:16 所属栏目:大数据 来源:网络整理
导读:如果需要动态的显示和隐藏footer的话,按照惯例,误以为直接通过setVisibility中的View.GONE就可以实现。但是在实际使用中发现并不是这样的。 例如,先加载footer布局: private View mFooter; mFooter = LayoutInflater.from( this).inflate(R.layout.foote
如果需要动态的显示和隐藏footer的话,按照惯例,误以为直接通过setVisibility中的View.GONE就可以实现。但是在实际使用中发现并不是这样的。例如,先加载footer布局: private View mFooter; mFooter = LayoutInflater.from(this).inflate(R.layout.footer,null); //加载footer的布局 mListView.addFooterView(mFooter);
如果想动态隐藏这个footer,惯性思维是直接设置footer为gone:(其实这样做是不对的) mFooter.setVisibility(View.GONE); //隐藏footer
实际上,直接设置GONE后,虽然元素是隐藏了,但是还是占用着那个区域,此时和View.INVISIBILE效果一样。 footer的正确使用方法如下: 1、方法一: (1)布局文件:在footer布局文件的最外层再套一层LinearLayout/RelativeLayout,我们称为footerParent。 layout_footer_listview.xml:(完整版代码) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mFooterparent" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:gravity="center" android:orientation="vertical" > <LinearLayout android:id="@+id/mFooter" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:gravity="center" android:text="查看更多" android:textColor="#ff0000" android:textSize="20sp"/> </LinearLayout> </LinearLayout>
(2)加载footer和footerParent的布局: private View mFooter; //footer private View mFooterParent; //footer的最外面再套一层LinearLayout mFooterParent = LayoutInflater.from(getActivity()).inflate(R.layout.footerparent_listview,null);//加载footerParent布局 mFooter = mFooterParent.findViewById(R.id.footer); listView.addFooterView(mFooterParent); //把footerParent放到ListView当中 mFooterParent.setOnClickListener(MainActivity.this); //绑定监听事件,点击查看全部列表
(3)设置footer为gone:(不是设置footerParent为gone) mFooter.setVisibility(View.GONE); ? 2、方法二:或者直接在代码中为footer添加footerParent也可以,如下: private View mFooter; //footer mFooter = LayoutInflater.from(getActivity()).inflate(R.layout.footer_listview,null);//加载footer布局 LinearLayout mFooterParent = new LinearLayout(context); mFooterParent.addView(mFooter);//在footer的最外面再套一层LinearLayout(即footerParent) listView.addFooterView(mFooterParent);//把footerParent放到ListView当中
当需要隐藏footer的时候,设置footer为gone:(不是设置footerParent为gone) mFooter.setVisibility(View.GONE); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ReportStudio入门教程(十五) - 列表显示行号
- inno-setup – Inno Setup – 避免显示子安装程序的文件名
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(
- Learning Perl: 8.7. General Quantifiers
- 如何在Perl 6中将序列作为参数传递?
- lua学习系列一之配置环境与IDE
- delphi – 自定义组件如何处理其子项的事件?
- php – 如果为空,Laravel密码验证在“min length”上失败
- delphi – 有没有办法以编程方式告诉特定的内存块是否没有被
- 用Lua语言编写Wireshark dissector插件