在 Android 中加载动态项目时无法将页脚视图添加到列表视图

Posted

技术标签:

【中文标题】在 Android 中加载动态项目时无法将页脚视图添加到列表视图【英文标题】:Cannot add footer view to listview on loading dynamic items in Android 【发布时间】:2016-05-22 06:21:32 【问题描述】:

我正在开发一个 android 应用程序。在我的应用程序中,我主要使用列表视图加载或添加更多动态数据。我正在使用 volley 通过网络加载数据。我的列表视图在一个片段中。所以不在活动中。我想要做的是,当 listview 正在加载新项目时,我想像 Facebook 和 Twitter 一样显示加载进度条。

所以我尝试在加载数据时添加页脚并删除页脚视图。但是当我在通过网络请求数据时将页脚视图添加到列表视图时会引发错误。

这就是我在 Fragment 中为 listview 页脚初始化加载视图的方式:

.
.
.
.
    private View loadingView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        view = inflater.inflate(R.layout.podcast_list_fragment,container,false);
        loadingView = inflater.inflate(R.layout.listview_loading,container,false);

.
.
.
.

这就是我将数据绑定到列表视图并添加页脚视图的方式

private void updateListItems()
    
        listView.addFooterView(loadingView);
        String url = getResources().getString(R.string.api_endpoint)+"podcast";
        final HashMap<String,String> params = new HashMap<String, String>();
        String queryString = "";
.
.
.

在我将页脚视图添加到列表视图以显示加载进度之前,我的应用程序运行良好。但是我使用了那个函数,它会抛出错误。

这是日志猫

02-11 12:53:52.844 5338-5338/com.blog.waiyanhein.llks.llks D/OpenGLRenderer: Enabling debug mode 0
02-11 12:53:52.844 5338-5338/com.blog.waiyanhein.llks.llks D/AndroidRuntime: Shutting down VM
02-11 12:53:52.844 5338-5338/com.blog.waiyanhein.llks.llks W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa613a908)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime: FATAL EXCEPTION: main
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.ListView.clearRecycledState(ListView.java:515)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.ListView.resetList(ListView.java:502)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.ListView.layoutChildren(ListView.java:1540)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:725)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-11 12:53:52.848 5338-5338/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

我该如何纠正这个问题?

【问题讨论】:

您可以隐藏和显示页脚视图。这是最简单的方法。 它的属性是什么?当我使用单独的视图时,比如进度条,当列表视图变得超过屏幕尺寸时,它就看不到了。 【参考方案1】:
list.setOnScrollListener(new OnScrollListener() 
    @Override
    public void onScrollStateChanged(ListView view, 
            int scrollState) 

    

    @Override
    public void onScroll(ListView view, int firstVisibleItem, 
            int visibleItemCount, int totalItemCount) 

        // threshold being indicator if bottom of list is hit
        if (firstVisibleItem = threshold) 
            load();
        
    
);

private void load() 

    list.addFooterView(loadingFooter);

【讨论】:

当我使用分离视图时,比如进度条,当列表视图超出屏幕尺寸时无法看到 我已经解释过了。当我使用它时它会抛出错误。请问为什么?【参考方案2】:

在您的 onCreate 调用中是这样的:

View footer = LayoutInflater.from(getActivity()).inflate(R.layout.your_fotter, mListView, false);
mListView.addFooterView(footer);

然后在获取数据时更新您的页脚;

看看this 答案也很有帮助。

【讨论】:

我正在使用片段。那么它会在 onCreateView 中吗?【参考方案3】:

我收到了同样的错误信息

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams 无法转换为 android.widget.AbsListView$LayoutParams

当我尝试向ListView 添加页脚时,使用androidx.fragment.app.FragmentContainerView 而不是fragment。此错误仅在 Android 4.3 级别 19 上发生。使用 fragment 而不是 androidx.fragment.app.FragmentContainerView 添加页脚是可以的。如果没有页脚,则不会发生错误。

我通过以下方式解决了这个问题:

onCreateView:创建适配器并在ListView上设置适配器

onViewCreated:创建页脚视图并将其添加到ListView - listView.addFooterView(footer);

【讨论】:

以上是关于在 Android 中加载动态项目时无法将页脚视图添加到列表视图的主要内容,如果未能解决你的问题,请参考以下文章

无法将页脚添加到集合视图

Android将页脚添加到ListView addFooterView()?

在 CI 中加载页眉和页脚视图

即使使用动态高度网站,如何将页脚保持在底部

Vue组件更改时将页脚保留在页面底部

如何将页脚按钮添加到我的回收站视图