如何将 ListView 添加到 fragment_main.xml
How to add ListView to fragment_main.xml
我知道这是个简单的问题。我也会因为这个问题而失去声誉。但我正在努力解决这个问题。我用 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); 不包含 ListView 而引发 NullPointerException。但是问题是如何在不丢失NavigationDrawerLayout的情况下实现Listview。我知道这可能与以前的一些帖子重复。但是我不明白如何实现它。
我尝试将 ContentView 设置为 fragment_main,但没有成功。
参考 - Adding elements to fragment_main.xml does noting
songView
将始终为 null,因为通过在 Activity 中执行 songView = (ListView) findViewById(R.id.song_list);
,您实际上是在 activity_main.xml
的视图层次结构中寻找 ListView,它是不在那里。
为了解决这个问题,您需要将 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
添加到 Activity 的布局中。这样做的一种方法,因为您似乎已经有了一个容器,所以在 Activity
:
的 onCreate
方法中执行类似的操作
FragmentManager fragmentManager = getSupportFragmentManager();
// This is assuming you're using classes from teh support lib, of course.
// Retrieve the appropriate FragmentManager according to the super class of your Activity
fragmentManager.beginTransaction().add(R.id.container, new MainFragment());
// MainFragment is the fragment that contains the ListView
我知道这是个简单的问题。我也会因为这个问题而失去声誉。但我正在努力解决这个问题。我用 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); 不包含 ListView 而引发 NullPointerException。但是问题是如何在不丢失NavigationDrawerLayout的情况下实现Listview。我知道这可能与以前的一些帖子重复。但是我不明白如何实现它。
我尝试将 ContentView 设置为 fragment_main,但没有成功。
参考 - Adding elements to fragment_main.xml does noting
songView
将始终为 null,因为通过在 Activity 中执行 songView = (ListView) findViewById(R.id.song_list);
,您实际上是在 activity_main.xml
的视图层次结构中寻找 ListView,它是不在那里。
为了解决这个问题,您需要将 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
添加到 Activity 的布局中。这样做的一种方法,因为您似乎已经有了一个容器,所以在 Activity
:
onCreate
方法中执行类似的操作
FragmentManager fragmentManager = getSupportFragmentManager();
// This is assuming you're using classes from teh support lib, of course.
// Retrieve the appropriate FragmentManager according to the super class of your Activity
fragmentManager.beginTransaction().add(R.id.container, new MainFragment());
// MainFragment is the fragment that contains the ListView