init_adapter
init_ViewPager
This commit is contained in:
parent
77133dd6b5
commit
7fc42259a9
@ -50,4 +50,6 @@ dependencies {
|
|||||||
|
|
||||||
|
|
||||||
implementation 'com.google.code.gson:gson:2.10.1'
|
implementation 'com.google.code.gson:gson:2.10.1'
|
||||||
|
implementation 'com.github.bumptech.glide:glide:4.12.0'
|
||||||
|
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
|
||||||
}
|
}
|
||||||
@ -2,7 +2,11 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@ -32,6 +32,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
private lateinit var fragmentList: ArrayList<Fragment>
|
private lateinit var fragmentList: ArrayList<Fragment>
|
||||||
|
private val rootModelList: MutableList<RootModel> = mutableListOf()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -62,7 +63,27 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
initDrawer()
|
initDrawer()
|
||||||
|
|
||||||
val rootModelList: MutableList<RootModel> = mutableListOf()
|
initTabLayOut()
|
||||||
|
|
||||||
|
binding.viewpager.offscreenPageLimit = 3
|
||||||
|
binding.viewpager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
|
||||||
|
override fun getCount(): Int {
|
||||||
|
return fragmentList.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItem(position: Int): Fragment {
|
||||||
|
return fragmentList[position]
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getPageTitle(position: Int): CharSequence {
|
||||||
|
return rootModelList[position].className
|
||||||
|
}
|
||||||
|
}
|
||||||
|
binding.tabLayout.setupWithViewPager(binding.viewpager)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initTabLayOut() {
|
||||||
val result = parseJsonFromAssets(this@MainActivity, "keyboard.json")
|
val result = parseJsonFromAssets(this@MainActivity, "keyboard.json")
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
rootModelList.addAll(result)
|
rootModelList.addAll(result)
|
||||||
@ -101,28 +122,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// null
|
// null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
binding.viewpager.offscreenPageLimit = 3
|
|
||||||
binding.viewpager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
|
|
||||||
override fun getCount(): Int {
|
|
||||||
return fragmentList.size
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItem(position: Int): Fragment {
|
|
||||||
return fragmentList[position]
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPageTitle(position: Int): CharSequence? {
|
|
||||||
return rootModelList[position].className
|
|
||||||
}
|
|
||||||
}
|
|
||||||
binding.tabLayout.setupWithViewPager(binding.viewpager)
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun setTabSize(p0: TabLayout.Tab?) {
|
private fun setTabSize(p0: TabLayout.Tab?) {
|
||||||
val textView = TextView(this)
|
val textView = TextView(this)
|
||||||
//字体样式
|
//字体样式
|
||||||
|
|||||||
@ -0,0 +1,77 @@
|
|||||||
|
package com.timber.soft.newkeyboard.adapter
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.LinearLayout
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
|
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||||
|
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
|
||||||
|
import com.timber.soft.newkeyboard.R
|
||||||
|
import com.timber.soft.newkeyboard.model.DataModel
|
||||||
|
import com.timber.soft.newkeyboard.model.RootModel
|
||||||
|
|
||||||
|
class MyPagerAdapter(
|
||||||
|
private val context: Context,
|
||||||
|
private val model: RootModel,
|
||||||
|
private val listener: OnItemClickListener
|
||||||
|
) : RecyclerView.Adapter<MyPagerAdapter.PreViewHolder>() {
|
||||||
|
private val dataModels = model.list
|
||||||
|
|
||||||
|
inner class PreViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
fun loadPreImg(context: Context, thumb: String, imgItemView: ImageView) {
|
||||||
|
try {
|
||||||
|
Glide.with(context).load(thumb)
|
||||||
|
// 缓存
|
||||||
|
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
||||||
|
// 加载占位图
|
||||||
|
// .apply(
|
||||||
|
// RequestOptions().placeholder(R.drawable.img_loading)
|
||||||
|
// )
|
||||||
|
// 淡入动画
|
||||||
|
.transition(DrawableTransitionOptions.withCrossFade())
|
||||||
|
// 加载失败占位图
|
||||||
|
// .error(R.drawable.img_loading_err)
|
||||||
|
.into(imgItemView)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取需要渲染图片的item
|
||||||
|
val imgItemView: ImageView = itemView.findViewById(R.id.image_item)
|
||||||
|
|
||||||
|
// 获取图片根节点
|
||||||
|
val rootItemLayout = itemView.findViewById<LinearLayout>(R.id.root_layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(
|
||||||
|
parent: ViewGroup, viewType: Int
|
||||||
|
): MyPagerAdapter.PreViewHolder {
|
||||||
|
val view = LayoutInflater.from(context).inflate(R.layout.item_pre_img, parent, false)
|
||||||
|
return PreViewHolder(view)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: MyPagerAdapter.PreViewHolder, position: Int) {
|
||||||
|
val dataModel = dataModels[position % dataModels.size]
|
||||||
|
holder.loadPreImg(context, dataModel.thumb, holder.imgItemView)
|
||||||
|
|
||||||
|
holder.rootItemLayout.setOnClickListener() {
|
||||||
|
listener.onItemClick(position, dataModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int {
|
||||||
|
return Int.MAX_VALUE
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OnItemClickListener {
|
||||||
|
fun onItemClick(position: Int, dataModel: DataModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,9 +1,53 @@
|
|||||||
package com.timber.soft.newkeyboard.fragment
|
package com.timber.soft.newkeyboard.fragment
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager.VERTICAL
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import androidx.recyclerview.widget.StaggeredGridLayoutManager
|
||||||
|
import androidx.viewpager.widget.PagerAdapter
|
||||||
|
import com.timber.soft.newkeyboard.R
|
||||||
|
import com.timber.soft.newkeyboard.adapter.MyPagerAdapter
|
||||||
|
import com.timber.soft.newkeyboard.model.DataModel
|
||||||
import com.timber.soft.newkeyboard.model.RootModel
|
import com.timber.soft.newkeyboard.model.RootModel
|
||||||
|
|
||||||
class VPFragment(private val rootModel: RootModel) : Fragment() {
|
class VPFragment(private val rootModel: RootModel) : Fragment() {
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
|
||||||
|
): View? {
|
||||||
|
val view = inflater.inflate(R.layout.fragment_pre_recyclerview, container, false)
|
||||||
|
|
||||||
|
val infoRecyclerView: RecyclerView = view.findViewById(R.id.info_recycler_view)
|
||||||
|
|
||||||
|
infoRecyclerView.layoutManager = StaggeredGridLayoutManager(2, VERTICAL)
|
||||||
|
|
||||||
|
val pagerAdapter = MyPagerAdapter(
|
||||||
|
requireContext(), rootModel,
|
||||||
|
object : MyPagerAdapter.OnItemClickListener {
|
||||||
|
override fun onItemClick(position: Int, dataModel: DataModel) {
|
||||||
|
// val intent = Intent(requireContext(), SetImgActivity::class.java)
|
||||||
|
// intent.putExtra("KEY_EXTRA", dataModel)
|
||||||
|
|
||||||
|
Log.e("onClick", "item has been click!")
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"item has been click!",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
infoRecyclerView.adapter = pagerAdapter
|
||||||
|
return view
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@
|
|||||||
android:id="@+id/viewpager"
|
android:id="@+id/viewpager"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_marginTop="4dp"
|
android:layout_marginTop="8dp"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/tabLayout" />
|
app:layout_constraintTop_toBottomOf="@id/tabLayout" />
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|||||||
9
app/src/main/res/layout/fragment_pre_recyclerview.xml
Normal file
9
app/src/main/res/layout/fragment_pre_recyclerview.xml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:id="@+id/info_recycler_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
tools:context="com.timber.soft.newkeyboard.fragment.VPFragment">
|
||||||
|
</androidx.recyclerview.widget.RecyclerView>
|
||||||
20
app/src/main/res/layout/item_pre_img.xml
Normal file
20
app/src/main/res/layout/item_pre_img.xml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/root_layout"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingBottom="8dp"
|
||||||
|
android:layout_marginStart="8dp">
|
||||||
|
|
||||||
|
<androidx.cardview.widget.CardView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:cardCornerRadius="18dp">
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/image_item"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="156dp"
|
||||||
|
android:scaleType="fitXY" />
|
||||||
|
</androidx.cardview.widget.CardView>
|
||||||
|
</LinearLayout>
|
||||||
@ -4,7 +4,7 @@
|
|||||||
<color name="white">#FFFFFFFF</color>
|
<color name="white">#FFFFFFFF</color>
|
||||||
|
|
||||||
|
|
||||||
<color name="theme_color">#FF9800</color>
|
<color name="theme_color">#FFBB86FC</color>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in New Issue
Block a user