原文链接:http://www.cnblogs.com/smyhvae/archive/2016/08/26/5810471.html

ListView的模板写法

ListView模板写法的完整代码:

•android代码优化----ListView中自定义adapter的封装(ListView的模板写法)

以后每写一个ListView,就这么做:直接导入ViewHolder.java和ListViewAdapter,然后写一个自定义adapter继承自ListViewAdapter就行了。

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

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

例如,先加载footer布局:

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

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

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

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

footer的正确使用方法如下:

1、方法一:

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 layout_footer_listview.xml:(完整版代码) <?xml version= "1.0" encoding= "utf-8" ?> 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" > android:id= "@+id/mFooter" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:gravity= "center" > 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" />

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

?
1 2 3 4 5 6 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)

?
1 mFooter.setVisibility(View.GONE);

2、方法二:

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

?
1 2 3 4 5 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)

?
1 mFooter.setVisibility(View.GONE);

以上所述是小编给大家介绍的Android ListView中动态显示和隐藏Header&Footer的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多相关文章

  1. Android学习笔记:布局
  2. Flutter的一些冷门操作
  3. Android中Activity常用功能设置小结(包括全屏、横竖屏等)
  4. 解读Android之HttpURLConnection
  5. Android(安卓)Google Map设置自动zoom scale
  6. 安卓开发过程中遇到的问题总结及解决方法
  7. android 启动白屏问题完美解决
  8. Android菜鸟日记20 - ListView
  9. ToolBar随ScrollView的滚动改变透明度

随机推荐

  1. Android(安卓)UI 测试框架Espresso详解
  2. Android时间转换星期 昨天 今天工具
  3. [RK3288]串口开发之JNI环境搭建(基于andro
  4. Android--Animation动画介绍和实现
  5. Android(安卓)使用RecyclerView实现轮播
  6. Android笔记:定时提醒、闹钟实现
  7. Android之Handler与多线程
  8. Android中8个常用的操作
  9. android 数据储存——ContentProvider(4)
  10. Android(安卓)自定义View——拖动选择时