Skip to content

Commit

Permalink
Bug Fix #277
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Feb 6, 2023
1 parent 100c49f commit 263ff1d
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 171 deletions.
328 changes: 169 additions & 159 deletions app/src/main/java/com/example/zhpan/banner/fragment/PageFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.zhpan.banner.fragment

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RadioGroup
import com.blankj.utilcode.util.ToastUtils
import com.example.zhpan.banner.R
Expand All @@ -18,178 +19,187 @@ import com.zhpan.indicator.enums.IndicatorSlideMode
* Created by zhpan on 2018/7/24.
*/
class PageFragment : BaseFragment() {
private lateinit var mViewPager: BannerViewPager<Int>
private lateinit var mRadioGroupPageStyle: RadioGroup
private lateinit var mRadioGroupMoreStyle: RadioGroup
override val layout: Int
get() = R.layout.fragment_find
private lateinit var mViewPager: BannerViewPager<Int>
private lateinit var mRadioGroupPageStyle: RadioGroup
private lateinit var mRadioGroupMoreStyle: RadioGroup
override val layout: Int
get() = R.layout.fragment_find

override fun initTitle() {}
override fun initView(savedInstanceState: Bundle?, view: View) {
mViewPager = view.findViewById(R.id.banner_view)
mRadioGroupPageStyle = view.findViewById(R.id.rg_page_style)
mRadioGroupMoreStyle = view.findViewById(R.id.rg_more_page_style)
initBVP()
initRadioGroup()
view.findViewById<View>(R.id.rb_multi_page_overlap).performClick()
override fun initTitle() {}
override fun initView(savedInstanceState: Bundle?, view: View) {
mViewPager = view.findViewById(R.id.banner_view)
mRadioGroupPageStyle = view.findViewById(R.id.rg_page_style)
mRadioGroupMoreStyle = view.findViewById(R.id.rg_more_page_style)
view.findViewById<Button>(R.id.btn_next).setOnClickListener {
mViewPager.currentItem = mViewPager.currentItem + 1
itemClick(mViewPager.currentItem)
}
view.findViewById<Button>(R.id.btn_pre).setOnClickListener {
mViewPager.currentItem = mViewPager.currentItem - 1
itemClick(mViewPager.currentItem)
}
initBVP()
initRadioGroup()
view.findViewById<View>(R.id.rb_multi_page_overlap).performClick()
}

private fun initBVP() {
mViewPager.apply {
registerLifecycleObserver(lifecycle)
adapter = ViewBindingSampleAdapter(resources.getDimensionPixelOffset(dimen.dp_8))
setIndicatorSlideMode(IndicatorSlideMode.SCALE)
setIndicatorSliderColor(
getColor(color.red_normal_color),
getColor(color.red_checked_color)
)
setIndicatorSliderRadius(
resources.getDimensionPixelOffset(dimen.dp_4),
resources.getDimensionPixelOffset(dimen.dp_5)
)
setOnPageClickListener ({ _: View, position: Int -> itemClick(position) },true)
setInterval(5000)
}
private fun initBVP() {
mViewPager.apply {
registerLifecycleObserver(lifecycle)
adapter = ViewBindingSampleAdapter(resources.getDimensionPixelOffset(dimen.dp_8))
setIndicatorSlideMode(IndicatorSlideMode.SCALE)
setIndicatorSliderColor(
getColor(color.red_normal_color),
getColor(color.red_checked_color)
)
setIndicatorSliderRadius(
resources.getDimensionPixelOffset(dimen.dp_4),
resources.getDimensionPixelOffset(dimen.dp_5)
)
.setAutoPlay(false)
setOnPageClickListener({ _: View, position: Int -> itemClick(position) }, true)
setInterval(5000)
}
}

private fun initRadioGroup() {
mRadioGroupPageStyle.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
when (checkedId) {
R.id.rb_multi_page_overlap ->
setupBanner(
PageStyle.MULTI_PAGE_OVERLAP,
resources.getDimensionPixelOffset(dimen.dp_10)
)
R.id.rb_multi_page_overlap1 ->
setupBanner(
PageStyle.MULTI_PAGE_OVERLAP,
resources.getDimensionPixelOffset(dimen.dp_100)
)
R.id.rb_multi_page_scale ->
setupBanner(
PageStyle.MULTI_PAGE_SCALE,
resources.getDimensionPixelOffset(dimen.dp_10)
)
R.id.rb_multi_scale_page2 ->
setupBanner(
PageStyle.MULTI_PAGE_SCALE,
resources.getDimensionPixelOffset(dimen.dp_120)
)
R.id.rb_multi_scale_page3 -> {
setupBanner(
PageStyle.MULTI_PAGE_SCALE,
resources.getDimensionPixelOffset(dimen.dp_0),
resources.getDimensionPixelOffset(dimen.dp_200)
)
}
}
}
mRadioGroupMoreStyle.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
when (checkedId) {
R.id.rb_multi_page3 ->
setupMultiPageBanner()
R.id.rb_multi_page4 ->
setupRightPageReveal()
R.id.rb_netease_music_style ->
setNetEaseMusicStyle()
R.id.rb_qq_music_style ->
setQQMusicStyle()
}
private fun initRadioGroup() {
mRadioGroupPageStyle.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
when (checkedId) {
R.id.rb_multi_page_overlap ->
setupBanner(
PageStyle.MULTI_PAGE_OVERLAP,
resources.getDimensionPixelOffset(dimen.dp_10)
)
R.id.rb_multi_page_overlap1 ->
setupBanner(
PageStyle.MULTI_PAGE_OVERLAP,
resources.getDimensionPixelOffset(dimen.dp_100)
)
R.id.rb_multi_page_scale ->
setupBanner(
PageStyle.MULTI_PAGE_SCALE,
resources.getDimensionPixelOffset(dimen.dp_10)
)
R.id.rb_multi_scale_page2 ->
setupBanner(
PageStyle.MULTI_PAGE_SCALE,
resources.getDimensionPixelOffset(dimen.dp_120)
)
R.id.rb_multi_scale_page3 -> {
setupBanner(
PageStyle.MULTI_PAGE_SCALE,
resources.getDimensionPixelOffset(dimen.dp_0),
resources.getDimensionPixelOffset(dimen.dp_200)
)
}
}
}

/**
* Different page styles can be implement by use [BannerViewPager.setPageStyle] and
* [BannerViewPager.setRevealWidth]
*
* @param pageStyle Optional params [PageStyle.MULTI_PAGE_SCALE] and [PageStyle.MULTI_PAGE_OVERLAP]
* @param revealWidth In the multi-page mode, The exposed width of the items on the left and right sides
*/
private fun setupBanner(@APageStyle pageStyle: Int, revealWidth: Int) {
setupBanner(pageStyle, revealWidth, revealWidth)
mRadioGroupMoreStyle.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
when (checkedId) {
R.id.rb_multi_page3 ->
setupMultiPageBanner()
R.id.rb_multi_page4 ->
setupRightPageReveal()
R.id.rb_netease_music_style ->
setNetEaseMusicStyle()
R.id.rb_qq_music_style ->
setQQMusicStyle()
}
}
}

private fun setupBanner(
@APageStyle pageStyle: Int,
leftRevealWidth: Int,
rightRevealWidth: Int
) {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_15))
.setScrollDuration(800)
.setRevealWidth(leftRevealWidth, rightRevealWidth)
.setPageStyle(pageStyle)
.create(getPicList(4))
}
/**
* Different page styles can be implement by use [BannerViewPager.setPageStyle] and
* [BannerViewPager.setRevealWidth]
*
* @param pageStyle Optional params [PageStyle.MULTI_PAGE_SCALE] and [PageStyle.MULTI_PAGE_OVERLAP]
* @param revealWidth In the multi-page mode, The exposed width of the items on the left and right sides
*/
private fun setupBanner(@APageStyle pageStyle: Int, revealWidth: Int) {
setupBanner(pageStyle, revealWidth, revealWidth)
}

