簡體   English   中英

使用適配器在android中顯示列表僅顯示第一項

[英]display of a list in android using adapter shows only first item

我向ArrayList添加了10個元素,並希望使用回收者視圖顯示該列表,但是,使用適配器顯示它時,它僅顯示第一個元素。

清單類別

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

        }
    }
}

回收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>

輸出模擬器中輸出的圖像

日志貓:

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

嘗試將LinearLayoutManager更改為:

ll = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);

並且不要忘記在row.xml中將高度更改為“ wrap_content”

更改

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() 不需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM