我有这段代码,但每当我尝试将其更改为片段时,我总是会收到错误。有人可以帮我这个吗?
这是我的带有cardview的RecyclerViewAdapter
public class RecyclerViewAdapter extendsRecyclerView.Adapter {
private Context mContext ;
private List<Book> mData ;
public RecyclerViewAdapter(Context mContext, List<Book> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardveiw_item_book,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,Book_Activity.class);
// passing data to the book activity
intent.putExtra("Title",mData.get(position).getTitle());
intent.putExtra("Description",mData.get(position).getDescription());
intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
// start the activity
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView img_book_thumbnail;
CardView cardView ;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id) ;
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_img_id);
cardView = (CardView) itemView.findViewById(R.id.cardview_id);
}
}
}
这适用于MainActivity
公共类MainActivity扩展了AppCompatActivity {
List<Book> lstBook ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstBook = new ArrayList<>();
lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
RecyclerView myrv = (RecyclerView) findViewById(R.id.reyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
myrv.setLayoutManager(new GridLayoutManager(this,2));
myrv.setAdapter(myAdapter);
}
}
如何将我的主要活动转换为片段?
答案 0 :(得分:1)
超级简单
public class MainActivity extends Fragment {
List<Book> lstBook ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstBook = new ArrayList<>();
lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
}
@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main,container)
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.reyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getActivity(),lstBook);
myrv.setLayoutManager(new GridLayoutManager(this,2));
myrv.setAdapter(myAdapter)
return view;
}
}