2016-11-12 8 views
1

私のアプリケーションでは、お互いに似たアダプタがたくさんあるはずです。インターフェイスまたはスーパークラスを継承するかどうかを知りたいのですが、10アダプター、ありがとう!アダプタクラスからクラスを拡張する

マイアダプターコード

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

+0

*それぞれの*に似たアダプタがたくさんあるはずです。 – Blackbelt

+0

@BlackBelt彼らは同じような種類のデザイン(項目)を持っています....私は写真を追加しました、それを見てください) –

+0

同じアダプタ、同じビューホルダーと同じPOJOクラスを使用することができますまたは類似している。軽微な違いがある場合は、リストごとに新しいアダプターを作成するのではなく、必要に応じて他のブロックを作成することができます。 –

答えて

0

それの代わりに10個のアダプタ

を書いて、そこに、その後 から継承するインタフェースやスーパークラスを作ることが可能ですもちろん可能ですが、共通のものが必要です。開始するには、データセットは均質でなければなりません。例えば。あなたのアイテムはすべてインターフェイス、アイテムとアダプタ自体の間の契約を実装する必要があります。あなたのケースでは、それは

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

、代わりにprivate List<News> newsList;の可能性があり、あなたはプライベートList<? extends DataItem> newsList;を持つことになります。私が言ったように、あなたのすべてのアイテムはそのインターフェースを実装しなければならず、プロパティはゲッターを介してアクセスする必要があります

+1

ありがとう! –

関連する問題