/**
* Multi Page Style 1
*/
private fun setupMultiPageBanner() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_10))
.setRevealWidth(resources.getDimensionPixelOffset(dimen.dp_10))
.create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}
private fun setupBanner(
@APageStyle pageStyle: Int,
leftRevealWidth: Int,
rightRevealWidth: Int
) {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_15))
// .setScrollDuration(800)
.setRevealWidth(leftRevealWidth, rightRevealWidth)
.setPageStyle(pageStyle)
.create(getPicList(4))
}

/**
* Multi Page Style 2
*/
private fun setupRightPageReveal() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_10))
.setRevealWidth(0, resources.getDimensionPixelOffset(dimen.dp_30))
.create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}
/**
* Multi Page Style 1
*/
private fun setupMultiPageBanner() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_10))
.setRevealWidth(resources.getDimensionPixelOffset(dimen.dp_10))
.create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}

/**
* QQ Music Banner Style
*/
private fun setNetEaseMusicStyle() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_20))
.setRevealWidth(resources.getDimensionPixelOffset(dimen.dp_m_10))
.setIndicatorSliderColor(
getColor(color.red_normal_color),
getColor(color.red_checked_color)
)
.setOnPageClickListener { view: View?, position: Int ->
ToastUtils.showShort(
"position:$position"
)
}
.setInterval(5000).create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}
/**
* Multi Page Style 2
*/
private fun setupRightPageReveal() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_10))
.setRevealWidth(0, resources.getDimensionPixelOffset(dimen.dp_30))
.create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}

