我正在向ArrayList添加10个元素,并希望使用recycler视图显示列表,但是,当使用适配器显示它时,它只显示第一个元素。
列出班级
public class MusicListName extends Fragment {
LinearLayoutManager ll;
//int[] imagesid=null;
List<String> musicnames=new ArrayList<String>();
public void addData() {
for(int i=0;i<10 ;i++){
musicnames.add(i,"Hello "+i);
//imagesid[i]=R.drawable.icon;
}
Log.d("Printing",String.valueOf(musicnames));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ll=new LinearLayoutManager(getActivity());
RecyclerView recyclerView=(RecyclerView)inflater.inflate(R.layout.fragment_music_list_name,container,false);
// final String path="/sdcard/Music/";
// int i=0;
// File f=new File(path);
// File list[]=f.listFiles();
// for(File ff:list)
// {
// if(!ff.isDirectory()) {
// musicnames.add(ff.getName());
// // imagesid[i]=R.drawable.icon;
// i++;
// }
//
// }
addData();
//View v = inflater.inflate(R.layout.fragment_music_list_name, container, false);
MusicAdapter musicAdapter=new MusicAdapter(musicnames);
//RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.music_list_recycler);
recyclerView.setLayoutManager(ll);
ll.generateDefaultLayoutParams();
recyclerView.setAdapter(musicAdapter);
return recyclerView;
}
}
适配器类
public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.ViewHolder> {
private List<String> mnames;
// private int[] imgIds;
public MusicAdapter(List<String> mnames){
this.mnames=mnames;
//this.imgIds=imgIds;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String itemName=mnames.get(position);
//int imgpos=imgIds[position];
holder.tv.setText(itemName);
//holder.iv.setImageDrawable(imgpos);
}
@Override
public int getItemCount() {
return mnames.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private final TextView tv;
private final ImageView iv;
public ViewHolder(View itemView) {
super(itemView);
tv=(TextView)itemView.findViewById(R.id.mntv);
iv=(ImageView)itemView.findViewById(R.id.aaiv);
}
}
}
recycler xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.hp.musisha.MainActivity"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/music_list_recycler"
android:scrollbars="vertical"
>
</android.support.v7.widget.RecyclerView>
输出 Image of output in emulator
logcat的:
09-04 10:41:01.140 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 9.697ms
09-04 10:41:01.224 2587-2587/com.example.hp.musisha D/Printing: [Hello 0, Hello 1, Hello 2, Hello 3, Hello 4, Hello 5, Hello 6, Hello 7, Hello 8, Hello 9]
09-04 10:41:01.250 2587-2927/com.example.hp.musisha D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 09-04 10:41:01.269 2587: 2587 D/ ]
HostConnection::get() New Host Connection established 0xaff0a380, tid 2587
[ 09-04 10:41:01.374 2587: 2927 D/ ]
HostConnection::get() New Host Connection established 0xaff0a6b0, tid 2927
09-04 10:41:01.385 2587-2927/com.example.hp.musisha I/OpenGLRenderer: Initialized EGL, version 1.4
09-04 10:41:01.661 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 40.377ms
09-04 10:41:02.390 2587-2587/com.example.hp.musisha I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread.
09-04 10:44:23.263 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 10.201ms
09-04 10:46:07.519 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 5.111ms
答案 0 :(得分:0)
尝试将LinearLayoutManager更改为:
ll = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
并且不要忘记在row.xml中,将高度更改为&#34; wrap_content&#34;
答案 1 :(得分:0)
更改
RecyclerView recyclerView=(RecyclerView)inflater.inflate(R.layout.fragment_music_list_name,container,false);
到
RecyclerView recyclerView=(RecyclerView)inflater.inflate(R.layout.fragment_music_list_name,null,false);
另外,为什么使用generateDefaultLayoutParams()
。没有必要。