简体   繁体   中英

Extend a class from an adapter class

In my application should be a lot of adapters similar to each other and I would like to know whether it is possible to make the interface or superclass to inherit from there and then, instead of writing 10 adapters, thanks !!

My adapter code

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {

    private List<News> newsList;
    private Context ctx;

    public NewsAdapter(List<News> newsList, Context ctx) {
        this.newsList = newsList;
        this.ctx = ctx;
    }

    static class NewsViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView newsTitle;
        TextView newsDescription;
        ImageView newsImage;
        TextView newsDate;

        NewsViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cvNews);
            newsTitle = (TextView) itemView.findViewById(R.id.title_news);
            newsDescription = (TextView) itemView.findViewById(R.id.description_news);
            newsDate = (TextView) itemView.findViewById(R.id.date_news);
            newsImage = (ImageView) itemView.findViewById(R.id.image_news);
        }
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent,
                false);
        return new NewsViewHolder(v);
    }

    @Override
    public void onBindViewHolder(NewsViewHolder holder, int position) {
        holder.newsTitle.setText(newsList.get(position).newsTitle);
        holder.newsDescription.setText(newsList.get(position).newsDescription);
        holder.newsDate.setText(newsList.get(position).newsDate);
        Glide.with(ctx).load(Constants.SITE + newsList.get(position).newsImage)
                .into(holder.newsImage);
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

}

All should reflect like this

it is possible to make the interface or superclass to inherit from there and then, instead of writing 10 adapters

It is possible, but of course there must be something in common. For a start the dataset should be homogeneous. Eg your items should all implement an interface, a contract between the item and the adapter itself. In your case, it could be

public interface DataItem {
     String getTitle();
     String getDescription();
     String getDate();
     String getImageUrl();
}

and instead of a private List<News> newsList; , you will have private List<? extends DataItem> newsList; List<? extends DataItem> newsList; . As I said, all your items have to implement that interface, and the properties have to be accessed through the getters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM