如何在 ListView 的末尾添加空格? [英] How can I add blank space to the end of a ListView?

查看:23
本文介绍了如何在 ListView 的末尾添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 方法将该视图添加到列表底部(还有一个 addHeaderView())

测量屏幕高度的代码

 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);显示 display = wm.getDefaultDisplay();int screenHeight = display.getHeight();

设置半屏高度的代码:

 View layout = inflater.inflate(R.layout.mylistviewfooter, container, false);ViewGroup.LayoutParams lp = layout.getLayoutParams();lp.height = screenHeight/2;layout.setLayoutParams(lp);myListView.addFooterView(layout);

旁白:当您向任何列表视图添加页脚或页眉视图时,必须在添加适配器之前完成.此外,如果您需要在执行此操作后获取您的适配器类,您将需要知道通过 getAdapter() 调用列表视图的适配器将返回一个 HeaderViewListAdapter 您需要在其中调用它的 getWrappedAdapter 方法像这样:

 MyAdapterClassInstance myAdapter = (MyAdapterClassInstance) ((HeaderViewListAdapter) myListView.getAdapter()).getWrappedAdapter();

I have a number of elements in a ListView that scroll off the screen.

I would like there to be blank space at the end of the View. That is, the user should be able to scroll past the last element such that the last element is in the middle of the viewport.

I could use an OverScroller, but I think that would only enable the View to have a bouncy effect like one often sees on the iPhone.

Is there something I might have overlooked?

The scrolled-to-the-botton screen should look something like this:

解决方案

  1. Inflate any layout of your choice (this could be an XML of and ImageView with no drawable and with set height and width of your choice)
  2. Measure the screen height and create new LayoutParams and set the height of it to 1/2 of the screen height
  3. Set the new layout params on your inflated view
  4. Use the ListView's addFooterView() method to add that view to the bottom of your list (there is also an addHeaderView())

Code to measure screen height

 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
 Display display = wm.getDefaultDisplay();
 int screenHeight = display.getHeight();

Code to set half screen height:

 View layout = inflater.inflate(R.layout.mylistviewfooter, container, false);
 ViewGroup.LayoutParams lp = layout.getLayoutParams();
 lp.height = screenHeight/2;
 layout.setLayoutParams(lp);
 myListView.addFooterView(layout);

An Aside: When you add a footer or header view to any listview, it has to be done before adding the adapter. Also, if you need to get your adapter class after doing this you will need to know calling the listview's adapter by getAdapter() will return an instance of HeaderViewListAdapter in which you will need to call its getWrappedAdapter method Something like this :

 MyAdapterClassInstance myAdapter = (MyAdapterClassInstance) ((HeaderViewListAdapter) myListView.getAdapter()).getWrappedAdapter();

这篇关于如何在 ListView 的末尾添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