From f500b8ba2e0b20dd3be5eac6115558c8da8a0bfd Mon Sep 17 00:00:00 2001 From: zlj <390057892@qq.com> Date: Wed, 25 Dec 2019 09:53:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=AE=A1=E7=AE=97=E4=B9=A6?= =?UTF-8?q?=E7=B1=8D=E5=AE=BD=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/novel/read/Ext.kt | 13 +++++++++++ .../com/novel/read/adapter/HumanAdapter.kt | 17 ++++++++------ .../com/novel/read/adapter/RankAdapter.kt | 18 +++++++++------ .../com/novel/read/fragment/ManFragment.kt | 16 ++++++++++---- .../novel/read/utlis/EvenItemDecoration.kt | 22 +++++++++++++++++++ app/src/main/res/layout/rlv_human_item.xml | 8 ++++--- 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/com/novel/read/utlis/EvenItemDecoration.kt diff --git a/app/src/main/java/com/novel/read/Ext.kt b/app/src/main/java/com/novel/read/Ext.kt index 4927cc6..72e8070 100644 --- a/app/src/main/java/com/novel/read/Ext.kt +++ b/app/src/main/java/com/novel/read/Ext.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/java/com/novel/read/adapter/HumanAdapter.kt b/app/src/main/java/com/novel/read/adapter/HumanAdapter.kt index 9ba18ff..94a744a 100644 --- a/app/src/main/java/com/novel/read/adapter/HumanAdapter.kt +++ b/app/src/main/java/com/novel/read/adapter/HumanAdapter.kt @@ -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) : RecyclerView.Adapter() { - 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) } } diff --git a/app/src/main/java/com/novel/read/adapter/RankAdapter.kt b/app/src/main/java/com/novel/read/adapter/RankAdapter.kt index df33195..17dd482 100644 --- a/app/src/main/java/com/novel/read/adapter/RankAdapter.kt +++ b/app/src/main/java/com/novel/read/adapter/RankAdapter.kt @@ -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) : RecyclerView.Adapter() { - private var mContext: Context? = null +class RankAdapter(private val mList: List) : + RecyclerView.Adapter() { + 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) } } diff --git a/app/src/main/java/com/novel/read/fragment/ManFragment.kt b/app/src/main/java/com/novel/read/fragment/ManFragment.kt index 732a084..5b9ede5 100644 --- a/app/src/main/java/com/novel/read/fragment/ManFragment.kt +++ b/app/src/main/java/com/novel/read/fragment/ManFragment.kt @@ -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() { diff --git a/app/src/main/java/com/novel/read/utlis/EvenItemDecoration.kt b/app/src/main/java/com/novel/read/utlis/EvenItemDecoration.kt new file mode 100644 index 0000000..0b20517 --- /dev/null +++ b/app/src/main/java/com/novel/read/utlis/EvenItemDecoration.kt @@ -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) + // 行间距 + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/rlv_human_item.xml b/app/src/main/res/layout/rlv_human_item.xml index f64d107..e93dc5f 100644 --- a/app/src/main/res/layout/rlv_human_item.xml +++ b/app/src/main/res/layout/rlv_human_item.xml @@ -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">