Skip to content

Commit

Permalink
feat: example of adding viewPager2
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-dhl committed Apr 10, 2021
1 parent 4b78599 commit 49f1375
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<activity
android:name=".viewbind.ViewBindIncludeActivity"
android:screenOrientation="portrait" />

<activity
android:name=".viewpager2.ViewPager2Activity"
android:screenOrientation="portrait" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.hi.dhl.demo.binding.databind.BindViewStubActivity
import com.hi.dhl.demo.binding.databind.list.DataBindRecycleActivity
import com.hi.dhl.demo.binding.databinding.FragmentViewBindBinding
import com.hi.dhl.demo.binding.navigation.NavigationActivity
import com.hi.dhl.demo.binding.viewpager2.ViewPager2Activity

/**
* <pre>
Expand Down Expand Up @@ -42,7 +43,15 @@ class ViewBindFragment : Fragment(R.layout.fragment_view_bind), View.OnClickList
}

private fun getViews() = with(binding) {
arrayListOf<View>(btnDialog, btnRecycle, btnNavigation, btnDialog, btnStub, btnInclude)
arrayListOf<View>(
btnDialog,
btnRecycle,
btnNavigation,
btnDialog,
btnStub,
btnInclude,
btnViewPager
)
}

override fun onClick(v: View) {
Expand All @@ -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 -> {
}
}
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment1.kt
Original file line number Diff line number Diff line change
@@ -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

/**
* <pre>
* author: dhl
* date : 2020/12/21
* desc :
* </pre>
*/
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()
}
}
}

}
}

}
50 changes: 50 additions & 0 deletions app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment2.kt
Original file line number Diff line number Diff line change
@@ -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

/**
* <pre>
* author: dhl
* date : 2020/12/21
* desc :
* </pre>
*/
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()
}

}

}
32 changes: 32 additions & 0 deletions app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment3.kt
Original file line number Diff line number Diff line change
@@ -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

/**
* <pre>
* author: dhl
* date : 2020/12/21
* desc :
* </pre>
*/
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)
}

}
33 changes: 33 additions & 0 deletions app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment4.kt
Original file line number Diff line number Diff line change
@@ -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

/**
* <pre>
* author: dhl
* date : 2020/12/21
* desc :
* </pre>
*/
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)
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment5.kt
Original file line number Diff line number Diff line change
@@ -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

/**
* <pre>
* author: dhl
* date : 2020/12/21
* desc :
* </pre>
*/
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)
}
}
Original file line number Diff line number Diff line change
@@ -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


/**
* <pre>
* author: dhl
* date : 2021/4/10
* desc :
* </pre>
*/
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<View>(
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)

}
}

}
Loading

0 comments on commit 49f1375

Please sign in to comment.