最近在做一个小项目 音乐APP播放器,用了不少在Activity中嵌入的Fragment
我想在fragment中使用ListView显示扫描到的本地音乐文件 懵逼了好久,求助....
怎么才能实现这个功能?
PS:在不使用ListFragment的情况下~~
先在layout文件夹下面创建一个fragment的xml布局文件,我建的是fragment_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
然后在自定义的Fragment中进行填充的操作:
package com.example.test2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class MyFragment extends Fragment{
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_listview, null);
lv = (ListView) view.findViewById(R.id.fragment_content);
/*
* 这样布局文件填充到Fragment中,ListView也实例化好了。就可以进行你想要的操作了。
*/
return view;
}
}
xml文件重发一次,这东西有毒,,,,,
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
fragment中显示ListView 和Activity中显示listview 差不多的啊。 你直接把代码贴出来吧。
在fragment 里使用Listview 跟Activity里面的使用没有区别,或者你可以用 RecycleView 代替ListView;
下面是 一个 在Fragment里面使用RecycleView的例子;
http://download.csdn.net/detail/u010623068/9621922
你可以把fragment当作activity一样,然后添加布局,实例化等等一系列操作。