所以我要在android studio中构建一个RecyclerView,以显示对象列表中的图像和标题。
这是适配器和视图:
public class FavoritesAdapter extends RecyclerView.Adapter<FavoritesAdapter.ViewHolder>{
private List<RecipeModel> list;
private Context context;
public FavoritesAdapter(List<RecipeModel> list, Context context) {
this.list = list;
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder{
private CircleImageView circleImageView;
private TextView title;
private RelativeLayout relativeLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
circleImageView=(CircleImageView) itemView.findViewById(R.id.favoritesCircleImage);
title=(TextView) itemView.findViewById(R.id.favoritesTitle);
relativeLayout=(RelativeLayout) itemView.findViewById(R.id.favorites_layout);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_favorites_list_item, viewGroup, false);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Glide.with(context)
.asBitmap()
.load(list.get(i).getImage_url())
.into(viewHolder.circleImageView);
viewHolder.title.setText(list.get(i).getTitle());
viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Clicking on the item
}
});
}
@Override
public int getItemCount() {
return list.size();
}
}
这是活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FavoritesActivity">
<view class="android.support.v7.app.AlertController$RecycleListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerViewFavorites">
</view>
</RelativeLayout>
列表项的XML:
<?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="match_parent"
android:padding="15dp"
android:id="@+id/favorites_layout">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/favoritesCircleImage"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/favoritesTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="25dp"
android:text="DummyText"
android:textColor="#000"
android:textSize="17sp" />
</RelativeLayout>
这是实现recyclerview的代码段:
private void initRecyclerView(){
RecyclerView recyclerView=findViewById(R.id.recyclerViewFavorites);
FavoritesAdapter favoritesAdapter=new FavoritesAdapter(recipeModelList, getApplicationContext());
recyclerView.setAdapter(favoritesAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
但是,当我启动应用程序并打开活动时,应用程序崩溃。该列表很好,它没有设置为null我已经检查过了。
有什么想法吗?
答案 0 :(得分:4)
在XML布局中,您使用了RecycleListView。但是,在initRecyclerView()方法中,您已经初始化了RecylerView。因此,请使用RecyclerView而不是RecycleListView。 RecyclerView和RecycleListView是两件事。
替换
<view class="android.support.v7.app.AlertController$RecycleListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerViewFavorites">
</view>
使用
<android.support.v7.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerViewFavorites"/>