如何风格ListFragment的ListView控件 [英] How can style the ListView of a ListFragment

查看:225
本文介绍了如何风格ListFragment的ListView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持这些方法:

setListShown(true);
setListShownNoAnimation(true);

但如果我用

onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

膨胀的片段与自定义风格的布局,previous方法不能用,并显示此异常:

to inflate the Fragment with a custom styled layout, the previous methods can't be used, and show this exception:

07-30 20:17:46.937: E/AndroidRuntime(1374): Caused by: java.lang.IllegalStateException: Can't be used with a custom content view
07-30 20:17:46.937: E/AndroidRuntime(1374):     at android.support.v4.app.ListFragment.setListShown(ListFragment.java:282)
07-30 20:17:46.937: E/AndroidRuntime(1374):     at android.support.v4.app.ListFragment.setListShown(ListFragment.java:258)

所以,可以有什么可能的解决方案?

So, what possible solution can be?

编辑:

片段使用加载程序来填充其<$​​ C $ C>的ListView 从数据库。所以这就是为什么我想保持这些方法的原因,他们需要在这里:

The Fragment uses a loader to populate its ListView from a database. So that's the reason why I want to keep those methods, they're needed here:

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);  
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

最简单的办法是使基于 ListFragment 源对我自己的code时,还可以添加进度窗口小部件的布局表现出同样的效果。到现在为止,我会删除那些行,因为我一直在说。如果我做了修改我在这里会贴。

The easiest solution would be to make my own code based on the ListFragment source, an also add the progress widget to the layout to show the same effect. By now, I'll delete those lines as I've been told. If I do the modification I'll paste it here.

推荐答案

只需更换原有的的ListView CustomListView 在<$ C $中的布局C> onCreateView()。为我工作。

Just replace the original ListView with your CustomListView Layout within the onCreateView(). Worked for me.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View layout = super.onCreateView(inflater, container,
            savedInstanceState);
    ListView lv = (ListView) layout.findViewById(android.R.id.list);
    ViewGroup parent = (ViewGroup) lv.getParent();

    // Remove ListView and add CustomView  in its place
    int lvIndex = parent.indexOfChild(lv);
    parent.removeViewAt(lvIndex);
    LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
            R.layout.custom_list, container, false);
    parent.addView(mLinearLayout, lvIndex, lv.getLayoutParams());
    return layout;
}

这篇关于如何风格ListFragment的ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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