我想在我的应用程序中将Listview转换为RecyclerView,我尝试转换代码,但停留在getView方法上,谁能解释如何在Recylcer视图中使用此getView代码。
用于ListView的适配器 CardAdapter.java
public class CardAdapter extends ArrayAdapter<CardDataModel> {
private Context context;
List<CardDataModel> cards;
SharedPreference sharedPreference;
public CardAdapter(Context context, List<CardDataModel> cards) {
super(context, R.layout.card_list_item, cards);
this.context = context;
this.cards = cards;
sharedPreference = new SharedPreference();
}
private class ViewHolder {
TextView CardNameTxt;
TextView CardDescTxt;
TextView CardPriceTxt;
ImageView favoriteImg;
}
@Override
public int getCount() {
return cards.size();
}
@Override
public CardDataModel getItem(int position) {
return cards.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_list_item, null);
holder = new ViewHolder();
holder.CardNameTxt = (TextView) convertView
.findViewById(R.id.txt_crd_name);
holder.CardDescTxt = (TextView) convertView
.findViewById(R.id.txt_crd_desc);
holder.CardPriceTxt = (TextView) convertView
.findViewById(R.id.txt_crd_no);
holder.favoriteImg = (ImageView) convertView
.findViewById(R.id.imgbtn_favorite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CardDataModel card = (CardDataModel) getItem(position);
holder.CardNameTxt.setText(card.getName());
holder.CardDescTxt.setText(card.getDescription());
holder.CardPriceTxt.setText(card.getPrice() + " ");
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(card)) {
holder.favoriteImg.setImageResource(R.drawable.heart_red);
holder.favoriteImg.setTag("red");
} else {
holder.favoriteImg.setImageResource(R.drawable.heart_grey);
holder.favoriteImg.setTag("grey");
}
return convertView;
}
public boolean checkFavoriteItem(CardDataModel checkCard) {
boolean check = false;
List<CardDataModel> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (CardDataModel card : favorites) {
if (card.equals(checkCard)) {
check = true;
break;
}
}
}
return check;
}
@Override
public void add(CardDataModel card) {
super.add(card);
cards.add(card);
notifyDataSetChanged();
}
@Override
public void remove(CardDataModel card) {
super.remove(card);
cards.remove(card);
notifyDataSetChanged();
}
}
在RecyclerView Adapter中转换了这样的代码... CardRvAdapter.java
public class CardRvAdapter extends RecyclerView.Adapter<CardRvAdapter.MyViewHolder> {
private Context context;
List<CardDataModel> cards;
SharedPreference sharedPreference;
public CardRvAdapter(Context context, List<CardDataModel> cards) {
this.context = context;
this.cards = cards;
sharedPreference = new SharedPreference(); }
@Override
public int getItemCount() {
return cards.size();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
// on create view holder code
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.card_list_item, viewGroup,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
//on bind view holder code
myViewHolder.CardPriceTxt.setText(cards.get(i).getPrice());
myViewHolder.CardNameTxt.setText(cards.get(i).getName());
myViewHolder.CardDescTxt.setText(cards.get(i).getDescription());
if (checkFavoriteItem(card)) {
myViewHolder.favoriteImg.setImageResource(R.drawable.heart_red);
myViewHolder.favoriteImg.setTag("red");
} else {
myViewHolder.favoriteImg.setImageResource(R.drawable.heart_grey);
myViewHolder.favoriteImg.setTag("grey");
} }
// checking for favotites in shared preferences
public boolean checkFavoriteItem(CardDataModel checkCard) {
boolean check = false;
List<CardDataModel> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (CardDataModel card : favorites) {
if (card.equals(checkCard)) {
check = true;
break;
}
}
}
return check;
}
@Override
public void add(CardDataModel card) {
super.add(card);
cards.add(card);
notifyDataSetChanged();
}
@Override
public void remove(CardDataModel card) {
super.remove(card);
cards.remove(card);
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView CardNameTxt;
TextView CardDescTxt;
TextView CardPriceTxt;
ImageView favoriteImg;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
this.CardDescTxt = (TextView) itemView.findViewById(R.id.txt_crd_desc);
this.CardNameTxt = (TextView) itemView.findViewById(R.id.txt_crd_name);
this.CardPriceTxt = (TextView) itemView.findViewById(R.id.txt_crd_no);
this.favoriteImg = (ImageView) itemView.findViewById(R.id.imgbtn_favorite);
}}}
问题部分。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_list_item, null);
holder = new ViewHolder();
holder.CardNameTxt = (TextView) convertView
.findViewById(R.id.txt_crd_name);
holder.CardDescTxt = (TextView) convertView
.findViewById(R.id.txt_crd_desc);
holder.CardPriceTxt = (TextView) convertView
.findViewById(R.id.txt_crd_no);
holder.favoriteImg = (ImageView) convertView
.findViewById(R.id.imgbtn_favorite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CardDataModel card = (CardDataModel) getItem(position);
holder.CardNameTxt.setText(card.getName());
holder.CardDescTxt.setText(card.getDescription());
holder.CardPriceTxt.setText(card.getPrice() + " ");
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(card)) {
holder.favoriteImg.setImageResource(R.drawable.heart_red);
holder.favoriteImg.setTag("red");
} else {
holder.favoriteImg.setImageResource(R.drawable.heart_grey);
holder.favoriteImg.setTag("grey");
}
return convertView;
}
如何修改此代码以及在何处添加此checkFavItem,添加和删除方法。.建议我,我是android开发的新手。
答案 0 :(得分:1)
完成RecyclerView代码
// specify the row layout file and click for each row
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(listItemLayout, parent, false);
ViewHolder myViewHolder = new ViewHolder(view);
return myViewHolder;
}
// load data in each row element
@Override
public void onBindViewHolder(final ViewHolder holder, final int listPosition) {
TextView item = holder.item;
item.setText(itemList.get(listPosition).getName());
}