2012-08-02 51 views
6

如何创建一个ListFragment(支持V4库)布局从XML? ListFragment以编程方式设置其布局,因此如果要修改它,则需要混乱添加/删除视图。ListFragment布局从XML

+0

请编辑此所以很明显的问题是什么(发布它作为一个问题),并把下面的解决方案中的答案部分。 – 2012-08-27 12:58:26

+0

你有没有一个可以参考的例子? – sherpya 2012-08-27 15:30:58

+0

该帖子应该只是一个问题和答案的形式。这看起来像一个解决方案,但它不清楚原来的问题是什么答案。 – 2012-08-27 15:56:21

回答

14

我已将创建的布局转换为xml,您可以添加/删除您的小部件或例如添加pulltorefresh解决方案。你不应该改变需要的ID。 由于某些小部件需要内部id,因此需要调用帮助器函数,并且需要支持v4命名空间,因为常量是内部默认可见性。

list_loader.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/progress_container_id" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/list_container_id" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TextView 
      android:id="@+id/empty_id" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" /> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawSelectorOnTop="false" > 
     </ListView> 
    </FrameLayout> 
</FrameLayout> 

android.support.v4.appListFragmentLayout(在你的项目,你并不需要修改支持V4 JAR)

package android.support.v4.app; 

import your.package.R; 
import android.view.View; 

public class ListFragmentLayout 
{ 
    public static void setupIds(View view) 
    { 
     view.findViewById(R.id.empty_id).setId(ListFragment.INTERNAL_EMPTY_ID); 
     view.findViewById(R.id.progress_container_id).setId(ListFragment.INTERNAL_PROGRESS_CONTAINER_ID); 
     view.findViewById(R.id.list_container_id).setId(ListFragment.INTERNAL_LIST_CONTAINER_ID); 
    } 
} 
在扩展 ListFragment

您的片段

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.list_loader, container, false); 
    ListFragmentLayout.setupIds(view); 
    return view; 
} 
+0

你的意思是这个改变它会工作,就好像它不是自定义视图一样? – seb 2012-09-06 23:51:01

+0

你真棒! – seb 2012-09-07 00:06:21

+0

不适用于我。看起来我正在使用旧版本的支持库。它甚至不包含ListFragmentLayout类。我不建议使用这种方法。在未来的支持lib版本中,它可能无法正常工作。 – WindRider 2013-01-31 21:32:24

-2

从androi d SDK(ListFragment)(http://developer.android.com/reference/android/app/ListFragment.html):

公共查看onCreateView(LayoutInflater吹气,ViewGroup中容器,包savedInstanceState)

提供默认实现返回一个简单的列表视图。子类可以覆盖以替换它们自己的布局。如果这样做,返回的视图层次结构必须有一个ListView,其ID是android.R.id.list,并且可以有一个兄弟视图ID android.R.id.empty,当列表为空时,该视图将被显示。

如果您用自己的自定义内容重写此方法,请考虑在布局文件中包含标准布局list_content,以便继续保留ListFragment的所有标准行为。特别是,这是目前唯一能够显示内置的不确定进度状态的方法。

所以,当你想让你自己的layout.xml在FragmentList上覆盖onCreateView并膨胀自己的布局。

例如:

创建一个Android的布局(yourlistlayout.xml),并把下面的XML上要显示你的ListFragment位置。

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
    </ListView> 
ListFragment

把下面的代码:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.yourlistlayout, null);  
    return v;  
} 
+0

由于id不同,这不适用于支持库 – sherpya 2014-04-10 23:03:38