没有适配器;跳过布局,同时使用Recycler VIew显示列表

时间:2018-03-02 20:41:31

标签: java android android-layout android-fragments android-recyclerview

我知道之前曾问过这个问题,但即使在寻找所有答案之后 我找不到任何相关的东西。我基本上是尝试使用recycler视图显示片段内的项目列表,但它说没有附加适配器;即使我已经连接了适配器,也会跳过布局。

注意:我使用的是Tab Layout,其中每个Tab中有三个选项卡和一个片段。我试图在其中一个片段中显示回收者视图列表。

这是我的适配器类

docker inspect --format='{{index .RepoDigests 0}}' mongo:3.4-jessie

片段代码:

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder> {

ArrayList<SongData> songList;

    public SongAdapter(ArrayList songList) {
        this.songList = songList;
    }


    //Getting hold of each item of the RecyclerView
    public static class SongViewHolder extends RecyclerView.ViewHolder {

        ImageView albumArt;
        TextView Title,Artist;

        public SongViewHolder(View itemView) {
            super(itemView);

            albumArt = (ImageView)itemView.findViewById(R.id.AlbumImage);
            Title= (TextView)itemView.findViewById(R.id.Title);
            Artist = (TextView)itemView.findViewById(R.id.Artist);
        }
    }



    @Override
    public SongViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_tiles,parent,false);
       SongViewHolder svh= new SongViewHolder(view);

        return svh;
    }

    @Override
    public void onBindViewHolder(SongViewHolder holder, int position) {

        SongData songData = songList.get(position);

        holder.Title.setText(songData.Title);
        holder.Artist.setText(songData.Artist);
        holder.albumArt.setImageResource(songData.ImageId);
    }

    @Override
    public int getItemCount() {

        return songList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

片段布局:

public class musicFrag extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;


    public musicFrag() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static musicFrag newInstance(String param1, String param2) {
        musicFrag fragment = new musicFrag();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       View view = inflater.inflate(R.layout.fragment_music, container, false);
        //Initializing RecyclerView , SongAdapter, LinerLayoutManager
        SongData songData =new SongData();
        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager( new LinearLayoutManager(getActivity()));
        SongAdapter songAdapter = new SongAdapter(songData.InitializeData());

        recyclerView.setAdapter(songAdapter);


        return view;


    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

SongData Class:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dannproductions.musify.MainActivity$PlaceholderFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

以下是 RecyclerView项目布局

public class SongData {

    String Title,Artist;
    int ImageId;

    public SongData() {
    };

    public SongData(String title, String Artist, int ImageId) {
        this.Title = Title;
        this.Artist = Artist;
        this.ImageId = ImageId;
    }



    ArrayList<SongData> songList;

    public ArrayList<SongData> InitializeData(){

        songList = new ArrayList<SongData>();

        songList.add(new SongData("Aman","Chatterjee",R.drawable.round));
        songList.add(new SongData("Aman","Chatterjee",R.drawable.round));
        songList.add(new SongData("Aman","Chatterjee",R.drawable.round));
        songList.add(new SongData("Aman","Chatterjee",R.drawable.round));
        songList.add(new SongData("Aman","Chatterjee",R.drawable.round));


     return songList;
    }

}

1 个答案:

答案 0 :(得分:0)

我在你的android工作室粘贴了你的适配器和recyclerview代码。我发现你的清单填写代码没有错误。以下是您的列表的外观。

如果您遇到问题,请仔细检查您的tablayout和片段代码。

enter image description here