gpt4 book ai didi

java - 如何将ListView添加到fragment_main.xml

转载 作者:行者123 更新时间:2023-12-01 11:21:39 25 4
gpt4 key购买 nike

我知道这是一个简单的问题。我也会因为这个问题而失去声誉。但我正在努力解决这个问题。我用 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);,您实际上是在查找 ListView在 activity_main.xml 的 View 层次结构中,但它不在那里。

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

@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 布局的任何位置。一种方法是在您的 ActivityonCreate 方法中执行类似的操作,因为您似乎已经有了一个容器:

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

关于java - 如何将ListView添加到fragment_main.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com