gpt4 book ai didi

android - 从 Kotlin RecyclerView(cardview) onclick 加载详细信息 View

转载 作者:行者123 更新时间:2023-12-02 12:57:12 27 4
gpt4 key购买 nike

我有一个 fragment ,它有一个 RecyclerView,项目在 CardViews 中。我有一个适配器,它将使用来自 newsapi.org 的数据填充 RecyclerView。我需要实现的是当我点击一个项目(CardView)来加载一个带有图像、标题和描述的 Activity 时。我对 kotlin 很陌生,发现我被困在这里,需要帮助才能跟进。真的很有帮助。我将附上我的适配器和 fragment (它有 RecyclerView)。

让我烦恼的是我应该在 onBindViewHolder -> ....cardView.setOnClickListener 中开始 Activity 还是其他?令人困惑的部分是将图像(来自 url)设置为将值传递给详细信息 View 。

适配器类

class ArticleAdapter(
private var articleList: ArrayList<Article>
) : RecyclerView.Adapter<ArticleViewHolder>() {

private val placeHolderImage = "https://picsum.photos/200/200/?blur"
private lateinit var viewGroupContext: Context

override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): ArticleViewHolder {
viewGroupContext = viewGroup.context
val itemView: View =
LayoutInflater.from(viewGroup.context).inflate(R.layout.article_item, viewGroup, false)
return ArticleViewHolder(itemView)
}

override fun getItemCount(): Int {
return articleList.size
}

override fun onBindViewHolder(articleViewHolder: ArticleViewHolder, itemIndex: Int) {
val article: Article = articleList.get(itemIndex)
setPropertiesForArticleViewHolder(articleViewHolder, article)
articleViewHolder.cardView.setOnClickListener {
//do something
}
}

private fun setPropertiesForArticleViewHolder(
articleViewHolder: ArticleViewHolder,
article: Article
) {
checkForUrlToImage(article, articleViewHolder)
articleViewHolder.title.text = article?.title
articleViewHolder.description.text = article?.description
articleViewHolder.url.text = article?.url
}

private fun checkForUrlToImage(article: Article, articleViewHolder: ArticleViewHolder) {
if (article.urlToImage == null || article.urlToImage.isEmpty()) {
Picasso.get()
.load(placeHolderImage)
.centerCrop()
.fit()
.into(articleViewHolder.urlToImage)
} else {
Picasso.get()
.load(article.urlToImage)
.centerCrop()
.fit()
.into(articleViewHolder.urlToImage)
}
}

fun setArticles(articles: ArrayList<Article>) {
articleList = articles
notifyDataSetChanged()
}
}

//interface ItemClickListener{
// fun onItemClick(articleList: Article, position:Int)
//}

fragment

class HomeFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {

// private lateinit var homeViewModel: HomeViewModel
private val ENDPOINT_URL by lazy { "https://newsapi.org/v2/" }
private lateinit var topHeadlinesEndpoint: TopHeadlinesEndpoint
private lateinit var newsApiConfig: String
private lateinit var articleAdapter: ArticleAdapter
private lateinit var articleList: ArrayList<Article>
private lateinit var userKeyWordInput: String
// RxJava related fields
private lateinit var topHeadlinesObservable: Observable<TopHeadlines>
private lateinit var compositeDisposable: CompositeDisposable

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

return inflater.inflate(R.layout.fragment_home, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

swipe_refresh.setOnRefreshListener {
queryTopHeadlines()
// refreshAction() //refresh the list
swipe_refresh.isRefreshing = false
}

//Network request
val retrofit: Retrofit = generateRetrofitBuilder()
topHeadlinesEndpoint = retrofit.create(TopHeadlinesEndpoint::class.java)
newsApiConfig = resources.getString(R.string.api_key)
swipe_refresh.setOnRefreshListener(this)
swipe_refresh.setColorSchemeResources(R.color.colorAccent)
articleList = ArrayList()
articleAdapter = ArticleAdapter(articleList)

// userKeyWordInput = ""

compositeDisposable = CompositeDisposable()
recycler_viewHome.setHasFixedSize(true)
recycler_viewHome.layoutManager = LinearLayoutManager(context)
recycler_viewHome.itemAnimator = DefaultItemAnimator()
recycler_viewHome.adapter = articleAdapter

}
override fun onStart() {
super.onStart()
queryTopHeadlines()
}
override fun onDestroy() {
super.onDestroy()
compositeDisposable.clear()
}
override fun onRefresh() {
queryTopHeadlines()
}

private fun queryTopHeadlines() {
swipe_refresh.isRefreshing = true
topHeadlinesObservable = topHeadlinesEndpoint.getTopHeadlines("us", newsApiConfig)
subscribeObservableOfArticle()
}

private fun subscribeObservableOfArticle() {
articleList.clear()
compositeDisposable.add(
topHeadlinesObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap {
Observable.fromIterable(it.articles)
}
.subscribeWith(createArticleObserver())
)
}

private fun createArticleObserver(): DisposableObserver<Article> {
return object : DisposableObserver<Article>() {
override fun onNext(article: Article) {
if (!articleList.contains(article)) {
articleList.add(article)
}
}

override fun onComplete() {
showArticlesOnRecyclerView()
}

override fun onError(e: Throwable) {
Log.e("createArticleObserver", "Article error: ${e.message}")
}
}
}

private fun showArticlesOnRecyclerView() {
if (articleList.size > 0) {
empty_text.visibility = View.GONE
retry_fetch_button.visibility = View.GONE
recycler_viewHome.visibility = View.VISIBLE
articleAdapter.setArticles(articleList)
} else {
recycler_viewHome.visibility = View.GONE
empty_text.visibility = View.VISIBLE
retry_fetch_button.visibility = View.VISIBLE
// retry_fetch_button.setOnClickListener { checkUserKeywordInput() }
}
swipe_refresh.isRefreshing = false
}

private fun generateRetrofitBuilder(): Retrofit {

return Retrofit.Builder()
.baseUrl(ENDPOINT_URL)
.addConverterFactory(GsonConverterFactory.create())
//Add RxJava2CallAdapterFactory as a Call adapter when building your Retrofit instance
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
}

最佳答案

首先,您需要从 cardView 的 onclick 传递数据,然后启动 Activity ,最后在您想要的项目详细信息 Activity 中处理这些数据...启动 Activity 时需要 context/AppCompatActivity,因此您可以修改 Adapter 的构造函数以接收 Context:

class ArticleAdapter(
private val context: Context,
private var articleList: ArrayList<Article>
) : RecyclerView.Adapter<ArticleViewHolder>()

在从您的 fragment 初始化它时使用这个构造函数:

articleAdapter = ArticleAdapter(activity, articleList) // activity => getActivity()

在您的项目点击监听器中:

 override fun onBindViewHolder(articleViewHolder: ArticleViewHolder, itemIndex: Int) {
val article: Article = articleList?.get(itemIndex)
setPropertiesForArticleViewHolder(articleViewHolder, article)
articleViewHolder.cardView.setOnClickListener {

val titleString = article.title
val descString = article.description
val urlString = article.url

val toPass = Bundle()
toPass.putString("url", urlString)
toPass.putString("title", titleString)
toPass.putString("desc", descString)

val intent =
Intent(context, YourActivity::class.java) //context we got from constructor
intent.putExtras(toPass)
context.startActivity(intent) // or we can use ContextCompat
}
}

现在处理这些数据并相应地设置 View :

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val bundle = intent.extras
val url = bundle?.get("url")
val title = bundle?.get("title")
val desc = bundle?.get("desc")

// now handle those...

titleTextView.text = title!!
// ...

}

关于android - 从 Kotlin RecyclerView(cardview) onclick 加载详细信息 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60407716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com