From f04ebcb02f38e23b4c1b3de6cf7f1bbe1d05d03e Mon Sep 17 00:00:00 2001 From: Defend Date: Tue, 14 Feb 2017 22:59:11 +0300 Subject: [PATCH] Added clearNewsFeed method for clearing layout of news feed every update. Added ...lastitem.xml - now on the end news feed looking better. Some other visual fixes with xml's. Rewrited drawNewsList for visual fixes. Added refresh news feed button. --- app/app.iml | 1 + .../defdevteam/tstu/CabinetActivity.java | 23 +++++++- .../defdevteam/tstu/FeedController.java | 56 +++++++++++-------- app/src/main/res/layout/cabinet_newsfeed.xml | 18 ++++++ .../main/res/layout/cabinet_newsfeed_item.xml | 3 +- .../res/layout/cabinet_newsfeed_lastitem.xml | 44 +++++++++++++++ 6 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 app/src/main/res/layout/cabinet_newsfeed_lastitem.xml diff --git a/app/app.iml b/app/app.iml index 8b0c423..8451206 100644 --- a/app/app.iml +++ b/app/app.iml @@ -102,6 +102,7 @@ + diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java index 1582c65..e190e81 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java @@ -29,6 +29,7 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.Chronometer; import android.widget.ImageView; import android.widget.LinearLayout; @@ -304,10 +305,15 @@ public class CabinetActivity extends AppCompatActivity } } - public void addNewsItem (String title, String text, String link) { + public void addNewsItem(String title, String text, String link, Boolean LastItem) { LayoutInflater inflater = getLayoutInflater(); LinearLayout newsFeed = (LinearLayout) findViewById(R.id.newsfeed_scroll); - View layout = inflater.inflate(R.layout.cabinet_newsfeed_item, null); + View layout = null; + if(LastItem) { + layout = inflater.inflate(R.layout.cabinet_newsfeed_lastitem, null); + } else { + layout = inflater.inflate(R.layout.cabinet_newsfeed_item, null); + } newsFeed.addView(layout); TextView titleTextView = (TextView) layout.findViewById(R.id.newsfeed_item_title); @@ -320,6 +326,15 @@ public class CabinetActivity extends AppCompatActivity .execute(link); } + public void clearNewsFeed() { + LinearLayout newsFeedPage = (LinearLayout) findViewById(R.id.newsfeed_scroll); + try { + newsFeedPage.removeAllViews(); + } catch (Exception e) { + + } + } + public void loadSite (String url) { browser.loadUrl(url); } @@ -364,6 +379,10 @@ public class CabinetActivity extends AppCompatActivity facultyList.setAdapter(facultyL); } + public void onClickButtonNewsFeed(View view) { + feedController.drawNewsList(feedController.newsItemList); + } + @TargetApi(19) public void getHTML(){ browser.evaluateJavascript("javascript:document.documentElement.outerHTML;", new ValueCallback() { diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java index 2008a89..27928c3 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java +++ b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java @@ -8,18 +8,17 @@ import android.util.Log; public class FeedController implements FeedReader.ReaderCallback, FeedParser.ParserCallback{ - FeedReader feedReader; - FeedUpdater feedUpdater; - String newsFeed; + private FeedReader feedReader; + private FeedUpdater feedUpdater; NewsItemList newsItemList; - int updateRate; //in minutes + private int updateRate; //in minutes private CabinetActivity activity; - public FeedController(CabinetActivity activity, int updateRate) { + FeedController(CabinetActivity activity, int updateRate) { this(activity, updateRate, "http://press.tstu.ru/?format=feed"); } - public FeedController(CabinetActivity activity, int updateRate, String rssUrl) { + FeedController(CabinetActivity activity, int updateRate, String rssUrl) { feedReader = new FeedReader(this, rssUrl); feedReader.registerCallback(this); newsItemList = new NewsItemList(); @@ -65,7 +64,6 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par public void run() { startFeedReader(); try { - //TODO: вернуть к норме updateRate*60*1000 sleep(updateRate*60*1000); } catch (InterruptedException e) { e.printStackTrace(); @@ -73,18 +71,9 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par } public FeedUpdater() { - //this.start(); } } -// public String getRssString() throws Exception{ -// if(checkFeedReader()) { -// newsFeed = feedReader.getResult(); -// return newsFeed; -// } -// throw new Exception("Feed Reader returned empty string"); -// } - public void addNewsList(String title, String text, String link) { newsItemList.add(new NewsItemList().new Group( title, @@ -93,17 +82,34 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par )); } + public void drawNewsList(NewsItemList newsItemList) { - for (NewsItemList.Group item: newsItemList) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.clearNewsFeed(); + } + }); + for (int i = 0; i < newsItemList.size(); i++) { + NewsItemList.Group item = newsItemList.get(i); final String title = item.getTitle(); final String text = item.getText(); final String link = item.getLink(); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.addNewsItem(title, text, link); - } - }); + if(i < newsItemList.size() - 1) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.addNewsItem(title, text, link, false); + } + }); + } else { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.addNewsItem(title, text, link, true); + } + }); + } } } @@ -119,8 +125,9 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par @Override public void onSuccess(final String result){ - this.newsFeed = result; addNewsList("Callback Reader Test", result, ""); + addNewsList("Multiple Items Test 1", result, ""); + addNewsList("Multiple Items Test 2", result, ""); FeedParser parser = new FeedParser(result); parser.registerCallback(this); parser.parseXML(); @@ -134,6 +141,7 @@ public class FeedController implements FeedReader.ReaderCallback, FeedParser.Par @Override public void onSuccessParsing() { drawNewsList(this.newsItemList); + drawNewsList(this.newsItemList); } @Override diff --git a/app/src/main/res/layout/cabinet_newsfeed.xml b/app/src/main/res/layout/cabinet_newsfeed.xml index f7b73bc..bdf3269 100644 --- a/app/src/main/res/layout/cabinet_newsfeed.xml +++ b/app/src/main/res/layout/cabinet_newsfeed.xml @@ -20,4 +20,22 @@ + + +