我按照本教程进行操作,并仔细按照说明进行操作,但遇到了一些问题。在我的 ItemView 构造函数中,cardview 和 photo 是“无法解析的符号”:
ItemViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardview);
title = (TextView)itemView.findViewById(R.id.title);
cover = (ImageView)itemView.findViewById(R.id.photo);
}
这是整个适配器类:
package com.example.aaronpries.myfinal;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>{
private Context context;
private List<Item> books;
ItemAdapter(List<Item> books){
this.books = books;
}
@Override
public int getItemCount() {
return books.size();
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_cal, viewGroup, false);
ItemViewHolder bvh = new ItemViewHolder(v);
return bvh;
}
@Override
public void onBindViewHolder(ItemViewHolder bookViewHolder, int i) {
bookViewHolder.title.setText(books.get(i).text);
bookViewHolder.cover.setImageResource(books.get(i).imgID);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
ImageView cover;
ItemViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardview);
title = (TextView)itemView.findViewById(R.id.title);
cover = (ImageView)itemView.findViewById(R.id.photo);
}
}
}
这是我的项目类:
package com.example.aaronpries.myfinal;
import java.io.Serializable;
public class Item implements Serializable{
public int id;
public String text;
public int imgID;
}
这是recyclerview xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview"
/>
</LinearLayout>
活动 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardview"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_toRightOf="@+id/photo"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
最后是 Activity 类:
public class CalActivity extends AppCompatActivity {
String[] events = {"A","B","C","D","E","F","G"};
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recycler = (RecyclerView)findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recycler.setLayoutManager(layoutManager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
我正在尝试遵循一些不同的教程来了解如何让 RecyclerView 正常工作,但其中大多数最后都没有显示完整的代码,而且由于我是新手,有时我很难遵循。我还想向 RecyclerView 添加图像和标题文本,因此如果您对此有任何建议,我将不胜感激。我是否只是一遍又一遍地执行 Item.add("text",imgID) 来填充数据?