如何将ListView添加到fragment_main.xml

时间:2015-07-01 09:51:09

标签: java android listview fragment navigation-drawer

我知道这是一个简单的问题。而且我也会失去这个问题的声誉。但我正在努力解决这个问题。我用DrawerNavigationLayout创建了android项目。但我很难将简单的ListView添加到布局中。

以下是我的布局文件。

activity_main.xml中

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.andromedatech.musicupv2.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView android:id="@+id/section_label" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#04330000"
        tools:context=".MainActivity" >

        <ListView
            android:id="@+id/song_list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

</RelativeLayout>

这是我的 MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        songView = (ListView) findViewById(R.id.song_list);
        songList = new ArrayList<Song>();

        getSongList();

        Collections.sort(songList, new Comparator<Song>(){
            public int compare(Song a, Song b){
                return a.getTitle().compareTo(b.getTitle());
            }
        });

        SongAdapter songAdt = new SongAdapter(this, songList);
        songView.setAdapter(songAdt);

    }

由于 setContentView(R.layout.activity_main),它会发出NullPointerException; 不包含ListView。但问题是如何实现Listview而不会丢失NavigationDrawerLayout。我理解这可能会复制到以前的一些帖子。但我不明白如何实施它。

我尝试将ConsttentView设置为fragment_main,但它没有成功。

参考 - Adding elements to fragment_main.xml does noting

1 个答案:

答案 0 :(得分:1)

songView将始终为null,因为在您的Activity中执行songView = (ListView) findViewById(R.id.song_list);,您实际上是在activity_main.xml的View层次结构中查找ListView,并且它不在那里。

为了解决这个问题,您需要在Fragment代码中移动ListView的代码。像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_main, container, false);
    songView = (ListView) v.findViewById(R.id.song_list);
    return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    songList = new ArrayList<Song>();

    getSongList();

    Collections.sort(songList, new Comparator<Song>(){
        public int compare(Song a, Song b){
            return a.getTitle().compareTo(b.getTitle());
        }
    });

    // Note that I removed 'this' (referring to the Activity) from the adapter's constructor since we are now in a Fragment.
    //and replaced it with getActivity()
    SongAdapter songAdt = new SongAdapter(getActivity(), songList);
    songView.setAdapter(songAdt);

另一个问题是,您没有在任何地方将Fragment添加到活动的布局中。实现这一目标的一种方法是,因为您似乎已经拥有了容器,就可以在onCreate的{​​{1}}方法中执行类似的操作:

Activity