Created
April 29, 2019 12:40
-
-
Save Marchuck/9da128be33a1e2da98109aa7e1db0316 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
secondFeedAdatper = new SecondFeedAdatper(); | |
secondFeedAdatper.setClickListener(new SecondFeedAdatper.ClickListener() { | |
@Override | |
public void onArticleClick(Article article) { | |
if (article.getLink() != null && !article.getLink().isEmpty()) { | |
if (getActivity() instanceof LockscreenActivity) { | |
((LockscreenActivity) getActivity()).openURL(article.getLink()); | |
} | |
} | |
} | |
}); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); | |
lvSecond.setLayoutManager(layoutManager); | |
lvSecond.setAdapter(secondFeedAdatper); | |
//todo: futher updates should be done in the following way: | |
secondFeedAdatper.setArticles(newArticles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint; | |
import android.text.Html; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import androidx.annotation.NonNull; | |
import androidx.recyclerview.widget.RecyclerView; | |
import com.launcha.dan.lockscreenusingservice.R; | |
import com.launcha.dan.utils.Utils; | |
import com.prof.rssparser.Article; | |
import com.squareup.picasso.Picasso; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Locale; | |
public class SecondFeedAdatper extends RecyclerView.Adapter<SecondFeedAdatper.ViewHolder> { | |
public interface ClickListener { | |
void onArticleClick(Article article); | |
} | |
private final SimpleDateFormat sourceDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); | |
private final SimpleDateFormat destinationDateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault()); | |
private List<Article> articles = new ArrayList<>(); | |
private ClickListener clickListener; | |
public void setClickListener(ClickListener clickListener) { | |
this.clickListener = clickListener; | |
} | |
public SecondFeedAdatper() { | |
} | |
public void setArticles(List<Article> articles) { | |
this.articles = articles; | |
notifyDataSetChanged(); | |
} | |
@NonNull | |
@Override | |
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { | |
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext()); | |
View v = inflater.inflate(R.layout.item_feed, viewGroup, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int position) { | |
final Article currentArticle = articles.get(position); | |
viewHolder.bind(currentArticle, sourceDateFormat, destinationDateFormat); | |
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { | |
@SuppressLint("SetJavaScriptEnabled") | |
@Override | |
public void onClick(View view) { | |
if (clickListener != null) { | |
clickListener.onArticleClick(currentArticle); | |
} | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return articles.size(); | |
} | |
static class ViewHolder extends RecyclerView.ViewHolder { | |
TextView title; | |
TextView pubDate; | |
ImageView image; | |
TextView category; | |
TextView description; | |
TextView price; | |
ViewHolder(View itemView) { | |
super(itemView); | |
title = itemView.findViewById(R.id.title); | |
pubDate = itemView.findViewById(R.id.pubDate); | |
image = itemView.findViewById(R.id.image); | |
category = itemView.findViewById(R.id.categories); | |
description = itemView.findViewById(R.id.description); | |
price = itemView.findViewById(R.id.price); | |
} | |
void bind(Article currentArticle, SimpleDateFormat from, SimpleDateFormat to) { | |
final ViewHolder viewHolder = this; | |
String pubDateString = resolveDateOfPublication(currentArticle, from, to); | |
viewHolder.title.setText(currentArticle.getTitle()); | |
loadImage(currentArticle.getImage(), image); | |
viewHolder.pubDate.setText(pubDateString); | |
viewHolder.category.setText(currentArticle.getMerchant()); | |
viewHolder.price.setText(currentArticle.getPrice()); | |
if (currentArticle.getDescription() != null) { | |
viewHolder.description.setText(Html.fromHtml(Utils.compactString(currentArticle.getDescription()))); | |
} | |
} | |
private void loadImage(String imageString, ImageView image1) { | |
Picasso.get() | |
.load(imageString) | |
.into(image1); | |
} | |
private String resolveDateOfPublication(Article currentArticle, | |
SimpleDateFormat sourceSdf, | |
SimpleDateFormat sdf) { | |
try { | |
String sourceDateString = currentArticle.getPubDate(); | |
Date date = sourceSdf.parse(sourceDateString); | |
return sdf.format(date); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
return currentArticle.getPubDate(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment