我想创建一个音乐列表,但是当我调用listfragment时会发生与previus布局的重叠
这是我的代码MainActivity.java
public class MainActivity extends AppCompatActivity {
Fragment_list fl;
FragmentManager fm;
FragmentTransaction ft;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
btn=(Button)findViewById(R.id.button);
btn.setText("OK");
fl=new Fragment_list();
fm=getSupportFragmentManager();
ft=fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.Contenedor,fl);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
这是listfragment代码:
public class Fragment_list extends ListFragment {
MediaPlayer player;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStatus)
{
File scan= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File [] list=scan.listFiles();
ArrayAdapter<File>adapter=new ArrayAdapter<File>(getContext(),android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
return super.onCreateView(inflater,container,savedInstanceStatus);
}
@Override
public void onListItemClick(ListView l,View v,int position,long id)
{
Toast.makeText(getActivity(),"Posicion"+getListAdapter().getItem(position),Toast.LENGTH_SHORT).show();
player= MediaPlayer.create(getActivity(), Uri.parse(String.valueOf(getListAdapter().getItem(position))));
player.setLooping(true);
player.start();
}
}
我只想要一个可以使用addToBack的Listfragment,如果我使用FragmentTransition发生重叠但addToBack有效,如果我使用xml片段没有重叠但我不知道如何使用addToBack