如何将自定义列表视图放在片段内?片段是视图寻呼机的一部分

时间:2013-08-01 08:22:24

标签: android android-fragments android-viewpager android-listfragment

有没有办法这样做而不将类扩展到“ListFragment”?我不想使用Listfragments因为那时我将不得不改变myviewpager适配器..但如果没有选择剩下我会有to :(。我也尝试使用Listfragments,但我无法将listfragments作为viewpager的一部分,换句话说,我无法在viewpager中填充Listfragments,

这只是显示我如何填充片段的代码的一部分

   List<Fragment> fragments = new Vector<Fragment>();
            fragments.add(Fragment.instantiate(this, FragmentSongs.class.getName()));
            fragments.add(Fragment.instantiate(this, FragmentArtists.class.getName()));
            fragments.add(Fragment.instantiate(this, FragmentPlaylists.class.getName()));
            fragments.add(Fragment.instantiate(this, FragmentAlbums.class.getName())); 


            ViewPagerAdapter adapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);


            ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);

是他们的任何并行代码,可以用于listfragments吗?

here is the code:




  package sourcecode.jazzplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

class Mp3Filter implements FilenameFilter{
    public boolean accept(File dir,String name)
    {

    return (name.endsWith(".mp3"))|| (name.endsWith(".Mp3")) ||(name.endsWith(".MP3"));//searching for the files
    }
}
public class FragmentSongs extends  Fragment {

    private static final String Sd_Path=new String("/sdcard/");
    //setting up the source path

    //private MediaPlayer mp =new MediaPlayer();




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //  updatePlaylist() ;
        //updatePlaylist();
        View view = inflater.inflate(R.layout.fragment_a, container, false);
        ListView SngList = (ListView) view.findViewById(R.id.SongList);

         SongDetails Detail = null;
         Context _c = null ;



           ArrayList<SongDetails> Songinfo;
             // AdapterView.AdapterContextMenuInfo info;
              Songinfo = new ArrayList<SongDetails>();

            File home=new File(Sd_Path);
            if(home.listFiles(new Mp3Filter()).length>0)
            {
                for(File file:home.listFiles(new Mp3Filter()))
                {
                 Detail.setSong(file.getName());
                 Detail.setArtist(file.getName());
                 Detail.setAlbum(file.getName());
                 Songinfo.add(Detail);

                 SngList.setAdapter(new CustomAdapter(Songinfo , _c));


                }
            }
        return view;


    } }




    /*private void updatePlaylist()
    {
         SongDetails Detail = null ;
         Context _c = null ;
           ArrayList<SongDetails> Songinfo;
          AdapterView.AdapterContextMenuInfo info;
          Songinfo = new ArrayList<SongDetails>();

        File home=new File(Sd_Path);
        if(home.listFiles(new Mp3Filter()).length>0)
        {
            for(File file:home.listFiles(new Mp3Filter()))
            {
             Detail.setSong(file.getName());
             Detail.setArtist(file.getName());
             Detail.setAlbum(file.getName());
             Songinfo.add(Detail);

             SngList.setAdapter(new CustomAdapter(Songinfo , _c));


            }
        }
    }

}

        */

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你所要做的就是让你的片段在其布局中有一个ListView。

答案 1 :(得分:0)

当然有可能。 如果您不想使用ListFragment,则需要添加自定义ID(android:id =“@ + id / custom_id”),然后您可以使用findViewById查找ListView

- 编辑 所以而不是:

       //ListView   SngList = (ListView)getView().findViewById(R.id.SongList);
    return inflater.inflate(R.layout.fragment_a, container, false);

你应该这样做:

View view = inflater.inflate(R.layout.fragment_a, container, false);
ListView sngList = (ListView) view.findViewById(R.id.SongList);
return view;

这只是我注意到的一件事。