英文:
android.widget.LinearLayout cannot be cast to android.support.v7.widget.RecyclerView
问题
public class FragmentSongs extends Fragment {
RecyclerView recyclerView;
ArrayList<ItemSong> arrayList;
AdapterSongList adapterSongList;
ZProgressHUD progressHUD;
GridLayoutManager gridLayoutManager;
LinearLayoutManager linearLayoutManager;
TextView textView_empty;
Button button_try;
LinearLayout ll_empty;
String errr_msg;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_songlist, container, false);
progressHUD = ZProgressHUD.getInstance(getActivity());
progressHUD.setMessage(getActivity().getResources().getString(R.string.loading));
progressHUD.setSpinnerType(ZProgressHUD.FADED_ROUND_SPINNER);
arrayList = new ArrayList<>();
recyclerView = rootView.findViewById(R.id.ll_songlist);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
ll_empty = rootView.findViewById(R.id.ll_empty);
textView_empty = rootView.findViewById(R.id.textView_empty_msg);
button_try = rootView.findViewById(R.id.button_empty_try);
loadSongs();
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentManager fm = getFragmentManager();
FragmentSongs f1 = new FragmentSongs();
FragmentTransaction ft = fm.beginTransaction();
Bundle bundl = new Bundle();
bundl.putString("type", getString(R.string.allsong));
bundl.putString("id", arrayList.get(getPosition(adapterSongList.getID(position))).getId());
bundl.putString("name", arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
f1.setArguments(bundl);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.hide(getFragmentManager().findFragmentByTag(getResources().getString(R.string.allsong)));
ft.add(R.id.fragment, f1, arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
ft.addToBackStack(arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
ft.commit();
}
}));
button_try.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadSongs();
}
});
return rootView;
}
private void loadSongs() {
if (JsonUtils.isNetworkAvailable(getActivity())) {
new LoadArtist().execute(Constant.URL_LATEST);
} else {
errr_msg = getString(R.string.internet_not_conn);
setEmpty();
}
}
private class LoadArtist extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
arrayList.clear();
ll_empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
progressHUD.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
String json = JsonUtils.getJSONString(strings[0]);
JSONObject mainJson = new JSONObject(json);
JSONArray jsonArray = mainJson.getJSONArray(Constant.TAG_ROOT);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
String id = objJson.getString(Constant.TAG_ID);
String cid = objJson.getString(Constant.TAG_CAT_ID);
String cname = objJson.getString(Constant.TAG_CAT_NAME);
String artist = objJson.getString(Constant.TAG_ARTIST);
String name = objJson.getString(Constant.TAG_SONG_NAME);
String url = objJson.getString(Constant.TAG_MP3_URL);
String desc = objJson.getString(Constant.TAG_DESC);
String duration = objJson.getString(Constant.TAG_DURATION);
String total_rate = objJson.getString(Constant.TAG_TOTAL_RATE);
String avg_rate = objJson.getString(Constant.TAG_AVG_RATE);
String thumb = objJson.getString(Constant.TAG_THUMB_B).replace(" ", "%20");
String thumb_small = objJson.getString(Constant.TAG_THUMB_S).replace(" ", "%20");
String views = objJson.getString(Constant.TAG_VIEWS);
String downloads = objJson.getString(Constant.TAG_DOWNLOADS);
ItemSong objItem = new ItemSong(id, cid, cname, artist, url, thumb, thumb_small, name, duration, desc, total_rate, avg_rate,views,downloads);
arrayList.add(objItem);
}
return "1";
} catch (JSONException e) {
e.printStackTrace();
return "0";
} catch (Exception ee) {
ee.printStackTrace();
return "0";
}
}
@Override
protected void onPostExecute(String s) {
if (getActivity() != null) {
if (s.equals("1")) {
progressHUD.dismissWithSuccess(getResources().getString(R.string.success));
adapterSongList = new AdapterSongList(getActivity(), arrayList, new RecyclerClickListener() {
@Override
public void onClick(int position) {
if (JsonUtils.isNetworkAvailable(getActivity())) {
} else {
Toast.makeText(getActivity(), getResources().getString(R.string.internet_not_conn), Toast.LENGTH_SHORT).show();
}
}
}, "online");
recyclerView.setAdapter(adapterSongList);
errr_msg = getString(R.string.no_data_found);
} else {
progressHUD.dismissWithFailure(getResources().getString(R.string.error));
errr_msg = getString(R.string.server_no_conn);
}
setEmpty();
super.onPostExecute(s);
}
}
}
public void setEmpty() {
if(arrayList.size() > 0) {
recyclerView.setVisibility(View.VISIBLE);
ll_empty.setVisibility(View.GONE);
} else {
textView_empty.setText(errr_msg);
recyclerView.setVisibility(View.GONE);
ll_empty.setVisibility(View.VISIBLE);
}
}
private int getPosition(String id) {
int count = 0;
for (int i = 0; i < arrayList.size(); i++) {
if (id.equals(arrayList.get(i).getId())) {
count = i;
break;
}
}
return count;
}
}
public class AdapterSongList extends RecyclerView.Adapter<AdapterSongList.MyViewHolder> {
private Context context;
private ArrayList<ItemSong> arrayList;
private ArrayList<ItemSong> filteredArrayList;
private RecyclerClickListener recyclerClickListener;
private NameFilter filter;
private String type;
private JsonUtils jsonUtils;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView_song, textView_duration, textView_catname, textView_total_rate, textView_views, textView_downloads;
EqualizerView equalizer;
ImageView imageView, imageView_option;
LinearLayout linearLayout, ll_counts;
RatingBar ratingBar;
MyViewHolder(View view) {
super(view);
linearLayout = view.findViewById(R.id.ll_songlist);
ll_counts = view.findViewById(R.id.ll_counts);
textView_song = view.findViewById(R.id.textView_songname);
textView_duration = view.findViewById(R.id.textView_songduration);
textView_total_rate = view.findViewById(R.id.textView_totalrate_songlist);
equalizer = view.findViewById(R.id.equalizer_view);
textView_catname = view.findViewById(R.id.textView_catname);
imageView = view.findViewById(R.id.imageView_songlist);
imageView_option = view.findViewById(R.id.imageView_option_songlist);
ratingBar = view.findViewById(R.id.ratingBar_songlist);
textView_views = view.findViewById(R.id.textView_views);
textView_downloads = view.findViewById(R.id.textView_downloads
<details>
<summary>英文:</summary>
im doing paid app customization searched and followed some instruction yet couldnt get the job done. copied the same data from other layout and Java classes to make new one as it explains but conflicted with layout
my fragment:
public class FragmentSongs extends Fragment {
RecyclerView recyclerView;
ArrayList<ItemSong> arrayList;
AdapterSongList adapterSongList;
ZProgressHUD progressHUD;
GridLayoutManager gridLayoutManager;
LinearLayoutManager linearLayoutManager;
TextView textView_empty;
Button button_try;
LinearLayout ll_empty;
String errr_msg;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_songlist, container, false);
progressHUD = ZProgressHUD.getInstance(getActivity());
progressHUD.setMessage(getActivity().getResources().getString(R.string.loading));
progressHUD.setSpinnerType(ZProgressHUD.FADED_ROUND_SPINNER);
arrayList = new ArrayList<>();
recyclerView = rootView.findViewById(R.id.ll_songlist);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
ll_empty = rootView.findViewById(R.id.ll_empty);
textView_empty = rootView.findViewById(R.id.textView_empty_msg);
button_try = rootView.findViewById(R.id.button_empty_try);
loadSongs();
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentManager fm = getFragmentManager();
FragmentSongs f1 = new FragmentSongs();
FragmentTransaction ft = fm.beginTransaction();
Bundle bundl = new Bundle();
bundl.putString("type", getString(R.string.allsong));
bundl.putString("id", arrayList.get(getPosition(adapterSongList.getID(position))).getId());
bundl.putString("name", arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
f1.setArguments(bundl);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.hide(getFragmentManager().findFragmentByTag(getResources().getString(R.string.allsong)));
ft.add(R.id.fragment, f1, arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
ft.addToBackStack(arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
ft.commit();
}
}));
button_try.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadSongs();
}
});
return rootView;
}
private void loadSongs() {
if (JsonUtils.isNetworkAvailable(getActivity())) {
new LoadArtist().execute(Constant.URL_LATEST);
} else {
errr_msg = getString(R.string.internet_not_conn);
setEmpty();
}
}
private class LoadArtist extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
arrayList.clear();
ll_empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
progressHUD.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
String json = JsonUtils.getJSONString(strings[0]);
JSONObject mainJson = new JSONObject(json);
JSONArray jsonArray = mainJson.getJSONArray(Constant.TAG_ROOT);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
String id = objJson.getString(Constant.TAG_ID);
String cid = objJson.getString(Constant.TAG_CAT_ID);
String cname = objJson.getString(Constant.TAG_CAT_NAME);
String artist = objJson.getString(Constant.TAG_ARTIST);
String name = objJson.getString(Constant.TAG_SONG_NAME);
String url = objJson.getString(Constant.TAG_MP3_URL);
String desc = objJson.getString(Constant.TAG_DESC);
String duration = objJson.getString(Constant.TAG_DURATION);
String total_rate = objJson.getString(Constant.TAG_TOTAL_RATE);
String avg_rate = objJson.getString(Constant.TAG_AVG_RATE);
String thumb = objJson.getString(Constant.TAG_THUMB_B).replace(" ", "%20");
String thumb_small = objJson.getString(Constant.TAG_THUMB_S).replace(" ", "%20");
String views = objJson.getString(Constant.TAG_VIEWS);
String downloads = objJson.getString(Constant.TAG_DOWNLOADS);
ItemSong objItem = new ItemSong(id, cid, cname, artist, url, thumb, thumb_small, name, duration, desc, total_rate, avg_rate,views,downloads);
arrayList.add(objItem);
}
return "1";
} catch (JSONException e) {
e.printStackTrace();
return "0";
} catch (Exception ee) {
ee.printStackTrace();
return "0";
}
}
@Override
protected void onPostExecute(String s) {
if (getActivity() != null) {
if (s.equals("1")) {
progressHUD.dismissWithSuccess(getResources().getString(R.string.success));
adapterSongList = new AdapterSongList(getActivity(), arrayList, new RecyclerClickListener() {
@Override
public void onClick(int position) {
if (JsonUtils.isNetworkAvailable(getActivity())) {
} else {
Toast.makeText(getActivity(), getResources().getString(R.string.internet_not_conn), Toast.LENGTH_SHORT).show();
}
}
}, "online");
recyclerView.setAdapter(adapterSongList);
errr_msg = getString(R.string.no_data_found);
} else {
progressHUD.dismissWithFailure(getResources().getString(R.string.error));
errr_msg = getString(R.string.server_no_conn);
}
setEmpty();
super.onPostExecute(s);
}
}
}
public void setEmpty() {
if(arrayList.size() > 0) {
recyclerView.setVisibility(View.VISIBLE);
ll_empty.setVisibility(View.GONE);
} else {
textView_empty.setText(errr_msg);
recyclerView.setVisibility(View.GONE);
ll_empty.setVisibility(View.VISIBLE);
}
}
private int getPosition(String id) {
int count = 0;
for (int i = 0; i < arrayList.size(); i++) {
if (id.equals(arrayList.get(i).getId())) {
count = i;
break;
}
}
return count;
}
and my Adapter
public class AdapterSongList extends RecyclerView.Adapter<AdapterSongList.MyViewHolder> {
private Context context;
private ArrayList<ItemSong> arrayList;
private ArrayList<ItemSong> filteredArrayList;
private RecyclerClickListener recyclerClickListener;
private NameFilter filter;
private String type;
private JsonUtils jsonUtils;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView_song, textView_duration, textView_catname, textView_total_rate, textView_views, textView_downloads;
EqualizerView equalizer;
ImageView imageView, imageView_option;
LinearLayout linearLayout, ll_counts;
RatingBar ratingBar;
MyViewHolder(View view) {
super(view);
linearLayout = view.findViewById(R.id.ll_songlist);
ll_counts = view.findViewById(R.id.ll_counts);
textView_song = view.findViewById(R.id.textView_songname);
textView_duration = view.findViewById(R.id.textView_songduration);
textView_total_rate = view.findViewById(R.id.textView_totalrate_songlist);
equalizer = view.findViewById(R.id.equalizer_view);
textView_catname = view.findViewById(R.id.textView_catname);
imageView = view.findViewById(R.id.imageView_songlist);
imageView_option = view.findViewById(R.id.imageView_option_songlist);
ratingBar = view.findViewById(R.id.ratingBar_songlist);
textView_views = view.findViewById(R.id.textView_views);
textView_downloads = view.findViewById(R.id.textView_downloads);
}
}
public AdapterSongList(Context context, ArrayList<ItemSong> arrayList, RecyclerClickListener recyclerClickListener, String type) {
this.arrayList = arrayList;
this.filteredArrayList = arrayList;
this.context = context;
this.type = type;
this.recyclerClickListener = recyclerClickListener;
jsonUtils = new JsonUtils(context);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_songlist, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
holder.textView_song.setText(arrayList.get(position).getMp3Name());
holder.textView_duration.setText(arrayList.get(position).getDuration());
if (!type.equals("offline")) {
holder.textView_total_rate.setText(arrayList.get(position).getTotalRate());
holder.ratingBar.setRating(Float.parseFloat(arrayList.get(position).getAverageRating()));
holder.textView_views.setText(jsonUtils.format(Double.parseDouble(arrayList.get(position).getViews())));
holder.textView_downloads.setText(jsonUtils.format(Double.parseDouble(arrayList.get(position).getDownloads())));
} else {
holder.textView_total_rate.setVisibility(View.GONE);
holder.ratingBar.setVisibility(View.GONE);
holder.ll_counts.setVisibility(View.GONE);
}
if (!type.equals("offline") || type.equals("fav")) {
Picasso.get()
.load(arrayList.get(position).getImageSmall())
.into(holder.imageView);
} else {
holder.imageView.setImageBitmap(arrayList.get(position).getBitmap());
}
if (Constant.isPlaying && Constant.arrayList_play.get(Constant.playPos).getId().equals(arrayList.get(position).getId())) {
holder.equalizer.animateBars();
holder.equalizer.setVisibility(View.VISIBLE);
} else {
holder.equalizer.stopBars();
holder.equalizer.setVisibility(View.GONE);
}
if (arrayList.get(position).getArtist() != null) {
holder.textView_catname.setText(arrayList.get(position).getArtist());
} else {
holder.textView_catname.setText(arrayList.get(position).getArtist());
}
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recyclerClickListener.onClick(holder.getAdapterPosition());
}
});
holder.imageView_option.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openOptionPopUp(holder.imageView_option, holder.getAdapterPosition());
}
});
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public int getItemCount() {
return arrayList.size();
}
public String getID(int pos) {
return arrayList.get(pos).getId();
}
private void openOptionPopUp(ImageView imageView, final int pos) {
PopupMenu popup = new PopupMenu(context, imageView);
popup.getMenuInflater().inflate(R.menu.popup_song, popup.getMenu());
if (type.equals("offline")) {
popup.getMenu().findItem(R.id.popup_add_song).setTitle(context.getString(R.string.delete));
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.popup_add_song:
if (type.equals("offline")) {
openDeleteDialog(pos);
} else {
jsonUtils.openPlaylists(arrayList.get(pos));
}
break;
}
return true;
}
});
popup.show();
}
private void openDeleteDialog(final int pos) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(context.getString(R.string.delete));
dialog.setMessage(context.getString(R.string.sure_delete));
dialog.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Boolean isDelete = new File(arrayList.get(pos).getMp3Url()).delete();
if (isDelete) {
arrayList.remove(pos);
notifyItemRemoved(pos);
Toast.makeText(context, context.getString(R.string.file_deleted), Toast.LENGTH_SHORT).show();
}
}
});
dialog.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.show();
}
public Filter getFilter() {
if (filter == null) {
filter = new NameFilter();
}
return filter;
}
private class NameFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if (constraint.toString().length() > 0) {
ArrayList<ItemSong> filteredItems = new ArrayList<>();
for (int i = 0, l = filteredArrayList.size(); i < l; i++) {
String nameList = filteredArrayList.get(i).getMp3Name();
if (nameList.toLowerCase().contains(constraint))
filteredItems.add(filteredArrayList.get(i));
}
result.count = filteredItems.size();
result.values = filteredItems;
} else {
synchronized (this) {
result.values = filteredArrayList;
result.count = filteredArrayList.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
arrayList = (ArrayList<ItemSong>) results.values;
notifyDataSetChanged();
}
}
and my XML file lay out which im confused about Recylcer View and Linear Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:background="@color/black40"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_songlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView_songlist"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginEnd="5dp"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textView_songname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="15sp"
android:layout_marginEnd="5dp"/>
<TextView
android:id="@+id/textView_catname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="sdasd"
android:textColor="@color/white80"
android:textSize="10sp"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<RatingBar
android:id="@+id/ratingBar_songlist"
style="@style/RatingBar_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="5"
android:numStars="5"
android:theme="@style/RatingBar_white"/>
<TextView
android:id="@+id/textView_totalrate_songlist"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_marginStart="5dp"
android:background="@drawable/bg_round_white"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:text="32"
android:textColor="@color/black"
android:textSize="10sp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<es.claucookie.miniequalizerlibrary.EqualizerView xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/equalizer_view"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginEnd="7dp"
android:layout_marginStart="7dp"
android:visibility="invisible"
custom:animDuration="3000"
custom:foregroundColor="@color/pink" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginEnd="35dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView_songduration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="12sp"
android:text="asad"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/imageView_option_songlist"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/bar_selector_white"
android:src="@mipmap/more_option" />
<LinearLayout
android:id="@+id/ll_counts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginEnd="15dp"
android:orientation="horizontal"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:gravity="center">
<ImageView
android:layout_width="15dp"
android:layout_height="wrap_content"
android:src="@mipmap/views" />
<TextView
android:id="@+id/textView_views"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:gravity="center"
android:text="25"
android:textColor="@color/white"
android:textSize="10sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:src="@mipmap/download_arrow" />
<TextView
android:id="@+id/textView_downloads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:gravity="center"
android:text="25"
android:textColor="@color/white"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</details>
# 答案1
**得分**: 0
你在`Fragment`和`Adapter`中都在使用相同的布局`R.layout.layout_songlist`。
在`FragmentSongs`的`onCreateView`方法中存在问题:
```java
recyclerView = rootView.findViewById(R.id.ll_songlist);
因为在布局中你使用了:
<LinearLayout
android:id="@+id/ll_songlist"
而你不能将LinearLayout
转换为RecyclerView
。在你的FragmentSongs
中,你应该使用一个带有RecyclerView
的不同布局。
英文:
You are using the same layout R.layout.layout_songlist
in the Fragment
and in the Adapter
for each item.
In the onCreateView
method of the FragmentSongs
the issue is here:
recyclerView = rootView.findViewById(R.id.ll_songlist);
because in the layout you are using:
<LinearLayout
android:id="@+id/ll_songlist"
and you can't cast a LinearLayout
to a RecyclerView
.
In your FragmentSongs
you should use a different layout with a RecyclerView
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论