【问题标题】:How to add ListView to fragment_main.xml如何将 ListView 添加到 fragment_main.xml
【发布时间】:2015-07-01 09:51:09
【问题描述】:

我知道这是一个简单的问题。我也会因为这个问题而失去声誉。但我正在努力解决这个问题。我用 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

【问题讨论】:

  • 如果可以的话,我会投票支持你的问题,但我今天没有票了。希望我的回答能弥补它;)如果有什么需要我澄清的,请告诉我。

标签: java android listview fragment navigation-drawer


【解决方案1】:

songView 将始终为空,因为通过在您的 Activity 中执行 songView = (ListView) findViewById(R.id.song_list);,您实际上是在 activity_main.xml 的 View 层次结构中寻找 ListView,但它不存在。

为了解决这个问题,您需要将 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 添加到任何位置的活动布局中。一种方法是,因为您似乎已经有了一个容器,所以在您的 onCreate 方法中的 Activity 中执行类似的操作:

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

【讨论】:

  • 感谢您的努力。但是我仍然在 super.onActivityCreated(savedInstanceState); 上遇到一个错误
  • @2Dee 这里是songView = (ListView) v.findViewById(R.id.song_list);
  • @MD 不错,你是真正的 MVP,我站得更正了,我用你的更正编辑了答案:)
  • @2Dee +1 值得。你现在在哪里?我没在G+看到你
  • @MD 非常感谢,兄弟! :)
猜你喜欢
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
相关资源
最近更新 更多