惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
腾讯CDC
J
Java Code Geeks
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
雷峰网
雷峰网
B
Blog RSS Feed
博客园_首页
量子位
F
Fortinet All Blogs
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog

博客园 - ganzhijie

清除应用缓存 Glide将网络图片压缩成指定大小并保存到本地 Android加固之后Apk重签名 Mac显示和隐藏“隐藏文件”命令 Android Installation failed with message INSTALL_FAILED_TEST_ONLY Could not find any version that matches com.android.support:appcompat-v7:30.+.的解决步骤 ndk{abiFilters:}的使用 Android Studio解决support-annotations版本冲突 解决javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.的问题 如何查看手机cpu是32位还是64位 Android DialogFragment 遇到 java.lang.IllegalStateException: Fragment already added: 的解决方法 DialogFragment异常Fragment already added的原因与解决办法 查看apk安装包的AndroidManifest.xml文件 Android动画效果translate、scale、alpha、rotate Android手机主流屏幕分辨率有哪些? OPhone中的图片特效处理 Android手势(上,下,左和右的判断) Android UI秘笈:谨记该做什么不该做什么 Android permission 访问权限大全
ListView中动态显示和隐藏Header&Footer
ganzhijie · 2019-08-12 · via 博客园 - ganzhijie

例如,先加载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);

(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的时候,设置footer为gone:(不是设置footerParent为gone)

mFooter.setVisibility(View.GONE);