如何让一个listview div 自填充剩余高度的空白

50个Android开发技巧(24 处理ListView数据为空的情况)
在移动平台上为用户展示数据的一个常用方法是将数据填充进一个List内,而此时需要注意的一点就是:
原文地址:(http://blog.csdn.net/vector_yi/article/details/)
如何处理需要填充的数据为空的情况?
ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View)。
当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。
举个栗子,假设我们需要创建一个应用来管理我们的待办事项,我们的主页面将会是一个用来展示这些待办事项的ListView。
而当我们第一次载入进这个应用时,待办事项必然为空。此时我们就可以利用一个图片或者一段描述性的话来表达“无待办事项”。
看看XML布局文件:
&frameLayout xmlns:android = "/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:orientation= "vertical" &
&/frameLayout&
再来看自定义的drawable/empty_view文件:
是一个自定义的shape,当ListView没数据的时候才展现出来。
最后再看MainActivity文件:
public class MainActivity extends Activity {
private ListView mListV
public void onCreate (Bundle savedInstanceState ) {
super. onCreate( savedInstanceState );
setContentView (R .layout .main );
mListView = (ListView ) findViewById (R .id .my_list_view );
mListView. setEmptyView (findViewById (R .id .empty_view ));
/*String[] strs=new String[]{"1","2"};
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,strs);
mListView.setAdapter(adapter);*/
仅仅创建一个ListView并设置了EmptyView为main.xml中创建的ImageView。注释内的代码用来测试当ListView有数据时,emptyview会不会显示。
当然,你可以利用ViewStub来作为EmptyView,利用ViewStub可以延迟加载视图,确保在不需要显示EmptyView的时候它不会被渲染。关于ViewStub的用法,我在之前的博文《延迟加载和避免重复渲染》已进行过叙述。
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!Android高手进阶篇-自定义ListView实现底部View自动隐藏和消失的功能 - 推酷
Android高手进阶篇-自定义ListView实现底部View自动隐藏和消失的功能
& 以后每一周会分享Android技术难点,今天主要分享这样一个功能:有这样一个ListView,要求在屏幕底部有一个筛选排序的浮动框:
1、手指下拉隐藏,上滑显示 ;
2、如果没做任何操作,2S之后,要自动显示;
3、滑动到最底部,始终显示。
首先看其效果图:
实现上述效果,其实现原理如下:
&1、在屏幕顶部固定一个BottomView,XML布局最好使用RelativeLayout(底部的BottomView并不是 ListView的footView,这个是和footView独立的,想想为什么?)
&2、然后自定义ListView控件,监听onTouchEvent事件,主要是监听手指下滑和上滑事件,同时实现onScrollListener,监听是否滑动到最底部和最顶部
3、 ListView监听事件中,控制bottomView的显示和隐藏,所以ListView提供一个接口,设置底部bootomView的内容,然后获之后,就可以对bottomView进行控制,同时加上动画效果。
接下来看是如何的具体实现这种效果:
1。底部BottomView的内容如下,这个XML文件的内容是自定义的,根据各项目的内容需求来定义的,我例子中bottom_view.xml:
&?xml version=&1.0& encoding=&UTF-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:id=&@+id/button_layout&
android:layout_width=&fill_parent&
android:layout_height=&50dp&
android:background=&#cbcbcb&
android:gravity=&center_vertical&
android:orientation=&horizontal& &
&Button android:layout_height=&40dp&
android:layout_width=&wrap_content&
android:layout_weight=&1&
android:text=&价格&
&Button android:layout_height=&40dp&
android:layout_width=&wrap_content&
android:layout_weight=&1&
android:text=&好评&
&Button android:layout_height=&40dp&
android:layout_width=&wrap_content&
android:layout_weight=&1&
android:text=&筛选&
&/LinearLayout&
2、main.xml如下:
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
&com.example.BottomFloatListView.BottomFloatListView
android:id=&@+id/listView&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:fadingEdge=&none&
android:id=&@+id/bottombar&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_alignParentBottom=&true&
layout=&@layout/bottom_view&
&/include&
&/RelativeLayout&
main.xml中采用的RelativeLayout,有自定义的控件BottomFloatListView,然后屏幕底部放的是bottom_view,我是通过include的方式引用过来的,这样多处引用比较方便。
3、自定义ListView控件BottomFloatListView代码如下
package com.example.BottomFloatListV
import android.content.C
import android.os.H
import android.util.AttributeS
import android.util.L
import android.view.MotionE
import android.view.V
import android.view.ViewG
import android.view.animation.A
import android.view.animation.OvershootI
import android.view.animation.TranslateA
import android.widget.*;
import android.widget.AbsListView.OnScrollL
* 底部View自动隐藏和消失listview(其他ListView可以继承该类,如CtripBottomRefreshListView类等)
* @author zhiwen.nan
下午3:35:15
public class BottomFloatListView extends ListView implements OnScrollListener {
public View mBottomB
private int mCurrentScrollS
private boolean bIsMoved =
private boolean bIsDown =
private int mDeltaY;
private float mMotionY;
private int oldFirstVisibleItem = 0;
private Handler mHandler = new Handler();
private static final String TAG = &BottomFloatListView&;
public BottomFloatListView(Context context) {
this(context, null);
super.setOnScrollListener(this);
public BottomFloatListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
super.setOnScrollListener(this);
public BottomFloatListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setOnScrollListener(this);
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
showBottomViewOnBottom(visibleItemCount, totalItemCount, firstVisibleItem);
public void onScrollStateChanged(AbsListView view, int scrollState) {
hideBottomViewOnScrollStateChanged(view, scrollState);
public boolean onTouchEvent(MotionEvent ev) {
float y = ev.getY();
float x = ev.getX();
Log.d(&FloatListView&, &onTouchEvent& + && + x + && + y);
int action = ev.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
action_down(y);
case MotionEvent.ACTION_MOVE:
mDeltaY = (int) (y - mMotionY);
bIsMoved =
//移动的时候,要移除掉显示bottomView的消息
mHandler.removeCallbacks(showBottomBarRunnable);
//补齐action_down事件,因为有的时候,action_down 事件没有执行
action_down(y);
case MotionEvent.ACTION_UP:
bIsMoved =
if (!bIsMoved && !bIsDown) {
// 如果屏幕上什么没做,则过2s之后要显示bottomView
mHandler.postDelayed(showBottomBarRunnable, 2000);
if (mDeltaY & 0) { //下滑影藏
hideBottomBar();
//上滑显示
showBottomBar();
bIsMoved =
return super.onTouchEvent(ev);
private void action_down(float y){
mMotionY =
Log.d(TAG, &action down execed&);
mHandler.removeCallbacks(showBottomBarRunnable);
* 滑动到顶部时,要隐藏bottomView
* @param view
* @param scrollState
private void hideBottomViewOnScrollStateChanged(AbsListView view, int scrollState) {
mCurrentScrollState = scrollS
if(view!=null){
if (view.getFirstVisiblePosition() == 0 && scrollState == SCROLL_STATE_IDLE) {
hideBottomBar();
Log.d(TAG, &hide bottom view&);
* 显示底部浮动栏
public void showBottomBar() {
if (mBottomBar != null && mBottomBar.getVisibility() == View.GONE) {
mBottomBar.setVisibility(View.INVISIBLE);
Animation translateAnimation = new TranslateAnimation(mBottomBar.getLeft(), mBottomBar.getLeft(),30, 0);
translateAnimation.setDuration(300);
translateAnimation.setInterpolator(new OvershootInterpolator(0.6f));
mBottomBar.startAnimation(translateAnimation);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
public void onAnimationRepeat(Animation animation) {
public void onAnimationEnd(Animation animation) {
mBottomBar.setVisibility(View.VISIBLE);
* 隐藏浮动底部栏
private void hideBottomBar() {
if (mBottomBar != null && mBottomBar.getVisibility() == View.VISIBLE) {
Animation translateAnimation = new TranslateAnimation(mBottomBar.getLeft(), mBottomBar.getLeft(), 0, 30);
translateAnimation.setDuration(300);
translateAnimation.setInterpolator(new OvershootInterpolator(0.6f));
mBottomBar.startAnimation(translateAnimation);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
public void onAnimationRepeat(Animation animation) {
public void onAnimationEnd(Animation animation) {
mBottomBar.setVisibility(View.GONE);
* 滑动到底部时直接显示bottomView
* @param visibleItemCount
* @param totalItemCount
* @param firstVisibleItem
private void showBottomViewOnBottom(int visibleItemCount, int totalItemCount, int firstVisibleItem) {
Log.d(TAG, &visible bottem item count:&
+ &firstVisibleItem:& +
firstVisibleItem + &oldFirstVisibleItem:& + oldFirstVisibleItem + mBottomBar);
if(getLastVisiblePosition() ==
totalItemCount -1 && mCurrentScrollState != SCROLL_STATE_IDLE){
showBottomBar();
private Runnable showBottomBarRunnable = new Runnable() {
public void run() {
showBottomBar();
* 将需要隐藏显示的view传入
* @param bottomBar
public void setBottomBar(ViewGroup bottomBar) {
this.mBottomBar = bottomB
上述代码中,重要和难理解的地方,都加了注释,很好理解。
4、主界面测试的Activity,MainActivity代码如下
package com.example.BottomFloatListV
import android.app.A
import android.os.B
import android.view.V
import android.view.ViewG
import android.widget.ArrayA
import java.util.ArrayL
import java.util.L
public class MainActivity extends Activity {
BottomFloatListView mBottomFloatListV
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBottomFloatListView = (BottomFloatListView)findViewById(R.id.listView)
mBottomFloatListView.setAdapter(new ArrayAdapter&String&(this, android.R.layout.simple_expandable_list_item_1,getData()));
ViewGroup bottomView = (ViewGroup)findViewById(R.id.bottombar) ;
mBottomFloatListView.setBottomBar(bottomView);
private List&String& getData(){
List&String& data = new ArrayList&String&();
for(int i = 0; i &100; i++)
data.add(&测试数据& + i);
上述Activity中告诉了如何使用了BottomFloatListView控件,下面两句代码比较关键:
ViewGroup bottomView = (ViewGroup)findViewById(R.id.bottombar) ;
mBottomFloatListView.setBottomBar(bottomView);
将底部的bottomView传入到ListView中,就可以让ListView具有底部View自动隐藏和消失的功能。
&集成到自己项目中,仅需1分钟搞定,有木有,如果有什么疑问,欢迎留言提问&& 。
源代码下载:
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致设置listView中divider左右留空白
设置listView中divider左右留空白
Android开发
listview中divider是用于间隔listview中的条目,主要是为了美观,使界面显得井井有条。
设计的时候,这个分割线也是很重要的一个部分,现在手机屏幕都比较大,通常认为大屏上1px比1dp要美观,小屏上看不出来区别。
开始的时候设计都是用ps画一个1px高的线,设置到listView的divider属性中,这样的好处是灵活性大,但是缺点是需要专门做个图,而且图片至少也得几KB。后期精简的时候,决定采用颜色来替换资源,因为分割线一般都是纯色的。但是这样一来,divider变成了一条线,两边贯穿屏幕,显得并不美观。
将listview设置padding之后,divider不贯穿屏幕了,但是listview的滚动条不贴着屏幕,也不是很好看,考虑将滚动条去掉。但是问题又来了,listview每个item都是有点击效果的,用于反馈用户点击已经触发,这个对提高用户友好度很重要,但是设置了padding之后,点击效果的区域也添加了padding。一般来说这个不太影响,以前的项目也都到此步骤就结束了。
还有一种方式就是listview设置dividerHeight=0,然后自己去Item View去画一条线,这个过于复杂,肯定直接排除掉。
接下来考虑通过xml画图的方式来解决这个问题,一般的xml画的线都是竖着的,所以不行。
这里采用的是layer-list,因为layer-list可以处理几个资源叠加起来的情况,更重要的是可以设置边界,由于divider可以设置宽度,这里只需要画一个横向有padding,然后纯色的item就可以了。
&?xml version=&1.0& encoding=&utf-8&?&
&!--用于listView中作为纯色的divider,左右默认有10dp的空白,高度设置为1px比较美观--&
&layer-list xmlns:android=&/apk/res/android&&
android:drawable=&@color/trans_black_20&
android:left=&10dp&
android:right=&10dp& /&
&/layer-list&
还可以使用inset,如下:
&?xml version=&1.0& encoding=&UTF-8&?&
&inset xmlns:android=&/apk/res/android&
android:insetLeft=&10dp&
android:insetRight=&10dp&
android:drawable=&@color/red&&
使用的时候设置 divider=&@drawable/xxx& , dividerHeight=&1px& 就行了。由于生成的图片本身带padding,而且纯代码的图片占空间很小,完美解决了以上问题。
我的热门文章
即使是一小步也想与你分享设置ListView加载中,空数据,加载数据失败三种状态的显示
来源:open开发经验库
设置listview加载中,空数据,加载数据失败三种状态的显示。
A library for showing different types of layouts when a list view is empty. These layouts can be shown when,
the list is loading
the list has no item to display
an error occured trying to load items
Loading animation is also supported.
Screenshots
Usage
Import the
into your workspace.
Use the imported
for your project.
In the onCreate event of your activity use the following code.
mListAdapter.clear(); EmptyLayout emptyLayout = new EmptyLayout(this, getListView()); 
When you want to show the loading animation, use this code.
mListAdapter.clear(); emptyLayout.showLoading(); 
When you want to show any error, use this code.
mListAdapter.clear(); emptyLayout.showError(); 
When your list doesn't have any item to show, use this code.
mListAdapter.clear(); emptyLayout.showEmpty(); 
Thats all you have to do to use this library. You may want to customize its behavior though.
P.S. Make sure you always clear the list adapter before calling showEmpty, showLoading and showError. The list have to empty after all.
Customization
There are bunch of methods to let you customize this pattern. Use the methods like this.
emptyLayout.setLoadingMessage("Please wait..."); 
Some useful methods are given below
showEmpty
showLoading
showError
setLoadingView
setEmptyView
setErrorView
setLoadingAnimation
setErrorMessage
setLoadingMessage
setEmptyMessage
setEmptyViewButtonClickListener
setLoadingViewButtonClickListener
setErrorViewButtonClickListener
setShowEmptyButton
setShowLoadingButton
setShowErrorButton
and there is more...
项目主页:
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动

我要回帖

更多关于 listview填充剩余空间 的文章

 

随机推荐