ListView中动态显示和隐藏Header&Footer

解决思路: 直接设置HeaderView和FooterView.setVisibility(View.GONE)无效, 布局仍然存在, 需要给布局设置父布局, 然后通过控制子布局来显示隐藏布局.

  1. 给最外层布局, 嵌套父布局, 通过控制子布局进而控制整个布局;

  2. 给整个布局在代码中动态添加一个父布局, 然后头尾部添加父布局,可以直接操控该布局;

具体实现如下

话外篇:

  1. 1.什么是阴影效果
  2. 2.fading:渐变,衰退 fadingEdge:渐变边缘,衰退边缘
  3.  
  4. 一、删除android ScrollView边界阴影方法方法
  5. 1 xml中添加:android:fadingEdge=”none
  6. 2 代码中添加:ScrollView.setHorizontalFadingEdgeEnabled(false);
  7.  
  8. 二、删除ScrollView拉到尽头(顶部、底部),然后继续拉出现的阴影效果
  9. 适用于2.3及以上的 否则不用设置
  10. android:overScrollMode="never"

除去ScrollVIew拉到尽头时再拉的阴影效果

如果需要动态的显示和隐藏footer的话,按照惯例,误以为直接通过setVisibility中的View.GONE就可以实现。但是在实际使用中发现并不是这样的。

例如,先加载footer布局:

  1. private View mFooter;
  2.  
  3. mFooter = LayoutInflater.from(this).inflate(R.layout.footer, null); //加载footer的布局
  4. mListView.addFooterView(mFooter);

如果想动态隐藏这个footer,惯性思维是直接设置footer为gone:(其实这样做是不对的)

  1. mFooter.setVisibility(View.GONE); //隐藏footer

实际上,直接设置GONE后,虽然元素是隐藏了,但是还是占用着那个区域,此时和View.INVISIBILE效果一样。

footer的正确使用方法如下:

1、方法一:

(1)布局文件:在footer布局文件的最外层再套一层LinearLayout/RelativeLayout,我们称为footerParent。

layout_footer_listview.xml:(完整版代码)

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:id="@+id/mFooterparent"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:background="#FFFFFF"
  9. android:gravity="center"
  10. android:orientation="vertical"
  11. >
  12.  
  13. <LinearLayout
  14. android:id="@+id/mFooter"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:gravity="center">
  18.  
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="40dp"
  22. android:layout_centerVertical="true"
  23. android:layout_marginLeft="10dp"
  24. android:gravity="center"
  25. android:text="查看更多"
  26. android:textColor="#ff0000"
  27. android:textSize="20sp"/>
  28. </LinearLayout>
  29. </LinearLayout>

(2)加载footer和footerParent的布局:

  1. private View mFooter; //footer
  2. private View mFooterParent; //footer的最外面再套一层LinearLayout
  3.  
  4. mFooterParent = LayoutInflater.from(getActivity()).inflate(R.layout.footerparent_listview, null);//加载footerParent布局
  5. mFooter = mFooterParent.findViewById(R.id.footer);
  6. listView.addFooterView(mFooterParent); //把footerParent放到ListView当中
  7.  
  8. mFooterParent.setOnClickListener(MainActivity.this); //绑定监听事件,点击查看全部列表

(3)设置footer为gone:(不是设置footerParent为gone)

  1. mFooter.setVisibility(View.GONE);

2、方法二:或者直接在代码中为footer添加footerParent也可以,如下:

  1. private View mFooter; //footer
  2. mFooter = LayoutInflater.from(getActivity()).inflate(R.layout.footer_listview, null);//加载footer布局
  3.  
  4. LinearLayout mFooterParent = new LinearLayout(context);
  5. mFooterParent.addView(mFooter);//在footer的最外面再套一层LinearLayout(即footerParent)
  6. listView.addFooterView(mFooterParent);//把footerParent放到ListView当中

当需要隐藏footer的时候,设置footer为gone:(不是设置footerParent为gone)

  1. mFooter.setVisibility(View.GONE);

