从API更改二十个第一篇帖子的颜色 [英] Change color of twenty first post from API

查看:90
本文介绍了从API更改二十个第一篇帖子的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里要做的是更改json占位符api

what I am trying to do here is change the color of the first 20 posts that come from the json place holder api https://jsonplaceholder.typicode.com/posts. Any ideas or suggestion on how to do it would be great.

这是MainActivity

this is the MainActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    val retrofit= Retrofit.Builder()
        .baseUrl("https://jsonplaceholder.typicode.com")
        .addConverterFactory(create())
        .build()

    val api = retrofit.create(ApiService:: class.java)

    api.fetchAllPosts().enqueue(object : Callback<List<Post>> {

        override fun onResponse(call: Call<List<Post>>, response: Response<List<Post>>) {
            showData(response.body()!!)
            Log.d("jv", "onResponse")
        }
        override fun onFailure(call: Call<List<Post>>, t: Throwable) {
            Log.d("jv", "onFailure")
        }
    })

}

private fun showData(posts: List<Post>) {
    recyclerView.apply {
        layoutManager = LinearLayoutManager(this@MainActivity)
        adapter = PostsAdapter(posts)
    }

}

}

这是适配器

class PostsAdapter(private val posts: List<Post>) : RecyclerView.Adapter<PostsAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.post_row, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount() = posts.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.userId.text= "Userid:" + posts[position].userId.toString()
        holder.id1.text= "id:" + posts[position].id.toString()
        holder.title.text= "Title:" + posts[position].title
        holder.body.text= "Body:" + posts[position].body
    }
    class ViewHolder (itemView:View): RecyclerView.ViewHolder(itemView){

        val userId: TextView = itemView.userid
        val id1: TextView = itemView.id1
        val title: TextView = itemView.title
        val body: TextView = itemView.body
    }
}

这是post_row布局

this is the post_row layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
    <ImageView
        android:id="@+id/photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:src="@drawable/ic_check_circle_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </ImageView>
    <TextView
        android:id="@+id/id1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000"
        android:textSize="32sp"
        app:layout_constraintBottom_toTopOf="@+id/userid"
        app:layout_constraintLeft_toRightOf="@+id/photo"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="1" />
    <TextView
        android:id="@+id/userid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000"
        android:textSize="32sp"
        app:layout_constraintBottom_toTopOf="@+id/title"
        app:layout_constraintLeft_toRightOf="@+id/photo"
        app:layout_constraintTop_toBottomOf="@+id/id1"
        tools:text="1" />
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000"
        android:textSize="32sp"
        app:layout_constraintBottom_toTopOf="@+id/body"
        app:layout_constraintLeft_toRightOf="@+id/photo"
        app:layout_constraintTop_toBottomOf="@+id/userid"
        tools:text="JV" />
    <TextView
        android:id="@+id/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000"
        android:textSize="32sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/photo"
        app:layout_constraintTop_toBottomOf="@+id/title"
        tools:text="JV" />
</androidx.constraintlayout.widget.ConstraintLayout>

推荐答案

为约束布局添加ID

<androidx.constraintlayout.widget.ConstraintLayout 
    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:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

定义2种颜色:第1种用于1-20种颜色,第二种对于其余颜色

Define 2 colors: 1st for 1-20 items and 2nd for the rest

 <color name="colorHighlitedPost">...</color>
 <color name="colorNormalPost">...</color>

在回收站适配器中设置颜色

Set color in Recycler Adapter

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.userId.text = "Userid:" + posts[position].userId.toString()
    holder.id1.text = "id:" + posts[position].id.toString()
    holder.title.text = "Title:" + posts[position].title
    holder.body.text = "Body:" + posts[position].body
    val context = holder.rootLayout.context
    if (position < 20) {
        holder.rootLayout.setBackgroundColor(
            ContextCompat.getColor(
                context,
                R.color.colorHighlitedPost
            )
        )
    } else {
        holder.rootLayout.setBackgroundColor(
            ContextCompat.getColor(
                context,
                R.color.colorNormalPost
            )
        )
    }
}

这篇关于从API更改二十个第一篇帖子的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