-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
465 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment3.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment4.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/hi/dhl/demo/binding/viewpager2/ViewPager2Activity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} | ||
|
||
} |
Oops, something went wrong.