ListView中动态显示隐藏HeaderView和FooterView的更多相关文章

  1. ListView中动态显示和隐藏Header&Footer

    ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...

  2. ListView设置headerview和footerview

    [简介]headerview就是通常看到的那种listview手势下滑露出上面的部分,下拉到一定位置,松手会开始请求网络数据,然后刷新listview的列表.footerview一般就是listvie ...

  3. [Android Bug] ListView中Header, Footer无法隐藏(gone)的问题

    ListView中Header.Footer View应该是会应该遇到, 比如说,滚动到底部时,自动开始加载: 对于一些应用市场,会在Header中加上ViewFlipper做应用推荐(滚动的那种,好 ...

  4. Android 编程下 ListView 的 HeaderView 和 FooterView 不可选择点击

    在 ListView 里,HeaderView 和 FooterView 也占一行,与其他的 item 一样,可以点击,有索引,HeaderView 的索引为0.如果要使这两项不可点击,可以使用下面的 ...

  5. 【转】listView中,checkBox的显示和隐藏

    原文网址:http://www.cnblogs.com/vicma/p/3460500.html 在listView中,每个item都有一个ChexBox,当显示的时候在listView外面设置一个按 ...

  6. Android 5.X新特性之为RecyclerView添加HeaderView和FooterView

    上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...

  7. Android GridView增加HeaderView和FooterView的实现

    Android GridView增加HeaderView和FooterView的实现 做的项目中遇到一个问题,需要实现一个页面 页面的上面是一个自定义的View和GridView,当向下滚动屏幕的时候 ...

  8. (转载)解决ListView中使用EditText所遇到的一些冲突

    大家都知道在listView中使用editText,在输入过程中是有冲突的.今天稍微研究了一下这个问题,有一点点小小的心得和大家一起分享下. 首先建立一个最简单的demo,主界面就是一个ListVie ...

  9. winform实现listview中combox

    一.概要 因为要在项目中要在ListView中实现下拉框选择,用DataGrid的话,一个不美观,二个绑定数据麻烦,参考网上一种做法,就是单击ListView时,判断单击的区域,然后将Combox控件 ...

随机推荐

  1. SSH技术介绍和Xshell公钥远程登陆

    SSH简介 传统的网络服务程序,比如FTP,POP,Telnet,本质上都是不安全的,因为它们在网络上用明文传送数据.用户账号和用户口令,很容易受到中间人攻击方式的攻击,攻击者会冒充真正的服务器接收用 ...

  2. 第八节:numpy之四则运算与逻辑运算

  3. PAT 1118 Birds in Forest

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  4. 关于wordclou的一些简单操作

    详细讲解一下怎么用python的三方库wordcloud制作一个关于歌曲<Vincent>的歌词,有特别背景的云词效果,如图所示: 首先的先准备好一张背景图,为了云词效果,可以实现修改一下 ...

  5. HDU 5343 MZL's Circle Zhou

    MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...

  6. Solr插件的弊端

    在前文<Solr Update插件自定义条件索引>中,我介绍了如何通过插件的模式,自定义Solr的Update过程.但是在大半年的使用过程中,发现这种方式存在如下弊端. 1.环境难以维护. ...

  7. web项目log日志查看分析->流程理解

    1.DEBUG [2017-07-10 11:38:41,705][] org.springframework.web.servlet.DispatcherServlet:865 - Dispatch ...

  8. Mac下查看文件编码方式

    一句话:file -I {filename}

  9. wait()方法写在while循环中可以在线程接到通知后再一次判断条件

    wait()方法写在while循环中可以在线程接到通知后再一次判断条件 synchronized public String pop() { String returnValue = "&q ...

  10. Chrome development tools学习笔记(3)

    (上次DOM的部分做了些补充,欢迎查看Chrome development tools学习笔记(2)) 利用DevTools Elements工具来调试页面样式 CSS(Cascading Style ...