Weather app #7 – ViewPager, TabLayout

MainFragment

import android.Manifest
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.FragmentActivity
import com.google.android.material.tabs.TabLayoutMediator
import com.meter_alc_rgb.weatherappcursey.R
import com.meter_alc_rgb.weatherappcursey.adapters.VpAdapter
import com.meter_alc_rgb.weatherappcursey.databinding.FragmentMainBinding

class MainFragment : Fragment() {
private val fList = listOf(
HoursFragment.newInstance(),
DaysFragment.newInstance()
)
private val tList = listOf(
"Hours",
"Days"
)
private lateinit var pLauncher: ActivityResultLauncher<String>
private lateinit var binding: FragmentMainBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentMainBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
checkPermission()
init()
}

private fun init() = with(binding){
val adapter = VpAdapter(activity as FragmentActivity, fList)
vp.adapter = adapter
TabLayoutMediator(tabLayout, vp){
tab, pos -> tab.text = tList[pos]
}.attach()

}

private fun permissionListener(){
pLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()){
Toast.makeText(activity, "Permission is $it", Toast.LENGTH_LONG).show()
}
}

private fun checkPermission(){
if(!isPermissionGranted(Manifest.permission.ACCESS_FINE_LOCATION)){
permissionListener()
pLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}

companion object {
@JvmStatic
fun newInstance() = MainFragment()
}
}

HoursFragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.meter_alc_rgb.weatherappcursey.R

class HoursFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_hours, container, false)
}

companion object {
@JvmStatic
fun newInstance() = HoursFragment()
}
}

DaysFragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.meter_alc_rgb.weatherappcursey.R

class DaysFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_days, container, false)
}

companion object {
@JvmStatic
fun newInstance() = DaysFragment()
}
}

VpAdapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

class VpAdapter(fa: FragmentActivity, private val list: List<Fragment>) : FragmentStateAdapter(fa) {
override fun getItemCount(): Int {
return list.size
}

override fun createFragment(position: Int): Fragment {
return list[position]
}
}

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WeatherAppCurseY" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/white</item>
<item name="colorPrimaryVariant">@color/blue</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

fragment_days.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.DaysFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="DAYS" />

</FrameLayout>

fragment_hours.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HoursFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="HOURS" />

</FrameLayout>

Добавить комментарий

Ваш адрес email не будет опубликован.