效果
TabActivity
package com.coral3.ah.ui.activity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;
import com.coral3.ah.R;
import com.coral3.ah.components.MoveTabBlock;
import com.coral3.ah.ui.fragment.tab.TabFragment1;
import com.coral3.ah.ui.fragment.tab.TabFragment2;
import com.coral3.ah.ui.fragment.tab.TabFragment3;
import com.coral3.common_module.utils.InitUtil;
import com.coral3.common_module.utils.LogUtil;
public class TabActivity extends AppCompatActivity implements View.OnClickListener{
private ViewPager viewPager;
private Fragment[] views;
private LinearLayout llTabLayout;
private MoveTabBlock moveTabBlock;
private int childCount;
private Scroller mScroller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
initView();
initListener();
}
public void initView(){
viewPager = findViewById(R.id.vp_view_tabs);
views = new Fragment[3];
views[0] = new TabFragment1();
views[1] = new TabFragment2();
views[2] = new TabFragment3();
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), 1));
llTabLayout = findViewById(R.id.ll_tabs_layout);
childCount = llTabLayout.getChildCount();
moveTabBlock = findViewById(R.id.tv_move_block);
mScroller = new Scroller(this);
chooseTab(0);
}
public void chooseTab(int pos){
for(int i = 0, len = childCount; i < len; i++){
LinearLayout childAt = (LinearLayout) llTabLayout.getChildAt(i);
((TextView) childAt.getChildAt(1)).setTextColor(InitUtil.getContext().getResources().getColor(R.color.text_no_select));
childAt.getChildAt(0).setEnabled(true);
}
LinearLayout childAt = (LinearLayout) llTabLayout.getChildAt(pos);
((TextView) childAt.getChildAt(1)).setTextColor(InitUtil.getContext().getResources().getColor(R.color.black));
childAt.getChildAt(0).setEnabled(false);
}
public void initListener(){
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
LinearLayout childAt = (LinearLayout) llTabLayout.getChildAt(position);
int childAtWidth = childAt.getWidth();
int moveTabBlockWidth = moveTabBlock.getWidth();
int[] location = new int[2];
childAt.getLocationOnScreen(location);
int x = location[0];
int[] tabXY = new int[2];
moveTabBlock.getLocationOnScreen(tabXY);
int tabX = tabXY[0];
LogUtil.d(String.valueOf(x) + "-" + tabXY[0]);
moveTabBlock.scrollTo(x - tabX + childAtWidth / 2 - moveTabBlockWidth / 2 + 13, 0);
chooseTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
for(int i = 0; i < childCount; i++){
llTabLayout.getChildAt(i).setTag(i);
llTabLayout.getChildAt(i).setOnClickListener(this);
}
}
@Override
public void onClick(View view) {
Log.d("yue-tag", view.getTag().toString());
viewPager.setCurrentItem((int)view.getTag());;
}
private class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(@NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
@NonNull
@Override
public Fragment getItem(int position) {
return views[position];
}
@Override
public int getCount() {
return views.length;
}
}
}
MoveTabBlock
package com.coral3.ah.components;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;
public class MoveTabBlock extends View {
private Boolean isEvent = false;
int lastX = 0;
int lastY = 0;
Scroller scroller;
public MoveTabBlock(Context context, AttributeSet attrs) {
super(context, attrs);
scroller = new Scroller(context);
}
@Override public boolean onTouchEvent(MotionEvent event) {
if(!isEvent) return false;
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int offsetX = x - lastX;
int offsetY = y - lastY;
((View) getParent()).scrollBy(-offsetX, -offsetY);
break;
case MotionEvent.ACTION_UP:
View viewGroup = (View) getParent();
scroller.startScroll(viewGroup.getScrollX(),
viewGroup.getScrollY(),
-viewGroup.getScrollX(),
-viewGroup.getScrollY(),
630);
invalidate();
break;
}
return true;
}
public void scrollTo(int x, int y){
View viewGroup = (View) getParent();
scroller.startScroll(viewGroup.getScrollX(),
viewGroup.getScrollY(),
-x,
-y,
800);
invalidate();
}
public void openEvent(Boolean params){
this.isEvent = params;
}
public int[] getXY(){
int[] location = new int[2];
View viewGroup = (View) getParent();
location[0] = viewGroup.getScrollX();
location[1] = viewGroup.getScrollY();
return location;
}
@Override public void computeScroll() {
if (scroller.computeScrollOffset()) {
((View) getParent()).scrollTo(scroller.getCurrX(), scroller.getCurrY());
invalidate();
}
}
}
activity_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F0F0F0"
tools:context=".ui.activity.TabActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:id="@+id/ll_tabs_layout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/tab_selector"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:layout_height="wrap_content"/>
<TextView
android:text="推荐"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/tab_selector"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:layout_height="wrap_content"/>
<TextView
android:text="热榜"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/tab_selector"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:layout_height="wrap_content"/>
<TextView
android:text="高赞"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:gravity="center_vertical">
<com.coral3.ah.components.MoveTabBlock
android:id="@+id/tv_move_block"
android:background="@drawable/bg_radius_5"
android:layout_marginLeft="50dp"
android:layout_width="30dp"
android:layout_height="3dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_view_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
图片素材
tab_img1 tab_img2 tab_img3 tab_blue tab_red
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="tabbar_no_act">#bfbfbf</color>
<color name="tabbar_act">#ffffff</color>
<color name="devide_line">#F0F0F0</color>
<color name="text_no_select">#515050</color>
</resources>
bg_radius_5.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#03A9F4"/>
</shape>
tab_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/tab_red"/>
<item android:state_enabled="true" android:drawable="@drawable/tab_blue"/>
</selector>
TabFragment1
package com.coral3.ah.ui.fragment.tab;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.coral3.ah.R;
public class TabFragment1 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public TabFragment1() {
}
public static TabFragment1 newInstance(String param1, String param2) {
TabFragment1 fragment = new TabFragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
}
tab_fragment2
package com.coral3.ah.ui.fragment.tab;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.coral3.ah.R;
public class TabFragment2 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public TabFragment2() {
}
public static TabFragment2 newInstance(String param1, String param2) {
TabFragment2 fragment = new TabFragment2();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab2, container, false);
}
}
tab_fragment3
package com.coral3.ah.ui.fragment.tab;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.coral3.ah.R;
public class TabFragment3 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public TabFragment3() {
}
public static TabFragment3 newInstance(String param1, String param2) {
TabFragment3 fragment = new TabFragment3();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab3, container, false);
}
}
fragment_tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.tab.TabFragment1">
<ImageView
android:src="@drawable/tab_img1"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
fragment_tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.tab.TabFragment2">
<ImageView
android:src="@drawable/tab_img2"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
fragment_tab3.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.tab.TabFragment3">
<ImageView
android:src="@drawable/tab_img3"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
*路漫长
|