Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. import android.text.Html;
  2.  
  3. import java.io.Serializable;
  4.  
  5. /**
  6. * This class is used to remember rss item data
  7. */
  8.  
  9. public class RSSItem implements Serializable {
  10.  
  11. private String title = null;
  12. private String description = null;
  13. private String rowdescription = null;
  14. private String link = null;
  15. private String pubdate = null;
  16. private String thumburl = null;
  17. private String audiourl = null;
  18. private String videourl = null;
  19.  
  20. void setTitle(String value) {
  21. title = value;
  22. }
  23.  
  24. void setDescription(String value) {
  25. description = value;
  26. rowdescription = stripHtml(value).toString();
  27. }
  28.  
  29. void setLink(String value) {
  30. link = value;
  31. }
  32.  
  33. void setPubdate(String value) {
  34. pubdate = value;
  35. }
  36.  
  37. void setThumburl(String value) {
  38. thumburl = value;
  39. }
  40.  
  41. void setVideourl(String value) {
  42. videourl = value;
  43. }
  44.  
  45. void setAudiourl(String value) {
  46. audiourl = value;
  47. }
  48.  
  49. public String getTitle() {
  50. return title;
  51. }
  52.  
  53. public String getDescription() {
  54. return description;
  55. }
  56.  
  57. public String getRowDescription() {
  58. return rowdescription;
  59. }
  60.  
  61. public String getLink() {
  62. return link;
  63. }
  64.  
  65. public String getPubdate() {
  66. return pubdate;
  67. }
  68.  
  69. public String getAudiourl() {
  70. return audiourl;
  71. }
  72.  
  73. public String getVideourl() {
  74. return videourl;
  75. }
  76.  
  77. public String getThumburl() {
  78. return thumburl;
  79. }
  80.  
  81. public CharSequence stripHtml(String s) {
  82. return Html.fromHtml(s).toString().replace('n', (char) 32)
  83. .replace((char) 160, (char) 32).replace((char) 65532, (char) 32).trim();
  84. }
  85.  
  86. }
  87.  
  88. import java.util.List;
  89. import java.util.Vector;
  90.  
  91. /**
  92. * This class is used to save information about our rss feed
  93. */
  94.  
  95. public class RSSFeed {
  96. private String title = null;
  97. private String description = null;
  98. private String link = null;
  99. private String pubdate = null;
  100. private List<RSSItem> itemList;
  101.  
  102. RSSFeed(){
  103. itemList = new Vector<RSSItem>(0);
  104. }
  105.  
  106. void addItem(RSSItem item){
  107. itemList.add(item);
  108. }
  109.  
  110. public RSSItem getItem(int location){
  111. return itemList.get(location);
  112. }
  113.  
  114. public List<RSSItem> getList(){
  115. return itemList;
  116. }
  117.  
  118. void setTitle(String value)
  119. {
  120. title = value;
  121. }
  122. void setDescription(String value)
  123. {
  124. description = value;
  125. }
  126. void setLink(String value)
  127. {
  128. link = value;
  129. }
  130. void setPubdate(String value)
  131. {
  132. pubdate = value;
  133. }
  134.  
  135. public String getTitle()
  136. {
  137. return title;
  138. }
  139. public String getDescription()
  140. {
  141. return description;
  142. }
  143. public String getLink()
  144. {
  145. return link;
  146. }
  147. String getPubdate()
  148. {
  149. return pubdate;
  150. //This is not formatted, as formatting might be different for every feed
  151. }
  152.  
  153. }
  154.  
  155. import android.content.Context;
  156. import android.content.Intent;
  157. import android.graphics.Bitmap;
  158. import android.graphics.drawable.Drawable;
  159. import android.os.Bundle;
  160. import android.support.v7.widget.RecyclerView;
  161. import android.view.LayoutInflater;
  162. import android.view.View;
  163. import android.view.ViewGroup;
  164. import android.widget.ImageView;
  165. import android.widget.TextView;
  166.  
  167. import com.sherdle.universal.R;
  168. import com.sherdle.universal.providers.rss.ui.RssDetailActivity;
  169. import com.sherdle.universal.providers.rss.ui.RssFragment;
  170. import com.sherdle.universal.util.InfiniteRecyclerViewAdapter;
  171. import com.sherdle.universal.util.ViewModeUtils;
  172. import com.squareup.picasso.Picasso;
  173. import com.squareup.picasso.Target;
  174.  
  175. import java.util.List;
  176.  
  177. public class RssAdapter extends InfiniteRecyclerViewAdapter {
  178.  
  179. private List<RSSItem> objects;
  180. private Context context;
  181.  
  182. private static int COMPACT = 0;
  183. private static int NORMAL = 1;
  184.  
  185. private ViewModeUtils viewModeUtils;
  186.  
  187. public RssAdapter(Context context,
  188. List<RSSItem> list) {
  189. super(context, null);
  190. this.context = context;
  191. this.objects = list;
  192.  
  193. this.viewModeUtils = new ViewModeUtils(context, RssFragment.class);
  194. }
  195.  
  196. @Override
  197. public int getViewType(int position) {
  198. if (viewModeUtils.getViewMode() == ViewModeUtils.NORMAL)
  199. return NORMAL;
  200. else
  201. return COMPACT;
  202. }
  203.  
  204. @Override
  205. protected RecyclerView.ViewHolder getViewHolder(ViewGroup parent, int viewType) {
  206. if (COMPACT == viewType) {
  207. View itemView = LayoutInflater.from(parent.getContext())
  208. .inflate(R.layout.fragment_rss_row, parent, false);
  209. return new RssViewHolder((itemView));
  210. } else if (viewType == NORMAL) {
  211. View itemView = LayoutInflater.from(parent.getContext())
  212. .inflate(R.layout.listview_row, parent, false);
  213. return new RssLargeViewHolder(itemView);
  214. }
  215. return null;
  216. }
  217.  
  218. @Override
  219. protected void doBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
  220. if (viewHolder instanceof RssViewHolder){
  221. RssViewHolder holder = (RssViewHolder) viewHolder;
  222.  
  223. String html = objects.get(position).getRowDescription();
  224.  
  225. holder.listTitle.setText(objects.get(position).getTitle());
  226. holder.listPubdate.setText(objects.get(position).getPubdate());
  227. holder.listDescription.setText(html);
  228.  
  229. holder.listThumb.setImageDrawable(null);
  230. String thumburl = objects.get(position).getThumburl();
  231.  
  232. loadImageIntoView(thumburl, holder.listThumb);
  233. setOnClickListener(holder.itemView, position);
  234. } else {
  235. RssLargeViewHolder itemHolder = (RssLargeViewHolder) viewHolder;
  236.  
  237. itemHolder.headlineView.setText(objects.get(position).getTitle());
  238. itemHolder.reportedDateView.setText(objects.get(position).getPubdate());
  239.  
  240. itemHolder.imageView.setImageBitmap(null);
  241. String thumburl = objects.get(position).getThumburl();
  242.  
  243. loadImageIntoView(thumburl, itemHolder.imageView);
  244. setOnClickListener(itemHolder.itemView, position);
  245.  
  246. }
  247. }
  248.  
  249. private void setOnClickListener(View view, final int position){
  250. view.setOnClickListener(new View.OnClickListener() {
  251. @Override
  252. public void onClick(View view) {
  253. Intent intent = new Intent(context,
  254. RssDetailActivity.class);
  255. Bundle bundle = new Bundle();
  256. intent.putExtra(RssDetailActivity.EXTRA_RSSITEM, objects.get(position));
  257.  
  258. intent.putExtras(bundle);
  259. context.startActivity(intent);
  260. }
  261. });
  262. }
  263.  
  264. private void loadImageIntoView(String thumburl, final ImageView listThumb){
  265. if (thumburl != null && !thumburl.equals("")) {
  266. //setting the image
  267. final ImageView imageView = listThumb; // The view Picasso is loading an image into
  268. final Target target = new Target() {
  269. @Override
  270. public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
  271. /* Save the bitmap or do something with it here */
  272.  
  273. if (10 > bitmap.getWidth() || 10 > bitmap.getHeight()) {
  274. // handle scaling
  275. listThumb.setVisibility(View.GONE);
  276. } else {
  277. listThumb.setVisibility(View.VISIBLE);
  278. listThumb.setImageBitmap(bitmap);
  279. }
  280. }
  281.  
  282. @Override
  283. public void onBitmapFailed(Exception e, Drawable errorDrawable) {
  284. }
  285.  
  286. @Override
  287. public void onPrepareLoad(Drawable placeHolderDrawable) {
  288. }
  289. };
  290.  
  291. imageView.setTag(target);
  292.  
  293. Picasso.get()
  294. .load(thumburl)
  295. .into(target);
  296. } else {
  297. listThumb.setVisibility(View.GONE);
  298. }
  299. }
  300.  
  301. @Override
  302. protected int getCount() {
  303. return objects.size();
  304. }
  305.  
  306. private class RssViewHolder extends RecyclerView.ViewHolder {
  307. TextView listTitle;
  308. TextView listPubdate;
  309. TextView listDescription;
  310. ImageView listThumb;
  311.  
  312. RssViewHolder(View view){
  313. super(view);
  314. this.listTitle = view.findViewById(R.id.listtitle);
  315. this.listPubdate = view.findViewById(R.id.listpubdate);
  316. this.listDescription = view.findViewById(R.id.shortdescription);
  317. this.listThumb = view.findViewById(R.id.listthumb);
  318. }
  319. }
  320.  
  321. private static class RssLargeViewHolder extends RecyclerView.ViewHolder {
  322. TextView headlineView;
  323. TextView reportedDateView;
  324. ImageView imageView;
  325.  
  326. RssLargeViewHolder(View view){
  327. super(view);
  328.  
  329. this.headlineView = view.findViewById(R.id.title);
  330. this.reportedDateView = view.findViewById(R.id.date);
  331. this.imageView = view.findViewById(R.id.thumbImage);
  332.  
  333. }
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement