动态计算书籍宽度

old
zlj 5 years ago
parent 2481fa242c
commit f500b8ba2e
  1. 13
      app/src/main/java/com/novel/read/Ext.kt
  2. 17
      app/src/main/java/com/novel/read/adapter/HumanAdapter.kt
  3. 18
      app/src/main/java/com/novel/read/adapter/RankAdapter.kt
  4. 16
      app/src/main/java/com/novel/read/fragment/ManFragment.kt
  5. 22
      app/src/main/java/com/novel/read/utlis/EvenItemDecoration.kt
  6. 8
      app/src/main/res/layout/rlv_human_item.xml

@ -7,3 +7,16 @@ import android.widget.Toast
fun Context.showToast(msg:String){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
}
fun Context.getScreenContentWidth(): Int {
val displayMetrics = this.resources.displayMetrics
return displayMetrics.widthPixels
}
fun Context.dp2px(dps: Int): Int {
return Math.round(dps.toFloat() * getDensityDpiScale(this))
}
fun getDensityDpiScale(context: Context): Float {
return context.resources.displayMetrics.xdpi / 160.0f
}

@ -13,32 +13,35 @@ import androidx.recyclerview.widget.RecyclerView
import com.novel.read.R
import com.novel.read.activity.NovelBookDetailActivity
import com.novel.read.constants.Constant
import com.novel.read.dp2px
import com.novel.read.getScreenContentWidth
import com.novel.read.model.protocol.RecommendListResp
import com.novel.read.utlis.GlideImageLoader
import kotlinx.android.synthetic.main.rlv_human_item.view.*
/**
* create by zlj on 2019/6/19
* describe:
*/
class HumanAdapter(private val mList: List<RecommendListResp.ListBean>) : RecyclerView.Adapter<HumanAdapter.ViewHolder>() {
private var mContext: Context? = null
private lateinit var mContext: Context
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
if (mContext == null) {
mContext = viewGroup.context
}
mContext = viewGroup.context
val view = LayoutInflater.from(mContext).inflate(R.layout.rlv_human_item, viewGroup, false)
val ivSize = (mContext.getScreenContentWidth() - mContext.dp2px(10) * 4) / 3
view.cl_layout.layoutParams.width = ivSize
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
viewHolder.mTvBookName.text = mList[i].book_title
viewHolder.mTvAuthor.text = mList[i].author
GlideImageLoader.displayCornerImage(mContext!!, mList[i].book_cover!!, viewHolder.mIvBook)
viewHolder.itemView.setOnClickListener { view ->
GlideImageLoader.displayCornerImage(mContext, mList[i].book_cover!!, viewHolder.mIvBook)
viewHolder.itemView.setOnClickListener {
val intent = Intent(mContext, NovelBookDetailActivity::class.java)
intent.putExtra(Constant.Bundle.BookId, mList[i].book_id)
mContext!!.startActivity(intent)
mContext.startActivity(intent)
}
}

@ -13,32 +13,36 @@ import androidx.recyclerview.widget.RecyclerView
import com.novel.read.R
import com.novel.read.activity.NovelBookDetailActivity
import com.novel.read.constants.Constant
import com.novel.read.dp2px
import com.novel.read.getScreenContentWidth
import com.novel.read.model.protocol.RecommendListResp
import com.novel.read.utlis.GlideImageLoader
import kotlinx.android.synthetic.main.rlv_human_item.view.*
/**
* create by zlj on 2019/6/20
* describe:
*/
class RankAdapter(private val mList: List<RecommendListResp.ListBean>) : RecyclerView.Adapter<RankAdapter.ViewHolder>() {
private var mContext: Context? = null
class RankAdapter(private val mList: List<RecommendListResp.ListBean>) :
RecyclerView.Adapter<RankAdapter.ViewHolder>() {
private lateinit var mContext: Context
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
if (mContext == null) {
mContext = viewGroup.context
}
mContext = viewGroup.context
val view = LayoutInflater.from(mContext).inflate(R.layout.rlv_human_item, viewGroup, false)
val ivSize = (mContext.getScreenContentWidth() - mContext.dp2px(10) * 4) / 3
view.cl_layout.layoutParams.width = ivSize
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
viewHolder.mTvBookName.text = mList[i].book_title
viewHolder.mTvAuthor.text = mList[i].author
GlideImageLoader.displayCornerImage(mContext!!, mList[i].book_cover!!, viewHolder.mIvBook)
GlideImageLoader.displayCornerImage(mContext, mList[i].book_cover!!, viewHolder.mIvBook)
viewHolder.itemView.setOnClickListener {
val intent = Intent(mContext, NovelBookDetailActivity::class.java)
intent.putExtra(Constant.Bundle.BookId, mList[i].book_id)
mContext!!.startActivity(intent)
mContext.startActivity(intent)
}
}

@ -10,8 +10,10 @@ import com.novel.read.adapter.HumanAdapter
import com.novel.read.adapter.RankAdapter
import com.novel.read.base.NovelBaseFragment
import com.novel.read.constants.Constant
import com.novel.read.dp2px
import com.novel.read.http.AccountManager
import com.novel.read.model.protocol.RecommendListResp
import com.novel.read.utlis.EvenItemDecoration
import kotlinx.android.synthetic.main.fragment_man.*
import retrofit2.Call
import retrofit2.Callback
@ -35,9 +37,12 @@ class ManFragment : NovelBaseFragment() {
override fun initView() {
EventManager.instance.registerSubscriber(this)
rlv_pop.layoutManager = GridLayoutManager(activity, 3)
mHumanAdapter = HumanAdapter(mHumanList)
rlv_pop.adapter = mHumanAdapter
rlv_pop.apply {
layoutManager = GridLayoutManager(activity, 3)
adapter = mHumanAdapter
addItemDecoration(EvenItemDecoration(activity!!.dp2px(10), 3))
}
val linearLayoutManager = LinearLayoutManager(activity)
linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
@ -45,9 +50,12 @@ class ManFragment : NovelBaseFragment() {
mEditAdapter = EditRecommendAdapter(mEditList)
rlv_recommend.adapter = mEditAdapter
rlv_update.layoutManager = GridLayoutManager(activity, 3)
mRankAdapter = RankAdapter(mRankList)
rlv_update.adapter = mRankAdapter
rlv_update.apply {
layoutManager = GridLayoutManager(activity, 3)
adapter = mRankAdapter
addItemDecoration(EvenItemDecoration(activity!!.dp2px(10), 3))
}
}
override fun initData() {

@ -0,0 +1,22 @@
package com.novel.read.utlis
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
/**
* 设置adapter padding
*/
class EvenItemDecoration(private val space: Int, private val column: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position = parent.getChildAdapterPosition(view)
// 每个span分配的间隔大小
val spanSpace = space * (column +1) / column
// 列索引
val colIndex = position % column
// 列左、右间隙
outRect.left = space * (colIndex + 1) - spanSpace * colIndex
outRect.right = spanSpace * (colIndex + 1) - space * (colIndex + 1)
// 行间距
}
}

@ -6,16 +6,18 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp">
android:id="@+id/cl_layout">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_book"
android:layout_width="100dp"
android:layout_height="140dp"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="h,9:13"
android:scaleType="fitXY"
android:src="@drawable/cover_default"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView

Loading…
Cancel
Save