如何设置 ListFragment 的 ListView 样式 [英] How can style the ListView of a ListFragment
问题描述
我想保留这些方法:
setListShown(true);
setListShownNoAnimation(true);
但是如果我使用
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
用自定义样式的布局来膨胀Fragment
,之前的方法无法使用,并显示此异常:
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?
Fragment
使用加载器从数据库中填充其 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
源制作我自己的代码,并将进度小部件添加到布局中以显示相同的效果.到现在为止,我会按照我的指示删除这些行.如果我做了修改,我会把它贴在这里.
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
替换为 onCreateView()
中的 CustomListView
布局代码>.为我工作.
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屋!