[英]No adapter attached; skipping layout ,while displaying a list using recycler VIew
I know this question was asked before but even after looking for all the answer I couldn't find anything relevant. 我知道之前曾问过这个问题,但即使在寻找所有答案后,我也找不到任何相关的内容。 I am basically trying to display a list of items inside a fragment using recycler view, but it saysNo adapter attached;
我基本上是在尝试使用回收站视图显示片段中的项目列表,但是它说没有连接适配器; skipping layout even though I have attached an adapter.
即使安装了适配器,也可以跳过布局。
Note: I am using a Tab Layout where there are three tabs and a fragment inside each Tab. 注意:我使用的是选项卡布局,其中有三个选项卡,每个选项卡中都有一个片段。 I am trying to display the recycler view list in one of the fragment.
我正在尝试在片段之一中显示回收者视图列表。
Here is my Adapter class : 这是我的Adapter类 :
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);
}
}
Fragment code: 片段代码:
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);
}
}
Fragment layout: 片段布局:
<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 Class : 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;
}
}
Here is the RecyclerView item layout : 这是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>
I pasted your adapter and recyclerview code in my android studio. 我在Android Studio中粘贴了您的适配器和recyclerview代码。 and i found no bug in your list filling code.
而且我在您的列表填充代码中没有发现错误。 Here is how your list looks.
这是您列表的外观。
So double check your tablayout and fragment code if you are having some issue. 因此,如果遇到问题,请仔细检查您的布局和片段代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.