packagecom.example.toutiao.ui.card.newsCardList;importjava.util.ArrayList;importjava.util.Locale;/**
* CardItemDataModel class: the card item data model in New Channel Fragment
*/publicclassNewsCardItemDataModel{publicstaticfinalintNO_IMAGE_TYPE=0;publicstaticfinalintONE_IMAGE_TYPE=1;publicstaticfinalintTHREE_IMAGE_TYPE=2;privateintmItemType;// cart type
privateStringmId;//id
privateStringmAvatar;// avatar
privateArrayList<String>mThreeImageDrawable;// three image
privateStringmImageDrawable;// one image
privateStringmTitle;// title
privateStringmSubTitle;// subtitle
privateStringmBottomText;// bottom text
privateStringmDetailUrl;// detail text to jump
// no image style constructor
publicNewsCardItemDataModel(intitemType,Stringid,StringnewsTitle,StringnewsAbstract,intnewsCommentsCount,StringnewsSource,StringnewsMediaAvatarUrl,StringnewsSourceUrl){mItemType=itemType;mId=id;mAvatar=newsMediaAvatarUrl;mTitle=String.format(Locale.CHINESE,"%s",newsTitle);mSubTitle=String.format(Locale.CHINESE,"%s",newsAbstract);mBottomText=String.format(Locale.CHINESE,"%s %d 评论",newsSource,newsCommentsCount);mDetailUrl=newsSourceUrl;}// one image style constructor
publicNewsCardItemDataModel(intitemType,Stringid,StringnewsTitle,StringnewsAbstract,intnewsCommentsCount,StringnewsSource,StringnewsMediaAvatarUrl,StringnewsSourceUrl,StringimageDrawable){mItemType=itemType;mId=id;mAvatar=newsMediaAvatarUrl;mTitle=String.format(Locale.CHINESE,"%s",newsTitle);mSubTitle=String.format(Locale.CHINESE,"%s",newsAbstract);mBottomText=String.format(Locale.CHINESE,"%s %d 评论",newsSource,newsCommentsCount);mDetailUrl=newsSourceUrl;// one
mImageDrawable=imageDrawable;}// three image style constructor
publicNewsCardItemDataModel(intitemType,Stringid,StringnewsTitle,StringnewsAbstract,intnewsCommentsCount,StringnewsSource,StringnewsMediaAvatarUrl,StringnewsSourceUrl,ArrayList<String>threeImageDrawable){mItemType=itemType;mId=id;mAvatar=newsMediaAvatarUrl;mTitle=String.format(Locale.CHINESE,"%s",newsTitle);mSubTitle=String.format(Locale.CHINESE,"%s",newsAbstract);mBottomText=String.format(Locale.CHINESE,"%s %d 评论",newsSource,newsCommentsCount);mDetailUrl=newsSourceUrl;// three
mThreeImageDrawable=threeImageDrawable;}publicintgetItemType(){returnmItemType;}publicvoidsetItemType(intitemType){mItemType=itemType;}publicStringgetId(){returnmId;}publicvoidsetId(Stringid){mId=id;}publicStringgetAvatar(){returnmAvatar;}publicvoidsetAvatar(Stringavatar){mAvatar=avatar;}publicStringgetBottomText(){returnmBottomText;}publicvoidsetBottomText(StringbottomText){mBottomText=bottomText;}publicStringgetTitle(){returnmTitle;}publicvoidsetTitle(Stringtitle){mTitle=title;}publicStringgetSubTitle(){returnmSubTitle;}publicvoidsetSubTitle(StringsubTitle){mSubTitle=subTitle;}publicArrayList<String>getThreeImageDrawable(){returnmThreeImageDrawable;}publicvoidsetThreeImageDrawable(ArrayList<String>threeImageDrawable){mThreeImageDrawable=threeImageDrawable;}publicStringgetImageDrawable(){returnmImageDrawable;}publicvoidsetImageDrawable(StringimageDrawable){mImageDrawable=imageDrawable;}publicStringgetDetailUrl(){returnmDetailUrl;}publicvoidsetDetailUrl(StringdetailUrl){mDetailUrl=detailUrl;}}
packagecom.example.toutiao.ui.card.newsCardList;// ...
importandroidx.annotation.NonNull;importandroidx.recyclerview.widget.RecyclerView;importcom.example.toutiao.R;importcom.example.toutiao.activity.NewsDetailActivity;importcom.squareup.picasso.Picasso;importorg.jetbrains.annotations.NotNull;importjava.util.ArrayList;importjava.util.List;/**
* A card adapter to help perform to control card item's render in news channel fragment
*/publicclassNewsCardAdapterextendsRecyclerView.Adapter<RecyclerView.ViewHolder>{privatefinalList<NewsCardItemDataModel>mDataModelList;privatefinalContextmContext;publicNewsCardAdapter(List<NewsCardItemDataModel>modelList,Contextcontext){mDataModelList=modelList;mContext=context;}@OverridepublicintgetItemViewType(finalintposition){switch(mDataModelList.get(position).getItemType()){caseNewsCardItemDataModel.NO_IMAGE_TYPE:returnNewsCardItemDataModel.NO_IMAGE_TYPE;caseNewsCardItemDataModel.ONE_IMAGE_TYPE:returnNewsCardItemDataModel.ONE_IMAGE_TYPE;caseNewsCardItemDataModel.THREE_IMAGE_TYPE:returnNewsCardItemDataModel.THREE_IMAGE_TYPE;default:return-1;}}/**
* load more news and add to mDataModelList
*
* @param modelList
*/publicvoidsetDataModelList(List<NewsCardItemDataModel>modelList){mDataModelList.addAll(modelList);}@OverridepublicintgetItemCount(){returnmDataModelList.size();}@NonNull@OverridepublicRecyclerView.ViewHolderonCreateViewHolder(@NonNullViewGroupparent,intviewType){Viewview;switch(viewType){caseNewsCardItemDataModel.NO_IMAGE_TYPE:view=LayoutInflater.from(parent.getContext()).inflate(R.layout.no_image_card_item,parent,false);returnnewNoImageCardViewHolder(view);caseNewsCardItemDataModel.ONE_IMAGE_TYPE:view=LayoutInflater.from(parent.getContext()).inflate(R.layout.one_image_card_item,parent,false);returnnewOneImageCardViewHolder(view);caseNewsCardItemDataModel.THREE_IMAGE_TYPE:view=LayoutInflater.from(parent.getContext()).inflate(R.layout.three_images_card_item,parent,false);returnnewThreeImageCardViewHolder(view);default:returnnull;}}@OverridepublicvoidonBindViewHolder(@NotNullfinalRecyclerView.ViewHolderholder,finalintposition){NewsCardItemDataModelobject=mDataModelList.get(position);if(object!=null){switch(object.getItemType()){caseNewsCardItemDataModel.NO_IMAGE_TYPE:NoImageCardViewHolderholder1=(NoImageCardViewHolder)holder;holder1.bindData(object,mContext);break;caseNewsCardItemDataModel.ONE_IMAGE_TYPE:OneImageCardViewHolderholder2=(OneImageCardViewHolder)holder;holder2.bindData(object,mContext);break;caseNewsCardItemDataModel.THREE_IMAGE_TYPE:ThreeImageCardViewHolderholder3=(ThreeImageCardViewHolder)holder;holder3.bindData(object,mContext);break;default:break;}}}// No image style card view holder
classNoImageCardViewHolderextendsRecyclerView.ViewHolder{privatefinalImageViewmAvatarView;privatefinalTextViewmTitleTextView;privatefinalTextViewmSubTitleTextView;privatefinalTextViewmBottomTextView;privatefinalTextViewmSourceUrlTextView;publicNoImageCardViewHolder(@NonNullViewitemView){super(itemView);mAvatarView=itemView.findViewById(R.id.image_view_card_avatar);mTitleTextView=itemView.findViewById(R.id.text_view_card_title);mSubTitleTextView=itemView.findViewById(R.id.text_view_card_subtitle);mBottomTextView=itemView.findViewById(R.id.text_view_card_bottom_text);mSourceUrlTextView=itemView.findViewById(R.id.text_view_source_url);}publicvoidbindData(NewsCardItemDataModeldataModel,Contextcontext){Picasso.get().load(dataModel.getAvatar()).into(mAvatarView);// deal with title's length and subtitle's length
Stringtitle=dataModel.getTitle();if(title.length()>15){title=title.substring(0,16);title+="...";}mTitleTextView.setText(title);StringsubTitle=dataModel.getSubTitle();if(subTitle.length()>70){subTitle=subTitle.substring(0,69);subTitle+="...";}mSubTitleTextView.setText(subTitle);mBottomTextView.setText(dataModel.getBottomText());mSourceUrlTextView.setText(dataModel.getDetailUrl());}}// One image style card view holder
classOneImageCardViewHolderextendsRecyclerView.ViewHolder{privatefinalImageViewmAvatarView;privatefinalTextViewmTitleTextView;privatefinalTextViewmSubTitleTextView;privatefinalTextViewmBottomTextView;privatefinalTextViewmSourceUrlTextView;privatefinalImageViewmCardImageView;publicOneImageCardViewHolder(@NonNullViewitemView){super(itemView);mAvatarView=itemView.findViewById(R.id.image_view_card_avatar);mTitleTextView=itemView.findViewById(R.id.text_view_card_title);mSubTitleTextView=itemView.findViewById(R.id.text_view_card_subtitle);mBottomTextView=itemView.findViewById(R.id.text_view_card_bottom_text);mSourceUrlTextView=itemView.findViewById(R.id.text_view_source_url);mCardImageView=itemView.findViewById(R.id.image_view_card_image);}publicvoidbindData(NewsCardItemDataModeldataModel,Contextcontext){Picasso.get().load(dataModel.getAvatar()).into(mAvatarView);Picasso.get().load(dataModel.getImageDrawable()).into(mCardImageView);Stringtitle=dataModel.getTitle();// deal with title's length and subtitle's length
if(title.length()>15){title=title.substring(0,14);title+="...";}mTitleTextView.setText(title);StringsubTitle=dataModel.getSubTitle();if(subTitle.length()>70){subTitle=subTitle.substring(0,69);subTitle+="...";}mSubTitleTextView.setText(subTitle);mBottomTextView.setText(dataModel.getBottomText());mSourceUrlTextView.setText(dataModel.getDetailUrl());}}// Three image style card view holder
classThreeImageCardViewHolderextendsRecyclerView.ViewHolder{privatefinalImageViewmAvatarView;privatefinalTextViewmTitleTextView;privatefinalTextViewmSubTitleTextView;privatefinalTextViewmBottomTextView;privatefinalTextViewmSourceUrlTextView;privatefinalImageViewmCardImageView1;privatefinalImageViewmCardImageView2;privatefinalImageViewmCardImageView3;publicThreeImageCardViewHolder(@NonNullViewitemView){super(itemView);mAvatarView=itemView.findViewById(R.id.image_view_card_avatar);mTitleTextView=itemView.findViewById(R.id.text_view_card_title);mSubTitleTextView=itemView.findViewById(R.id.text_view_card_subtitle);mBottomTextView=itemView.findViewById(R.id.text_view_card_bottom_text);mSourceUrlTextView=itemView.findViewById(R.id.text_view_source_url);mCardImageView1=itemView.findViewById(R.id.image_view_image_1);mCardImageView2=itemView.findViewById(R.id.image_view_image_2);mCardImageView3=itemView.findViewById(R.id.image_view_image_3);}publicvoidbindData(NewsCardItemDataModeldataModel,Contextcontext){Picasso.get().load(dataModel.getAvatar()).into(mAvatarView);ArrayList<String>images=dataModel.getThreeImageDrawable();Picasso.get().load(images.get(0)).into(mCardImageView1);Picasso.get().load(images.get(1)).into(mCardImageView2);Picasso.get().load(images.get(2)).into(mCardImageView3);// deal with title's length and subtitle's length
Stringtitle=dataModel.getTitle();if(title.length()>15){title=title.substring(0,16);title+="...";}mTitleTextView.setText(title);StringsubTitle=dataModel.getSubTitle();if(subTitle.length()>70){subTitle=subTitle.substring(0,69);subTitle+="...";}mSubTitleTextView.setText(subTitle);mBottomTextView.setText(dataModel.getBottomText());mSourceUrlTextView.setText(dataModel.getDetailUrl());}}}
packagecom.example.toutiao.ui.page.newsChannel;// ...
importandroidx.annotation.NonNull;importandroidx.annotation.Nullable;importandroidx.fragment.app.Fragment;importandroidx.lifecycle.Observer;importandroidx.lifecycle.ViewModelProvider;importandroidx.recyclerview.widget.LinearLayoutManager;importandroidx.recyclerview.widget.RecyclerView;importcom.example.toutiao.R;importcom.example.toutiao.ui.card.newsCardList.NewsCardAdapter;importcom.example.toutiao.ui.card.newsCardList.NewsCardItemDataModel;/**
* A simple {@link Fragment} subclass.
* Use the {@link NewsChannelFragment#newInstance} factory method to
* create an instance of this fragment.
*/publicclassNewsChannelFragmentextendsFragment{privateStringmCategory;privateintmIndex;privateRecyclerViewmCardListRecyclerView;privateNewsCardAdaptermCardListAdapter;privateRecyclerView.LayoutManagermCardListLayoutManager;privatefinalList<NewsCardItemDataModel>mCardDataModelList=newArrayList<>();publicNewsChannelFragment(){}/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*/publicstaticNewsChannelFragmentnewInstance(Stringcategory,intindex){NewsChannelFragmentfragment=newNewsChannelFragment();Bundlebundle=newBundle();bundle.putString("category",category);bundle.putInt("index",index);fragment.setArguments(bundle);returnfragment;}@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);// ...
}@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){Viewview=inflater.inflate(R.layout.fragment_news_channel,container,false);// ...
for(inti=0;i<10;i++){inttype=i%3;StringnewsId=i;StringnewsTitle="你好";StringnewsAbstract="我是卡片";intnewsCommentsCount=100;StringnewsSource="https://example.com/e.png";StringnewsMediaAvatarUrl="https://example.com/e.png";StringnewsSourceUrl="www.google.com";if(type==NO_IMAGE_TYPE){mCardDataModelList.add(newNewsCardItemDataModel(NO_IMAGE_TYPE,newsId,newsTitle,newsAbstract,newsCommentsCount,newsSource,newsMediaAvatarUrl,newsSourceUrl));}elseif(type==ONE_IMAGE_TYPE){StringmiddleImage="https://example.com/e.png";mCardDataModelList.add(newNewsCardItemDataModel(ONE_IMAGE_TYPE,newsId,newsTitle,newsAbstract,newsCommentsCount,newsSource,newsMediaAvatarUrl,newsSourceUrl,middleImage));}elseif(type==THREE_IMAGE_TYPE){ArrayList<String>newsThreeImage=newArrayList()<>;newsThreeImage.add("https://example.com/e.png");newsThreeImage.add("https://example.com/e.png");newsThreeImage.add("https://example.com/e.png");mCardDataModelList.add(newNewsCardItemDataModel(THREE_IMAGE_TYPE,newsId,newsTitle,newsAbstract,newsCommentsCount,newsSource,newsMediaAvatarUrl,newsSourceUrl,newsThreeImage));}}// cardList
mCardListRecyclerView=view.findViewById(R.id.recycler_view_card_list);// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mCardListRecyclerView.setHasFixedSize(true);// use a linear layout manager
mCardListLayoutManager=newLinearLayoutManager(getContext());mCardListRecyclerView.setLayoutManager(mCardListLayoutManager);// specify an adapter and pass in our data model list
mCardListAdapter=newNewsCardAdapter(mCardDataModelList,getContext());mCardListRecyclerView.setAdapter(mCardListAdapter);returnview;}}