Skip to content

Instantly share code, notes, and snippets.

@Vagonn
Created July 13, 2018 05:40
Show Gist options
  • Save Vagonn/5da9aaa7e7accb278e5a6c87ed766414 to your computer and use it in GitHub Desktop.
Save Vagonn/5da9aaa7e7accb278e5a6c87ed766414 to your computer and use it in GitHub Desktop.
FeedAdapter
package com.websiteca.metbuataz;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedViewHolder>
{
private Context mContext;
private ArrayList<FeedNewsItem> mFeedNewsItemsList;
private OnItemClickListener mListener;
public interface OnItemClickListener
{
void onItemclick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener)
{
mListener = listener;
}
FeedAdapter(Context context, ArrayList<FeedNewsItem> feedNewsItemsList)
{
mContext = context;
mFeedNewsItemsList = feedNewsItemsList;
}
@NonNull
@Override
public FeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(mContext).inflate(R.layout.feed_news_item, parent, false);
return new FeedViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull FeedViewHolder holder, int position)
{
FeedNewsItem currentItem = mFeedNewsItemsList.get(position);
String imageUrl = currentItem.getmFeedImageUrl();
String feedNewsTitle = currentItem.getmFeedNewsTitle();
String feedNewsSubTitle = currentItem.getmFeedNewsSubTitle();
String feedNewsTime = currentItem.getmFeedNewsTime();
String feedNewsDate = currentItem.getmFeedNewsDate();
// Title ve subTitle-a ayri style verib textview-a setText edirik
Spannable s1 = new SpannableString(feedNewsTitle);
s1.setSpan(new ForegroundColorSpan(Color.WHITE), 0, s1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.mFeedNewsTitleTextView.setText(s1);
Spannable s2 = new SpannableString(feedNewsSubTitle);
s2.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.subTitle)), 0, s2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.mFeedNewsTitleTextView.append(s2);
holder.mFeedNewsTimeTextView.setText(feedNewsTime);
holder.mFeedNewsDateTextView.setText(feedNewsDate);
Picasso.get().load(imageUrl).fit().centerInside().into(holder.mFeedImageView);
}
@Override
public int getItemCount()
{
return mFeedNewsItemsList.size();
}
class FeedViewHolder extends RecyclerView.ViewHolder
{
ImageView mFeedImageView;
TextView mFeedNewsTitleTextView;
TextView mFeedNewsTimeTextView;
TextView mFeedNewsDateTextView;
FeedViewHolder(View itemView)
{
super(itemView);
mFeedImageView = itemView.findViewById(R.id.imageview_feedNews);
mFeedNewsTitleTextView = itemView.findViewById(R.id.textview_feedNewsTitle);
mFeedNewsTimeTextView = itemView.findViewById(R.id.textview_feedNewsTime);
mFeedNewsDateTextView = itemView.findViewById(R.id.textview_feedNewsDate);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mListener != null)
{
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION)
{
mListener.onItemclick(position);
}
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment