MainActivity 完成

This commit is contained in:
LUX-Timber 2024-04-09 17:44:14 +08:00
parent 6de35a0156
commit ed89859d3a
8 changed files with 88 additions and 23 deletions

View File

@ -3,7 +3,20 @@
<component name="deploymentTargetDropDown">
<value>
<entry key="app">
<State />
<State>
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="C:\Users\timbe\.android\avd\Pixel_8_API_34.avd" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-04-09T09:40:09.789743200Z" />
</State>
</entry>
</value>
</component>

1
.idea/gradle.xml generated
View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -6,13 +6,18 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager.VERTICAL
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.timber.soft.myemoticon.model.ChildDataModel
import com.timber.soft.myemoticon.model.RootDataModel
@ -25,7 +30,7 @@ class MainViewPagerFragment(private val rootModel: RootDataModel) : Fragment() {
recyclerViewList.layoutManager = StaggeredGridLayoutManager(2, VERTICAL)
val pagerAdapter = MainHomeCardAdapter(requireContext(),
rootModel,
object : MainHomeCardAdapter.OnItemClickListener {
object : OnItemClickListener {
override fun onItemClick(position: Int, childModel: ChildDataModel) {
// val intent = Intent(requireContext(), DetailsActivity::class.java)
// intent.putExtra("KEY_EXTRA", dataModel)
@ -39,6 +44,10 @@ class MainViewPagerFragment(private val rootModel: RootDataModel) : Fragment() {
}
}
interface OnItemClickListener {
fun onItemClick(position: Int, childModel: ChildDataModel)
}
class MainHomeCardAdapter(
private val context: Context,
private val model: RootDataModel,
@ -52,9 +61,6 @@ class MainHomeCardAdapter(
val recyclerPreview: RecyclerView = itemView.findViewById(R.id.recycler_preview)
}
interface OnItemClickListener {
fun onItemClick(position: Int, childModel: ChildDataModel)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PreViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_home_card, parent, false)
@ -71,7 +77,23 @@ class MainHomeCardAdapter(
holder.recyclerPreview.layoutManager = StaggeredGridLayoutManager(2, VERTICAL)
val cardImgAdapter = CardImgAdapter(context, childModel.previewList, childModel.count)
val customList = mutableListOf<String>()
customList.addAll(childModel.previewList)
customList.add("xxx")
val cardImgAdapter = CardImgAdapter(
context,
customList,
childModel.count,
childModel,
object : OnItemClickListener {
override fun onItemClick(position: Int, childModel: ChildDataModel) {
// val intent = Intent(requireContext(), DetailsActivity::class.java)
// intent.putExtra("KEY_EXTRA", dataModel)
// startActivity(intent)
Log.d("onClick", "item has been click!")
}
})
holder.recyclerPreview.adapter = cardImgAdapter
@ -84,24 +106,49 @@ class MainHomeCardAdapter(
class CardImgAdapter(
private val context: Context,
private val urlList: List<String>,
private val imgCount: Int
private val imgCount: Int,
private val childModel: ChildDataModel,
private val listener: OnItemClickListener
) : RecyclerView.Adapter<CardImgAdapter.ImgViewHolder>() {
inner class ImgViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val layoutRoot: RelativeLayout = itemView.findViewById(R.id.relayout_root)
val preCardImg: ImageView = itemView.findViewById(R.id.pre_card_img)
val preCardCount: TextView = itemView.findViewById(R.id.pre_car_count)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImgViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_home_card_img, parent, false)
return ImgViewHolder(view)
}
override fun getItemCount(): Int {
return urlList.size
}
override fun onBindViewHolder(holder: ImgViewHolder, position: Int) {
override fun onBindViewHolder(holder: ImgViewHolder, position: Int) {
val preUrl: String = urlList[position]
try {
Glide.with(context).load(preUrl)
// 淡入动画
.transition(DrawableTransitionOptions.withCrossFade())
// 加载失败占位图
.into(holder.preCardImg)
} catch (e: Exception) {
e.printStackTrace()
}
if (position == urlList.size-1) {
holder.preCardCount.visibility = View.VISIBLE
holder.preCardCount.text = "+" + imgCount
} else {
holder.preCardCount.visibility = View.GONE
}
holder.layoutRoot.setOnClickListener() {
listener.onItemClick(position, childModel)
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/item_bg_color" />
<solid android:color="@color/main_bg_color" />
</shape>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<corners android:radius="8dp" />
<solid android:color="@color/item_bg_color" />
</shape>

View File

@ -14,19 +14,22 @@
<RelativeLayout
android:layout_width="match_parent"
android:padding="12dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/icon_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:text="@string/item_name"
android:textColor="@color/black"
android:textColor="@color/main_color"
android:textSize="12sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_preview"
android:layout_marginStart="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"

View File

@ -2,7 +2,8 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="12dp"
android:id="@+id/relayout_root"
android:background="@drawable/shape_main_pre_img"
android:orientation="horizontal"
@ -22,10 +23,10 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/shape_main_pre_count"
android:paddingStart="4dp"
android:paddingTop="2dp"
android:paddingEnd="4dp"
android:paddingBottom="2dp"
android:paddingStart="10dp"
android:paddingTop="12dp"
android:paddingEnd="12dp"
android:paddingBottom="12dp"
android:text="@string/item_count"
android:textColor="@color/main_color"
android:textSize="16sp" />

View File

@ -4,5 +4,5 @@
<string name="main_rate">Find us in store</string>
<string name="google_play_link">https://play.google.com/store/apps/details?id=</string>
<string name="item_name">Item Name</string>
<string name="item_count">+6</string>
<string name="item_count">+66</string>
</resources>