【问题标题】:No adapter attached; skipping layout ,while displaying a list using recycler VIew未连接适配器;跳过布局,同时使用回收器视图显示列表
【发布时间】:2018-03-02 20:41:30
【问题描述】:

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

注意:我正在使用选项卡布局,其中有三个选项卡和每个选项卡内的一个片段。我正在尝试在其中一个片段中显示回收站视图列表。

这是我的适配器类

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

片段布局:

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

SongData 类:

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

}

这是 RecyclerView 项目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/AlbumImage"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginEnd="10dp"
                android:src="@drawable/round"
                app:civ_border_color="#d35400"
                app:civ_border_width="4dp" />

            <TextView
                android:id="@+id/Title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="30dp"
                android:layout_toEndOf="@id/AlbumImage"
                android:text="TextView"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/Artist"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/Title"
                android:layout_centerHorizontal="true"
                android:layout_toEndOf="@id/AlbumImage"
                android:text="TextView" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>


</LinearLayout>

【问题讨论】:

  • 你recyclerview没有显示吗??有物品??
  • 它只是显示空白
  • 如果您将其他视图添加到您的片段布局中,您是否能够看到任何内容?比如只添加一个显示“这里”的文本视图。
  • Yaa...我添加了一个按钮...它很好地显示了按钮
  • 您可以尝试将RecyclerView 的宽度设置为match_parent,将其高度设置为wrap_content

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


【解决方案1】:

我将您的适配器和 recyclerview 代码粘贴到我的 android 工作室中。而且我在您的列表填充代码中没有发现任何错误。这是您的列表的外观。

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

【讨论】:

  • 那太好了,所以如果你喜欢,请将我的答案标记为正确,也请投票
猜你喜欢
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多