Browse Source

added resize refresh when rotating

yattoz 5 years ago
parent
commit
a971343c3b

+ 23 - 6
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsFragment.kt View File

11
 import androidx.lifecycle.ViewModelProviders
11
 import androidx.lifecycle.ViewModelProviders
12
 import androidx.recyclerview.widget.LinearLayoutManager
12
 import androidx.recyclerview.widget.LinearLayoutManager
13
 import androidx.recyclerview.widget.RecyclerView
13
 import androidx.recyclerview.widget.RecyclerView
14
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
14
 import fr.forum_thalie.tsumugi.R
15
 import fr.forum_thalie.tsumugi.R
15
 
16
 
16
 class NewsFragment : Fragment() {
17
 class NewsFragment : Fragment() {
47
                 Log.d(tag, "webview already created!?")
48
                 Log.d(tag, "webview already created!?")
48
             }
49
             }
49
 
50
 
51
+            newsViewModel.root.addOnLayoutChangeListener(orientationLayoutListener)
50
             return newsViewModel.root
52
             return newsViewModel.root
51
         }
53
         }
52
 
54
 
53
         newsViewModel =
55
         newsViewModel =
54
                 ViewModelProviders.of(this).get(NewsViewModel::class.java)
56
                 ViewModelProviders.of(this).get(NewsViewModel::class.java)
55
 
57
 
56
-        val root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.swiperefreshlayout.widget.SwipeRefreshLayout
58
+        newsViewModel.root = inflater.inflate(R.layout.fragment_news, container, false) as SwipeRefreshLayout
57
 
59
 
58
         viewManager = LinearLayoutManager(context)
60
         viewManager = LinearLayoutManager(context)
59
         viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!)
61
         viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!)
60
-        recyclerView = root.findViewById<RecyclerView>(R.id.news_recycler).apply {
62
+        recyclerView = newsViewModel.root.findViewById<RecyclerView>(R.id.news_recycler).apply {
61
             // use this setting to improve performance if you know that changes
63
             // use this setting to improve performance if you know that changes
62
             // in content do not change the layout size of the RecyclerView
64
             // in content do not change the layout size of the RecyclerView
63
             setHasFixedSize(true)
65
             setHasFixedSize(true)
69
             adapter = viewAdapter
71
             adapter = viewAdapter
70
         }
72
         }
71
 
73
 
72
-        root.setOnRefreshListener {
74
+        (newsViewModel.root as SwipeRefreshLayout).setOnRefreshListener {
75
+            newsViewModel.fetch((newsViewModel.root as SwipeRefreshLayout), viewAdapter, context!!)
76
+        }
73
 
77
 
74
-            newsViewModel.fetch(root, viewAdapter, context!!)
78
+        newsViewModel.root.addOnLayoutChangeListener(orientationLayoutListener)
79
+        return newsViewModel.root
80
+    }
75
 
81
 
76
-        }
82
+    private val orientationLayoutListener : View.OnLayoutChangeListener = View.OnLayoutChangeListener { _: View, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int ->
77
 
83
 
78
-        return root
84
+        val viewHeight = (newsViewModel.root.rootView?.height ?: 1)
85
+        val viewWidth = (newsViewModel.root.rootView?.width ?: 1)
86
+
87
+        val newRatio = if (viewWidth > 0)
88
+            (viewHeight*100)/viewWidth
89
+        else
90
+            100
91
+
92
+        if (newsViewModel.screenRatio != newRatio) {
93
+            newsViewModel.fetch((newsViewModel.root as SwipeRefreshLayout), viewAdapter, context!!)
94
+            newsViewModel.screenRatio = newRatio
95
+        }
79
     }
96
     }
80
 
97
 
81
     override fun onCreate(savedInstanceState: Bundle?) {
98
     override fun onCreate(savedInstanceState: Bundle?) {

+ 1 - 0
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsViewModel.kt View File

26
 
26
 
27
 class NewsViewModel : ViewModel() {
27
 class NewsViewModel : ViewModel() {
28
 
28
 
29
+    var screenRatio: Int = 100
29
     lateinit var root: View
30
     lateinit var root: View
30
     var webView: WebView? = null
31
     var webView: WebView? = null
31
     var webViewNews: WebViewNews? = null
32
     var webViewNews: WebViewNews? = null