Commit 5a367956 authored by wanglei's avatar wanglei

...

parent 725a51a7
......@@ -6,8 +6,9 @@
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
......
package com.base.browserwhite.ui.adapter
import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.browserwhite.R
import com.base.browserwhite.bean.NewsBean
import com.base.browserwhite.databinding.ItemNewsBinding
import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.NewsUtils
import com.base.browserwhite.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class NewsAdapter(
private val lifecycleScope: LifecycleCoroutineScope
) : RecyclerView.Adapter<NewsAdapter.NewsViewHolder>() {
private val TAG = "NewsAdapter"
private val beanList = arrayListOf<NewsBean>()
inner class NewsViewHolder(view: View) : ViewHolder(view)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
return NewsViewHolder(R.layout.item_news.inflate(parent))
}
override fun getItemCount(): Int {
return beanList.size
}
override fun onBindViewHolder(holder: NewsViewHolder, @SuppressLint("RecyclerView") position: Int) {
val binding = ItemNewsBinding.bind(holder.itemView)
val bean = beanList[holder.absoluteAdapterPosition]
val context = holder.itemView.context
runCatching {
Glide.with(context).load(bean.orgImgPath).listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
beanList.remove(bean)
notifyItemRemoved(position)
return true
}
override fun onResourceReady(
resource: Drawable,
model: Any,
target: Target<Drawable>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
return false
}
}).centerCrop().into(binding.ivMedia)
}
binding.tvTittle.text = bean.headline
runCatching {
Glide.with(context).load(bean.mediaIcon).into(binding.ivIcon)
}
binding.tvMediaName.text = bean.mediaName
binding.tvPublishTime.text = bean.publishTime.toFormatTime()
}
fun loadMore(categoryId: Int = 0, callBack: (() -> Unit)? = null) {
val position = beanList.size
val last = if (beanList.isEmpty()) null else beanList.last()
LogEx.logDebug(TAG, "newsId=${last?.newsId}")
NewsUtils.requestNews(categoryId, last?.newsId) {
beanList.addAll(it)
lifecycleScope.launch(Dispatchers.Main) {
this@NewsAdapter.notifyItemInserted(position)
delay(1000)
callBack?.invoke()
}
}
}
}
\ No newline at end of file
package com.base.browserwhite.ui.fragment
import android.content.Intent
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.base.browserwhite.R
import com.base.browserwhite.bean.ConstObject.BAIDU
import com.base.browserwhite.bean.ConstObject.BING
......@@ -19,20 +22,25 @@ import com.base.browserwhite.service.StayNotificationService.Companion.restartSt
import com.base.browserwhite.ui.activity.scanqrc.ScanQRCActivity
import com.base.browserwhite.ui.activity.webbrowser.WebBrowserActivity
import com.base.browserwhite.ui.activity.webstore.WevStoreActivity
import com.base.browserwhite.ui.adapter.NewsAdapter
import com.base.browserwhite.ui.adapter.WebsiteAdapter
import com.base.browserwhite.ui.views.DialogViews.showSearchEngineDialog
import com.base.browserwhite.ui.views.itemtouch.MyItemTouchHelperCallBack
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.LogEx
class HomeFragment : BaseFragment<FragmentHomeBinding>() {
private val TAG = "HomeFragment"
override val binding: FragmentHomeBinding by lazy {
FragmentHomeBinding.inflate(layoutInflater)
}
private lateinit var websiteAdapter: WebsiteAdapter
private lateinit var itemTouchHelper: ItemTouchHelper
private lateinit var newsAdapter: NewsAdapter
private var isLoadingMore = false
override fun setView() {
......@@ -50,6 +58,29 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
binding.rvWeb.adapter = websiteAdapter
itemTouchHelper = ItemTouchHelper(MyItemTouchHelperCallBack())
itemTouchHelper.attachToRecyclerView(binding.rvWeb)
newsAdapter = NewsAdapter(lifecycleScope)
binding.rvNews.adapter = newsAdapter
binding.rvNews.addOnScrollListener(object : OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val lastVisibleItemPosition = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
if (!isLoadingMore && lastVisibleItemPosition == newsAdapter.itemCount - 5) {
isLoadingMore = true
// 触发加载更多数据
newsAdapter.loadMore {
LogEx.logDebug(TAG, "loadMore BACK")
isLoadingMore = false
}
}
}
})
isLoadingMore = true
newsAdapter.loadMore {
LogEx.logDebug(TAG, "loadMore BACK")
isLoadingMore = false
}
}
override fun onResume() {
......
......@@ -34,7 +34,7 @@ object NewsUtils {
}
fun requestNews(
categoryId: Int,
categoryId: Int = 0,
pageNumber: Long? = null,
beanCallBack: (beanList: List<NewsBean>) -> Unit
) {
......
......@@ -320,6 +320,41 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:text="News"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/tv_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:text="More"
android:textColor="#0571ED"
android:textSize="15sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_news" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="8dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="180dp"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<ImageView
android:id="@+id/iv_media"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ContentDescription" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/tv_tittle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="19sp"
android:textStyle="bold"
tools:text="Snoop Dogg Loved Watching AHorse Dance To 'Gin And Juice" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="15dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:minWidth="25dp"
android:minHeight="25dp"
app:civ_border_color="#E6E7EB"
app:civ_border_width="1px" />
<TextView
android:id="@+id/tv_media_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="5dp"
android:textColor="#666666"
android:textSize="13sp"
tools:text="Uproxx" />
<TextView
android:id="@+id/tv_publishTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="15dp"
android:textColor="#666666"
android:textSize="13sp"
tools:text="1 minute ago" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#E5E6EB" />
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment