Skip to content

Commit

Permalink
new added tablayout
Browse files Browse the repository at this point in the history
  • Loading branch information
H07000223 committed Dec 8, 2015
1 parent 4cb128c commit b577c3b
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.roundview.RoundTextView;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.utils.FragmentChangeManager;
import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.nineoldandroids.animation.TypeEvaluator;
import com.nineoldandroids.animation.ValueAnimator;

Expand Down Expand Up @@ -72,6 +77,8 @@ public class SegmentTabLayout extends FrameLayout implements ValueAnimator.Anima
private int barStrokeColor;
private float barStrokeWidth;

private int h;

/** anim */
private ValueAnimator valueAnimator;
private OvershootInterpolator interpolator = new OvershootInterpolator(0.8f);
Expand Down Expand Up @@ -99,6 +106,19 @@ public SegmentTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {

obtainAttributes(context, attrs);

//get layout_height
String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");

//create ViewPager
if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
} else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
} else {
int[] systemAttrs = {android.R.attr.layout_height};
TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
h = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
a.recycle();
}

valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), lp, cp);
valueAnimator.addUpdateListener(this);
}
Expand Down Expand Up @@ -160,7 +180,7 @@ public void notifyDataSetChanged() {
this.tabCount = titles.length;
View tabView;
for (int i = 0; i < tabCount; i++) {
tabView = View.inflate(context, R.layout.layout_tab, null);
tabView = View.inflate(context, R.layout.layout_tab_segment, null);
tabView.setTag(i);
addTab(i, tabView);
}
Expand Down Expand Up @@ -573,6 +593,95 @@ public boolean isTextAllCaps() {
}

//setter and getter
// show MsgTipView
private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private SparseArray<Boolean> initSetMap = new SparseArray<>();

/**
* 显示未读消息
*
* @param position 显示tab位置
* @param num num小于等于0显示红点,num大于0显示数字
*/
public void showMsg(int position, int num) {
if (position >= tabCount) {
position = tabCount - 1;
}

View tabView = tabsContainer.getChildAt(position);
RoundTextView tipView = (RoundTextView) tabView.findViewById(R.id.rtv_msg_tip);
if (tipView != null) {
UnreadMsgUtils.show(tipView, num);

if (initSetMap.get(position) != null && initSetMap.get(position)) {
return;
}

setMsgMargin(position, 2, 2);

initSetMap.put(position, true);
}
}

/**
* 显示未读红点
*
* @param position 显示tab位置
*/
public void showDot(int position) {
if (position >= tabCount) {
position = tabCount - 1;
}
showMsg(position, 0);
}

public void hideMsg(int position) {
if (position >= tabCount) {
position = tabCount - 1;
}

View tabView = tabsContainer.getChildAt(position);
RoundTextView tipView = (RoundTextView) tabView.findViewById(R.id.rtv_msg_tip);
if (tipView != null) {
tipView.setVisibility(View.GONE);
}
}

/**
* 设置提示红点偏移,注意
* 1.控件为固定高度:参照点为tab内容的右上角
* 2.控件高度不固定(WRAP_CONTENT):参照点为tab内容的右上角,此时高度已是红点的最高显示范围,所以这时bottomPadding其实就是topPadding
*/
public void setMsgMargin(int position, float leftPadding, float bottomPadding) {
if (position >= tabCount) {
position = tabCount - 1;
}
View tabView = tabsContainer.getChildAt(position);
RoundTextView tipView = (RoundTextView) tabView.findViewById(R.id.rtv_msg_tip);
if (tipView != null) {
TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
textPaint.setTextSize(textsize);
float textWidth = textPaint.measureText(tv_tab_title.getText().toString());
float textHeight = textPaint.descent() - textPaint.ascent();
MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams();

lp.leftMargin = dp2px(leftPadding);
lp.topMargin = h > 0 ? (int) (h - textHeight) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding);

tipView.setLayoutParams(lp);
}
}

/** 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取RoundTextView对象从而各种设置 */
public RoundTextView getMsgView(int position) {
if (position >= tabCount) {
position = tabCount - 1;
}
View tabView = tabsContainer.getChildAt(position);
RoundTextView tipView = (RoundTextView) tabView.findViewById(R.id.rtv_msg_tip);
return tipView;
}

private OnTabSelectListener listener;

