首先,我在1.6(甜甜圈)上使用的是安卓兼容库V4 rev.3。
当您第一次创建ListFragment时,它会显示一个不确定的进度指示器,直到您使用setListAdabpter()。根据ListFragment.onCreateView的文档,如果我想使用自定义布局:
链接如果您使用自己的自定义内容覆盖此方法,请考虑在版面文件中包含标准版面{@
android.R.layout#list_content},以便继续保留ListFragment的所有标准行为。特别是,这是当前显示内置的不确定进度状态的唯一方法。
问题是,当我进入布局文件并尝试:<include layout="@android:layout/list_content" ...>
it (eclipse)时,它告诉我
资源不是公共的。(在'layout‘,值为'@android:layout/list_content')。
我认为这意味着我的V4源代码中不存在list_content.xml
。这是正确的,因为根据文档,它出现在v11接口中。
我只是假设它会存在于兼容库源中,不知道它是否存在...
我能想到的唯一的解决方案就是挖掘android的API V11源代码,然后复制并粘贴list_content.xml
。但是,现在我想避免这种黑客攻击。这是唯一的解决方案吗?
发布于 2012-03-17 05:38:12
兼容性库中包含的ListFragment
似乎并不像真正的布局(list_content)那样使用该布局。这可能是因为它是作为jar提供的,并且不可能打包资源。下面是它的onCreateView
的内容
兼容库中的ListFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = getActivity();
FrameLayout root = new FrameLayout(context);
// ------------------------------------------------------------------
LinearLayout pframe = new LinearLayout(context);
pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
pframe.setOrientation(LinearLayout.VERTICAL);
pframe.setVisibility(View.GONE);
pframe.setGravity(Gravity.CENTER);
ProgressBar progress = new ProgressBar(context, null,
android.R.attr.progressBarStyleLarge);
pframe.addView(progress, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
root.addView(pframe, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// ------------------------------------------------------------------
FrameLayout lframe = new FrameLayout(context);
lframe.setId(INTERNAL_LIST_CONTAINER_ID);
TextView tv = new TextView(getActivity());
tv.setId(INTERNAL_EMPTY_ID);
tv.setGravity(Gravity.CENTER);
lframe.addView(tv, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
ListView lv = new ListView(getActivity());
lv.setId(android.R.id.list);
lv.setDrawSelectorOnTop(false);
lframe.addView(lv, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
root.addView(lframe, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// ------------------------------------------------------------------
root.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return root;
}
来自Android4.0的ListFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(com.android.internal.R.layout.list_content, container, false);
}
如果要在从APIv11源代码复制布局文件和包含此视图初始化代码之间进行选择,我认为很清楚哪一个会被认为是“黑客”;)
发布于 2013-10-24 07:34:45
我很沮丧地看到list_content布局不能使用支持库。我的方法很简单,就是重用ListFragment的默认onCreateView方法,并将其添加到我的自定义布局中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View listContent = super.onCreateView(inflater, container,
savedInstanceState);
View v = inflater.inflate(R.layout.my_custom_layout, container,
false);
FrameLayout listContainer = (FrameLayout) v.findViewById(R.id.my_list_container);
listContainer.addView(listContent);
return v;
}
我在以前自定义布局中的ListView所在的位置添加了一个FrameLayout。
发布于 2012-04-19 23:38:50
在使用兼容库时,将default @android.R.layout/list_content合并到自定义布局中的一种可能的解决方法是创建一个虚拟ListFragment:
package com.example.app;
import android.support.v4.app.ListFragment;
public class ListContentFragment extends ListFragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Supress default list and empty text behavior
//super.onViewCreated(view, savedInstanceState);
}
}
然后将此片段(而不是推荐的list_content布局)包含到您的自定义布局中:
...
<fragment class="com.example.app.ListContentFragment"
android:layout_weight ="1"
android:layout_width ="fill_parent"
android:layout_height ="0dip" />
...
这样,您将获得一个全功能的ListFragment默认行为,同时仍然拥有它的自定义布局。
https://stackoverflow.com/questions/7863074
复制相似问题