在片段的Recycler视图

时间:2016-04-22 06:29:55

标签: android

我正在开发app。我想做的就是,当我点击一个按钮时它会开始片段,而在片段中我创建了一个回收者视图。 Recycler视图将显示设备中的歌曲列表。但问题只是片段显示。没有回收者视图或甚至回收者视图的布局 这是我的片段类,recycleler视图在片段类中:

public class Fragment_song extends Fragment {

//ArrayList<HashMap<String, String>> songs;
private RecyclerView recyclerView;
SongAdapter mAdapter;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.song_layout, container, false);
    recyclerView = (RecyclerView) v.findViewById(R.id.recycler_View);

    // mAdapter = new MoviesAdapter(songs);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    mAdapter=new SongAdapter();
    recyclerView.setAdapter(mAdapter);


    return v;
}


public void SongList() {
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    ContentResolver cr = getActivity().getContentResolver();
    String[] projection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DURATION
    };
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

    Cursor cur = cr.query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            sortOrder);

    mAdapter.songs = new ArrayList<HashMap<String, String>>();
    while (cur.moveToNext()) {
        HashMap<String, String> map = new HashMap<String, String>();
      /*  songs.add(cur.getString(0) + "||"
                + cur.getString(1) + "||"
                + cur.getString(2) + "||"
                + cur.getString(3) + "||"
                + cur.getString(4) + "||" );*/

        map.put("ID", cur.getString(0));
        map.put("artist", cur.getString(1));
        map.put("title", cur.getString(2));
        map.put("displayname", cur.getString(3));
        map.put("duration", cur.getString(4));

        mAdapter.songs.add(map);


    }

}

 }

我的适用于Recycler View的适配器。请在这个..因为在运行时,有时我没有附加适配器。跳过布局。!

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.MyViewHolder> {
ArrayList<HashMap<String, String>> songs=new ArrayList<HashMap<String, String>>() ;





@Override
public SongAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recyclerview, parent, false);

    MyViewHolder viewHolder = new MyViewHolder(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    //Movie movie = moviesList.get(position);
    holder.title.setText(songs.get(position).get("displayname"));
    holder.artist.setText(songs.get(position).get("artist"));
    holder.duration.setText(songs.get(position).get("duration"));
}



@Override
public int getItemCount() {
    return songs.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, duration, artist;


    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        artist = (TextView) view.findViewById(R.id.artist);
        duration = (TextView) view.findViewById(R.id.duration);
    }
}
 }

2 个答案:

答案 0 :(得分:0)

你忘了在onCreateView()

中调用SongList()

答案 1 :(得分:0)

现在,您正在做的是渲染一个列表,其中包含您的适配器中没有元素。 试试这个。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.song_layout, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_View);

// mAdapter = new MoviesAdapter(songs);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter=new SongAdapter();
SongList();
recyclerView.setAdapter(mAdapter);
return v;
}