/**
* NetEase Music Banner Style
*/
private fun setQQMusicStyle() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_15))
.setRevealWidth(BannerUtils.dp2px(0f))
.setIndicatorSliderColor(
getColor(color.red_normal_color),
getColor(color.red_checked_color)
)
.setOnPageClickListener { _: View?, position: Int ->
ToastUtils.showShort(
"position:$position"
)
}
.setInterval(5000).create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}
/**
* QQ Music Banner Style
*/
private fun setNetEaseMusicStyle() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_20))
.setRevealWidth(resources.getDimensionPixelOffset(dimen.dp_m_10))
.setIndicatorSliderColor(
getColor(color.red_normal_color),
getColor(color.red_checked_color)
)
.setOnPageClickListener { view: View?, position: Int ->
ToastUtils.showShort(
"position:$position"
)
}
.setInterval(5000).create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}

private fun itemClick(position: Int) {
ToastUtils.showShort("position:$position")
}
/**
* NetEase Music Banner Style
*/
private fun setQQMusicStyle() {
mViewPager
.setPageMargin(resources.getDimensionPixelOffset(dimen.dp_15))
.setRevealWidth(BannerUtils.dp2px(0f))
.setIndicatorSliderColor(
getColor(color.red_normal_color),
getColor(color.red_checked_color)
)
.setOnPageClickListener { _: View?, position: Int ->
ToastUtils.showShort(
"position:$position"
)
}
.setInterval(5000).create(getPicList(4))
mViewPager.removeDefaultPageTransformer()
}

companion object {
val instance: PageFragment
get() = PageFragment()
}
private fun itemClick(position: Int) {
ToastUtils.showShort("position:$position")
}

companion object {
val instance: PageFragment
get() = PageFragment()
}
}
21 changes: 19 additions & 2 deletions app/src/main/res/layout/fragment_find.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
app:bvp_indicator_slide_mode="worm"
app:bvp_page_style="multi_page" />

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pre"/>

<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
/>
</androidx.appcompat.widget.LinearLayoutCompat>

<TextView
android:id="@+id/tv_page_style"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -132,8 +151,6 @@
android:layout_marginTop="@dimen/dp_15"
android:text="NetEase Music Banner Style"
android:textSize="@dimen/sp_16" />


</RadioGroup>

</LinearLayout>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@
<string name="wrong_password">密码错误</string>
<string name="remote_login">您的账号已在其它设备上登录,如非本人操作,请及时修改密码!</string>
<string name="text_page_style_more">More Page Styles:</string>
<string name="next">下一个</string>
<string name="pre">上一个</string>
</resources>
Loading

0 comments on commit 263ff1d

Please sign in to comment.