Skip to content

Commit

Permalink
添加均分
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzhengsr committed Mar 11, 2020
1 parent 72de947 commit af84691
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 24 deletions.
8 changes: 8 additions & 0 deletions Label_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ LabelFlowLayout 竖向布局,支持自动换行,单选、多选、长按等

**它的状态变化,根据 view 的 selected 来,所以大家可以写 selector 当背景,或者在方法中自己设置**


**LabelFlowLayout 支持以下效果:**
- **单选**
- **多选**
- **长按**
- **显示更多**
- **收起**

### 3.3.1 使用

LabelFlowLayout 默认单选,在 xml 这样配置:
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ allprojects {
```

```
implementation 'com.github.LillteZheng:FlowHelper:v1.21'
implementation 'com.github.LillteZheng:FlowHelper:v1.22'
```

**如果要支持 AndroidX ,如果你的工程已经有以下代码,直接关联即可:**
Expand Down Expand Up @@ -77,6 +77,17 @@ android.enableJetifier=true

</table>

**目前TabFlowLayout 支持以下效果:**
- **矩形**
- **三角形**
- **圆角**
- **shape 或者 bitmap 等资源**
- **自定义功能**
- **放大Item效果,与上述效果可共用**
- **颜色渐变效果,需要使用 TabColorTextView 控件,与上述效果可共用,只支持有viewpager 的情况**
- **竖直效果,需要设置 tab_orientation = vertical**
- **宽度均分**

[TabFlowLayout 使用说明](https://github.com/LillteZheng/FlowHelper/blob/master/TAB_README.md)

## LabelFlowLayout 效果图
Expand All @@ -93,6 +104,15 @@ android.enableJetifier=true

</table>


**LabelFlowLayout 支持以下效果:**
- **单选**
- **多选**
- **长按**
- **显示更多**
- **收起**


[LabelFlowLayout 使用说明](https://github.com/LillteZheng/FlowHelper/blob/master/Label_README.md)


Expand Down
14 changes: 12 additions & 2 deletions TAB_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- **放大Item效果,与上述效果可共用**
- **颜色渐变效果,需要使用 TabColorTextView 控件,与上述效果可共用,只支持有viewpager 的情况**
- **竖直效果,需要设置 tab_orientation = vertical**
- **宽度均分**


## 三、使用
Expand Down Expand Up @@ -286,7 +287,16 @@ private void resFlow(){
```
通过重写 valueChange 拿到移动偏移量,然后通过 flowLayout.setAction(new CircleAction()) 即可。如果是竖直方向,拿到 value.top,value.bottom 再去写逻辑即可。

### 3.1.4、参考代码
### 3.1.4 宽度均分

上面中,item_layout 的宽度都是通过测量自身的,如果想要均分宽度怎么做呢?

可以设置 tab_visual_count ,当前可视个数来试下,比如当前可视界面只显示3个:

<img src="https://github.com/LillteZheng/FlowHelper/raw/master/gif/tab_visual_count.gif" align="left" height="789" width="479">


### 3.1.5、参考代码

上面的效果,可以参考以下代码:

Expand Down Expand Up @@ -343,7 +353,7 @@ private void resFlow(){
|tab_orientation|integer|vertical竖直防线,horizontal横向,默认横向|
|tab_action_orientaion|integer|left坐标,right右边,只支持 tri、rect 两种效果|
|tab_isAutoScroll|boolean|是否支持自动滚动,默认为true|

|tab_visual_count|integer|可视化个数,比如有一排,我们就只要显示4个,此时宽度均分|


**TabColorTextView**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void rectFlow(){
.setTextId(R.id.item_text)
.setSelectedColor(Color.WHITE)
.setUnSelectedColor(getResources().getColor(R.color.unselect));
flowLayout.setAdapter(new TabFlowAdapter<String>(R.layout.item_msg,mTitle) {
flowLayout.setAdapter(new TabFlowAdapter<String>(R.layout.item_test,mTitle) {
@Override
public void bindView(View view, String data, int position) {
setText(view,R.id.item_text,data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void bindView(View view, String data, int position) {
});

TabFlowLayout flowLayout2 = findViewById(R.id.rectflow2);
flowLayout2.setAdapter(new TabFlowAdapter<String>(R.layout.item_msg,mTitle) {
flowLayout2.setAdapter(new TabFlowAdapter<String>(R.layout.item_test,mTitle) {
@Override
public void bindView(View view, String data, int position) {
setText(view,R.id.item_text,data);
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_tab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
android:background="@color/black_ff_bg"
app:tab_scale_factor="1.2"
app:tab_item_autoScale="true"
app:tab_visual_count="4"
android:layout_marginStart="20dp"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_tab_no_view_pager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
app:tab_type="rect"
app:tab_color="@color/colorPrimary"
app:tab_height="3dp"
app:tab_width="20dp"
app:tab_margin_b="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
app:tab_visual_count="3"
android:layout_height="wrap_content"/>

<com.zhengsr.tablib.view.flow.TabFlowLayout
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/item_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_text"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="14sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="@color/white"
android:layout_height="match_parent">

</TextView>
Binary file modified gif/tab_viewpager.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/tab_visual_count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tablib/src/main/java/com/zhengsr/tablib/bean/TabBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public class TabBean {
*/
public boolean isAutoScroll = true;

/**
* 可视个数
*/
public int visualCount = -1;


@Override
public String toString() {
Expand Down
36 changes: 31 additions & 5 deletions tablib/src/main/java/com/zhengsr/tablib/view/flow/FlowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ class FlowLayout extends ViewGroup {
private static final String TAG = "FlowLayout";
protected int mViewWidth;
protected int mViewHeight;
/**
* tabflowlayout
*/
private int mTabOrientation;
protected int mVisualCount = -1;
/**
* label
* labelflowlayout
*/
private List<Integer> mLineHeights = new ArrayList<>();
private List<List<View>> mAllViews = new ArrayList<>();
Expand Down Expand Up @@ -60,6 +64,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


}
public void setVisualCount(int visualCount){
mVisualCount = visualCount;
}

/**
* 设置行数
Expand Down Expand Up @@ -150,23 +157,42 @@ private void measureTabHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
* 计算宽高
*/


int widthPixels = getResources().getDisplayMetrics().widthPixels;
if (MeasureSpec.EXACTLY == widthMode) {
widthPixels = widthSize;
}


for (int i = 0; i < childCount; i++) {



View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
measureChild(child, widthMeasureSpec, heightMeasureSpec);

MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();

//拿到 子控件宽高 + margin
int cw = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
int cw;
if (mVisualCount != -1){
cw = widthPixels/mVisualCount;
params.width = cw;
child.setLayoutParams(params);
width = widthPixels;
}else{
cw = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
width += cw;
}
int ch = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;

width += cw;
//拿到 子控件高度,拿到最大的那个高度
height = Math.max(height, ch);




}

//具体大小,padding不受影响
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
/**
* TabFlowLayout 或者 LabelFlowLayout 的竖向滚动
*/
//todo 这是什么魔性 if else 找机会改一下
if (isVerticalMove()){
if (mViewHeight < mScreenHeight){
if (mBottomRound > mViewHeight){
Expand All @@ -95,22 +96,32 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
}else{
//TabFlowLayout 横向布局
if (!isVertical()) {
//说明控件没有满屏或者固定宽度
if (mViewWidth < mScreenWidth) {
if (mRightBound > mViewWidth) {
//如果是固定宽度
if (mVisualCount != -1){
if (getChildCount() > mVisualCount){
isCanMove = true;
} else {
}else{
isCanMove = false;
}
mWidth = mViewWidth;
} else {
//再确认一遍
if (mRightBound > mScreenWidth) {
isCanMove = true;
}else {
//说明控件没有满屏或者固定宽度
if (mViewWidth < mScreenWidth) {
if (mRightBound > mViewWidth) {
isCanMove = true;
} else {
isCanMove = false;
}
mWidth = mViewWidth;
} else {
//再确认一遍
if (mRightBound > mScreenWidth) {
isCanMove = true;
}
mWidth = mScreenWidth;
}
mWidth = mScreenWidth;
}
if (!isTabAutoScroll()){
if (!isTabAutoScroll()) {
isCanMove = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public TabFlowLayout(Context context, @Nullable AttributeSet attrs, int defStyle
mScroller = new Scroller(getContext());
mTabOrientation = mTypeArray.getInteger(R.styleable.TabFlowLayout_tab_orientation,FlowConstants.HORIZONTATAL);
isAutoScroll = mTypeArray.getBoolean(R.styleable.TabFlowLayout_tab_isAutoScroll,true);
int visualCount = mTypeArray.getInteger(R.styleable.TabFlowLayout_tab_visual_count, -1);
setVisualCount(visualCount);
setTabOrientation(mTabOrientation);
chooseTabTpye(tabStyle);
setLayerType(LAYER_TYPE_SOFTWARE,null);
Expand Down Expand Up @@ -117,11 +119,7 @@ public void onGlobalLayout() {
if (mViewPager == null) {
updateScroll(view, false);
}

}



}
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
Expand Down Expand Up @@ -663,6 +661,9 @@ public TabFlowLayout setTabBean(TabBean bean){
if (isAutoScroll != bean.isAutoScroll){
isAutoScroll = bean.isAutoScroll;
}
if (bean.visualCount != -1){
setVisualCount(bean.visualCount);
}
return this;
}

Expand Down
1 change: 1 addition & 0 deletions tablib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<attr name="tab_item_res" format="reference"/>
<attr name="tab_item_autoScale" format="boolean"/>
<attr name="tab_scale_factor" format="float"/>
<attr name="tab_visual_count" format="integer"/>
<attr name="tab_orientation">
<enum name="vertical" value="1"/>
<enum name="horizontal" value="2"/>
Expand Down

0 comments on commit af84691

Please sign in to comment.