public void setOnTabSelectListener(OnTabSelectListener listener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.flyco.tablayout.utils;


import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;
Expand Down Expand Up @@ -48,4 +47,14 @@ public static void show(RoundTextView rtv, int num) {
rtv.setLayoutParams(lp);
}
}

public static void setSize(RoundTextView rtv, int size) {
if (rtv == null) {
return;
}
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rtv.getLayoutParams();
lp.width = size;
lp.height = size;
rtv.setLayoutParams(lp);
}
}
39 changes: 39 additions & 0 deletions FlycoTabLayout_Lib/src/main/res/layout/layout_tab_segment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">

<LinearLayout
android:id="@+id/ll_tap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>

<com.flyco.roundview.RoundTextView
android:layout_toRightOf="@+id/ll_tap"
android:id="@+id/rtv_msg_tip"
xmlns:rv="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="11.5sp"
android:visibility="gone"
rv:rv_backgroundColor="#FD481F"
rv:rv_isRadiusHalfHeight="true"
rv:rv_strokeColor="#ffffff"
rv:rv_strokeWidth="1dp"/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void onTabReselect(int position) {
tl_2.showDot(2);
RoundTextView rtv_2_2 = tl_2.getMsgView(2);
if (rtv_2_2 != null) {
rtv_2_2.setWidth(dp2px(7.5f));
UnreadMsgUtils.setSize(rtv_2_2, dp2px(7.5f));
}

//设置未读消息背景
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flyco.tablayoutsamples.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Expand All @@ -9,9 +10,11 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.flyco.roundview.RoundTextView;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayoutsamples.R;
import com.flyco.tablayoutsamples.entity.TabEntity;
import com.flyco.tablayoutsamples.utils.ViewFindUtils;
Expand Down Expand Up @@ -72,61 +75,27 @@ protected void onCreate(Bundle savedInstanceState) {
tl_3();
tl_4.setTabData(titles_2, this, R.id.fl_change, fragments2);
tl_5.setTabData(titles_3);
// /** indicator固定宽度 */
// tl_5 = ViewFindUtils.find(decorView, R.id.tl_5);
// /** indicator矩形圆角 */
// tl_6 = ViewFindUtils.find(decorView, R.id.tl_6);
// /** indicator三角形 */
// tl_7 = ViewFindUtils.find(decorView, R.id.tl_7);
// /** indicator圆角色块 */
// tl_8 = ViewFindUtils.find(decorView, R.id.tl_8);


// tl_3.setTabData(tabs, this, R.id.fl_change, fragments2);
// tl_4.setTabData(tabs);
// tl_5.setTabData(tabs);
// tl_6.setTabData(tabs);
// tl_7.setTabData(tabs);
// tl_8.setTabData(tabs);
//
// tl_3.setOnTabSelectListener(new OnTabSelectListener() {
// @Override
// public void onTabSelect(int position) {
// tl_1.setCurrentTab(position);
// tl_3.setCurrentTab(position);
// tl_4.setCurrentTab(position);
// tl_5.setCurrentTab(position);
// tl_6.setCurrentTab(position);
// tl_7.setCurrentTab(position);
// tl_8.setCurrentTab(position);
// }
//
// @Override
// public void onTabReselect(int position) {
//
// }
// });
//
// //显示未读红点
// tl_1.showDot(2);
// tl_3.showDot(1);
// tl_4.showDot(1);
//

//显示未读红点
tl_1.showDot(2);
tl_3.showDot(1);
tl_4.showDot(1);

// //两位数
// tl_3.showMsg(0, 55);
// tl_3.setMsgMargin(0, -5, 5);
//
// //三位数
// tl_3.showMsg(1, 100);
// tl_3.setMsgMargin(1, -5, 5);
//
// //设置未读消息红点
// tl_3.showDot(2);
// RoundTextView rtv_2_2 = tl_3.getMsgView(2);
// if (rtv_2_2 != null) {
// rtv_2_2.setWidth(dp2px(7.5f));
// }
//

//设置未读消息红点
tl_3.showDot(2);
RoundTextView rtv_3_2 = tl_3.getMsgView(2);
if (rtv_3_2 != null) {
rtv_3_2.getDelegate().setBackgroundColor(Color.parseColor("#6D8FB0"));
}

// //设置未读消息背景
// tl_3.showMsg(3, 5);
// tl_3.setMsgMargin(3, 0, 5);
Expand Down

0 comments on commit b577c3b

Please sign in to comment.