diff --git a/app/build.gradle b/app/build.gradle
index 067cfca..81a6f3c 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -61,6 +61,8 @@ dependencies {
implementation"androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation"androidx.navigation:navigation-ui-ktx:$nav_version"
+ implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02'
+
if (remote) {
implementation 'com.hi-dhl:binding:1.1.2'
} else {
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index f06ff9f..00d8c67 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -47,6 +47,10 @@
@@ -42,7 +43,15 @@ class ViewBindFragment : Fragment(R.layout.fragment_view_bind), View.OnClickList } private fun getViews() = with(binding) { - arrayListOf(btnDialog, btnRecycle, btnNavigation, btnDialog, btnStub, btnInclude) + arrayListOf ( + btnDialog, + btnRecycle, + btnNavigation, + btnDialog, + btnStub, + btnInclude, + btnViewPager + ) } override fun onClick(v: View) { @@ -59,6 +68,7 @@ class ViewBindFragment : Fragment(R.layout.fragment_view_bind), View.OnClickList btnNavigation -> NavigationActivity.startActivity(requireActivity()) btnStub -> BindViewStubActivity.startActivtiy(requireActivity()) btnInclude -> ViewBindIncludeActivity.startActivtiy(requireActivity()) + btnViewPager -> ViewPager2Activity.startActivity(requireActivity()) else -> { } } diff --git a/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment1.kt b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment1.kt new file mode 100644 index 0000000..b231232 --- /dev/null +++ b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment1.kt @@ -0,0 +1,48 @@ +package com.hi.dhl.demo.binding.viewpager2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.viewpager2.adapter.FragmentStateAdapter +import com.hi.dhl.binding.viewbind +import com.hi.dhl.demo.binding.databinding.Fragment1Binding + +/** + * + * author: dhl + * date : 2020/12/21 + * desc : + *+ */ +class Fragment1 : Fragment() { + + val binding: Fragment1Binding by viewbind() + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return binding.root + } + + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + with(binding) { + viewpager2.adapter = object : FragmentStateAdapter(this@Fragment1.requireActivity()) { + override fun getItemCount() = 2 + + override fun createFragment(position: Int): Fragment { + return when (position) { + 0 -> Fragment4() + else -> Fragment5() + } + } + } + + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment2.kt b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment2.kt new file mode 100644 index 0000000..2167f3b --- /dev/null +++ b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment2.kt @@ -0,0 +1,50 @@ +package com.hi.dhl.demo.binding.viewpager2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.navigation.Navigation +import com.hi.dhl.binding.databind +import com.hi.dhl.demo.binding.MainViewModel +import com.hi.dhl.demo.binding.R +import com.hi.dhl.demo.binding.databinding.Fragment1Binding +import com.hi.dhl.demo.binding.databinding.Fragment2Binding +import com.hi.dhl.demo.binding.databinding.FragmentNav2Binding +import org.koin.androidx.viewmodel.ext.android.viewModel + +/** + *+ * author: dhl + * date : 2020/12/21 + * desc : + *+ */ +class Fragment2 : Fragment() { + + val binding: Fragment2Binding by databind() + val viewModel: MainViewModel by viewModel() + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + + return binding.apply { + lifecycleOwner = this@Fragment2 + mainViewModel = viewModel + }.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + with(binding) { + mainViewModel = viewModel + viewModel.generateTimber() + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment3.kt b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment3.kt new file mode 100644 index 0000000..89c5c6f --- /dev/null +++ b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment3.kt @@ -0,0 +1,32 @@ +package com.hi.dhl.demo.binding.viewpager2 + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.Fragment +import com.hi.dhl.binding.viewbind +import com.hi.dhl.demo.binding.R +import com.hi.dhl.demo.binding.databinding.Fragment3Binding + +/** + *+ * author: dhl + * date : 2020/12/21 + * desc : + *+ */ +class Fragment3 : Fragment(R.layout.fragment3) { + + val binding: Fragment3Binding by viewbind() + +// override fun onCreateView( +// inflater: LayoutInflater, container: ViewGroup?, +// savedInstanceState: Bundle? +// ): View { +// return binding.root +// } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment4.kt b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment4.kt new file mode 100644 index 0000000..a2028ba --- /dev/null +++ b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment4.kt @@ -0,0 +1,33 @@ +package com.hi.dhl.demo.binding.viewpager2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.hi.dhl.binding.viewbind +import com.hi.dhl.demo.binding.databinding.Fragment4Binding + +/** + *+ * author: dhl + * date : 2020/12/21 + * desc : + *+ */ +class Fragment4 : Fragment() { + + val binding: Fragment4Binding by viewbind() + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return binding.root + } + + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment5.kt b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment5.kt new file mode 100644 index 0000000..b34a066 --- /dev/null +++ b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment5.kt @@ -0,0 +1,33 @@ +package com.hi.dhl.demo.binding.viewpager2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.hi.dhl.binding.viewbind +import com.hi.dhl.demo.binding.databinding.Fragment5Binding + +/** + *+ * author: dhl + * date : 2020/12/21 + * desc : + *+ */ +class Fragment5 : Fragment() { + + val binding: Fragment5Binding by viewbind() + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return binding.root + } + + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/ViewPager2Activity.kt b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/ViewPager2Activity.kt new file mode 100644 index 0000000..4320aad --- /dev/null +++ b/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/ViewPager2Activity.kt @@ -0,0 +1,88 @@ +package com.hi.dhl.demo.binding.viewpager2 + +import android.app.Activity +import android.content.Intent +import android.os.Bundle +import android.view.View +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.FragmentTransaction +import com.hi.dhl.binding.viewbind +import com.hi.dhl.demo.binding.R +import com.hi.dhl.demo.binding.databinding.ActivityViewpagerBinding + + +/** + *+ * author: dhl + * date : 2021/4/10 + * desc : + *+ */ +class ViewPager2Activity : AppCompatActivity(), View.OnClickListener { + val binding: ActivityViewpagerBinding by viewbind() + var fragment1: Fragment1 = Fragment1() + var fragment2: Fragment2 = Fragment2() + var fragment3: Fragment3 = Fragment3() + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + with(binding) { + + val fragmentTransaction = supportFragmentManager.beginTransaction() + fragmentTransaction.add(R.id.container, fragment3) + fragmentTransaction.add(R.id.container, fragment2) + fragmentTransaction.add(R.id.container, fragment1) + hideFragment(fragmentTransaction) + fragmentTransaction.show(fragment1) + fragmentTransaction.commit() + + getViews().forEach { + it.setOnClickListener(this@ViewPager2Activity) + } + } + + } + + private fun getViews() = with(binding) { + arrayListOf( + tab1, + tab2, + tab3 + ) + } + + companion object { + fun startActivity(activity: Activity) { + activity.startActivity(Intent(activity, ViewPager2Activity::class.java)) + } + } + + override fun onClick(v: View) { + val fragmentTransaction = supportFragmentManager.beginTransaction() + hideFragment(fragmentTransaction) + with(binding) { + when (v) { + tab1 -> { + fragmentTransaction.show(fragment1) + } + tab2 -> { + fragmentTransaction.show(fragment2) + } + tab3 -> fragmentTransaction.show(fragment3) + else -> { + } + } + } + fragmentTransaction.commitNow() + } + + private fun hideFragment(fragmentTransaction: FragmentTransaction) { + getViews().forEach { + fragmentTransaction.hide(fragment1) + fragmentTransaction.hide(fragment2) + fragmentTransaction.hide(fragment3) + + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_viewpager.xml b/app/src/main/res/layout/activity_viewpager.xml new file mode 100644 index 0000000..c9cbf23 --- /dev/null +++ b/app/src/main/res/layout/activity_viewpager.xml @@ -0,0 +1,63 @@ + + + + diff --git a/app/src/main/res/layout/fragment1.xml b/app/src/main/res/layout/fragment1.xml new file mode 100644 index 0000000..3bdc5b2 --- /dev/null +++ b/app/src/main/res/layout/fragment1.xml @@ -0,0 +1,15 @@ + ++ + + + + + + + ++ + + + + + + + diff --git a/app/src/main/res/layout/fragment2.xml b/app/src/main/res/layout/fragment2.xml new file mode 100644 index 0000000..82ae78a --- /dev/null +++ b/app/src/main/res/layout/fragment2.xml @@ -0,0 +1,34 @@ + ++ + + + + + diff --git a/app/src/main/res/layout/fragment3.xml b/app/src/main/res/layout/fragment3.xml new file mode 100644 index 0000000..360681c --- /dev/null +++ b/app/src/main/res/layout/fragment3.xml @@ -0,0 +1,14 @@ + ++ + + + + + ++ + + + + diff --git a/app/src/main/res/layout/fragment4.xml b/app/src/main/res/layout/fragment4.xml new file mode 100644 index 0000000..2d0a60d --- /dev/null +++ b/app/src/main/res/layout/fragment4.xml @@ -0,0 +1,15 @@ + ++ + + + diff --git a/app/src/main/res/layout/fragment5.xml b/app/src/main/res/layout/fragment5.xml new file mode 100644 index 0000000..24abb1c --- /dev/null +++ b/app/src/main/res/layout/fragment5.xml @@ -0,0 +1,15 @@ + ++ + + + + diff --git a/app/src/main/res/layout/fragment_view_bind.xml b/app/src/main/res/layout/fragment_view_bind.xml index bcb9b3d..b266b39 100644 --- a/app/src/main/res/layout/fragment_view_bind.xml +++ b/app/src/main/res/layout/fragment_view_bind.xml @@ -57,4 +57,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" /> ++ + + +