0

有没有办法在不将类扩展到“ListFragment”的情况下做到这一点?我不想使用 Listfragments,因为那样我将不得不更改 myviewpager 适配器..但如果别无选择,我将不得不:( .我也尝试使用 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);

他们是否有任何并行代码可以用于列表片段?

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));


            }
        }
    }

}

        */
4

2 回答 2

0

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

于 2013-08-01T08:33:42.990 回答
0

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

--edit 所以而不是:

       //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;

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

于 2013-08-01T08:54:43.253 回答