Skip to content

Commit

Permalink
2.1版本
Browse files Browse the repository at this point in the history
1. 修复item快速点击,TabColorTextView 还有残留的问题
2. 添加 chooseItem,结合setCurrentItem(position,false)使用
  • Loading branch information
Zzhengsr committed Mar 16, 2022
1 parent 4db0d46 commit 407c65c
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ allprojects {

**如果你的工程的是androidx且想支持ViewPager2,可以关联tablibx库**
```
implementation 'com.github.LillteZheng:FlowHelper:vv2.0'
implementation 'com.github.LillteZheng:FlowHelper:vv2.1'
```
**support可以关联以下连接,请尽快切到androidx,support包不再维护**
```
Expand Down Expand Up @@ -144,6 +144,7 @@ implementation 'com.github.LillteZheng:FlowHelper:vv2.0'


## 版本信息:
- v2.1 : 修改TabVpFlowLayout 快速点击时,TabColorTextView 还有残留的问题
- v2.0 : 内置部分控件,减少接入成本,并优化一些bug,和关闭demo ,viewpager 内存泄露的问题
- v1.37 : 增加 tab_width_equals_text ,让 rect 根据 text 的长度变化,修复 TabColorTextView 加粗不起作用的问题
- v1.32 : 修复res和round,tab_margin_x 不起作用的问题
Expand Down
3 changes: 2 additions & 1 deletion TAB_README3_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ flowLayout.setViewPager(viewpager) .

**为了避免卡顿,当viewpager结合fragment时,可以有以下优化手段:**
- fragment 布局复杂或者网络加载数据时,建议在懒加载中去初始化或者加载数据
- viewpager 增加缓存,setOffscreenPageLimit(3)。
- ViewPager 增加缓存,setOffscreenPageLimit(3)。
- ViewPager 选择页面的方法设置为 setCurrentItem(position,false);再调用 chooseItem(position) 方法


**如果您觉得viewpager切换太快,可以使用 ViewPagerHelperUtils.initSwitchTime(getContext(), viewPager, 600) 改变滚动速度**
Expand Down
3 changes: 3 additions & 0 deletions appx/src/main/java/com/zhengsr/tabhelper/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
Expand All @@ -29,6 +30,8 @@ public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text);
textView.setTextColor(getResources().getColor(R.color.color_selector));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected void onCreate(Bundle savedInstanceState) {
triFlow();
roundFlow();
resFlow();
colorFlow();
cusFlow();
colorFlow();
}

boolean isDetele = false;
Expand Down Expand Up @@ -129,8 +129,8 @@ private void colorFlow() {
int t = DisplayUtil.dip2px(this, 6);
TextConfig textConfig = new TextConfig()
.setPadding(l, t, l, t)
.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
.setTextSize(18);
.setTypeface(Typeface.DEFAULT_BOLD)
.setTextSize(14);

TabConfig config = new TabConfig.Builder()
.setViewpager(mViewPager)
Expand Down
1 change: 1 addition & 0 deletions appx/src/main/res/color/color_selector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

<item android:color="@color/white" android:state_selected="true"/>
<item android:color="@color/black_ff_bg" android:state_selected="false"/>
<item android:color="@color/white" />
</selector>
7 changes: 7 additions & 0 deletions appx/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
android:background="#506E7A"
tools:context=".MainActivity">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试textcolor"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.zhengsr.tablib.view.action;

import android.util.Log;

import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

Expand All @@ -14,6 +12,7 @@ public class BViewPager {
protected ViewPager2 mViewPager2;
protected ViewPager mViewPager;
private Pager2Listener mPager2Listener;
protected boolean isChooseItemWhenPageSelected = false;
public BViewPager setViewPager(ViewPager2 viewPager){
if (viewPager != null) {
mViewPager2 = viewPager;
Expand Down Expand Up @@ -43,6 +42,12 @@ public ViewPager2 getViewPager2() {
return mViewPager2;
}

public void chooseItemWhenPageSelected() {
isChooseItemWhenPageSelected = true;
}



private class PagerListener implements ViewPager.OnPageChangeListener{

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;
Expand Down Expand Up @@ -291,8 +290,34 @@ public void onPageSelected(int position) {
mLastIndex = mCurrentIndex;
mCurrentIndex = position;
chooseSelectedPosition(position);
checkIfNeedScroll(position);

}

private void checkIfNeedScroll(int position){
if (isChooseItemWhenPageSelected) {
if (mParentView != null) {
if (mParentView.isCanMove()) {
View curView = mParentView.getChildAt(position);
int scrollX = (int) (curView.getLeft());
if (scrollX > (mViewWidth / 2 - mParentView.getPaddingLeft())) {
scrollX -= (mViewWidth / 2 - mParentView.getPaddingLeft());
//有边界提醒
if (scrollX <= mRightBound - mViewWidth) {
mParentView.scrollTo(scrollX, 0);
} else {
int dx = mRightBound - mViewWidth;
mParentView.scrollTo(dx, 0);
}
} else {
mParentView.scrollTo(0, 0);
}
}
isChooseItemWhenPageSelected = false;
}

}
}

@Override
public void onPageScrollStateChanged(int state) {
Expand Down Expand Up @@ -472,10 +497,10 @@ public void onAnimationEnd(Animator animation) {
*/
public void chooseSelectedPosition(int position) {
if (mParentView != null) {
if (isTextView && !isColorText) {
int childCount = mParentView.getChildCount();
for (int i = 0; i < childCount; i++) {
TextView textView = mParentView.getTextView(i);
int childCount = mParentView.getChildCount();
for (int i = 0; i < childCount; i++) {
TextView textView = mParentView.getTextView(i);
if (textView != null) {
if (i == position) {
if (mSelectedColor != FlowConstants.COLOR_ILLEGAL) {
textView.setTextColor(mSelectedColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,17 @@ public AbsFlowLayout setDefaultPosition(int position) {
mCurrentIndex = position;
return this;
}

/**
* 选择某个item,建议 viewpager setCurrentItem(position,false);
* 此方法是为了优化当 viewpager 页面过多,从第一页选择到最后一页,此时
* ViewPaper onPageScrolled 执行过多,导致滚动异常的问题。
* @param position
*/
public void chooseItem(int position){
mLastIndex = mCurrentIndex;
mCurrentIndex = position;
mAction.chooseIndex(mLastIndex,position);
mAction.chooseItemWhenPageSelected();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ private TextView getTextview(int pos,int textType){
}
textView = colorTextView;


}else {
textView = new TextView(getContext());
textView.setTextColor(Color.BLACK);
Expand Down

0 comments on commit 407c65c

Please sign in to comment.