【问题标题】:Android : How to add a listview inside a scrollview.?Android:如何在滚动视图中添加列表视图。?
【发布时间】:2014-09-04 18:50:20
【问题描述】:

在一个 android 应用程序中,我有一个垂直方向的线性布局。此布局包含 2 个子项(listview 和一个 textview)。问题是布局必须仅在列表视图完成滚动时才显示文本视图。请帮我设计布局?

【问题讨论】:

标签: android listview layout scrollview


【解决方案1】:

您只需要知道列表视图何时位于底部或显示最后一项,然后让您的文本视图可见,这段代码就可以解决问题:

yourListView.setOnScrollListener(this);//The class must implement onscrolllistener

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
                 final int visibleItemCount, final int totalItemCount) {

    switch(lw.getId()) {
        case android.R.id.list:     

            // Make your calculation stuff here. You have all your
            // needed info from the parameters of this function.

            // Sample calculation to determine if the last 
            // item is fully visible.
             final int lastItem = firstVisibleItem + visibleItemCount;
           if(lastItem >= totalItemCount) {
              if(preLast!=lastItem){ //to avoid multiple calls for last item
                Log.d("Last", "Last");
                preLast = lastItem;
                //Make your text view visible
              }
           }
    }
}

同样重要的是要提及“您不应该在 ScrollView 中包含列表视图。”,它违反了 Android 设计指南,如果您采用这种方法,您可能会做一些非常错误的事情。

希望对你有帮助!

问候!

【讨论】:

  • 谢谢马丁。你的解决方案对我有用。我还采纳了您的建议并更改了在滚动视图中包含列表视图的实现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多