提交 c579bc4b 编写于 作者: C chaychan

1.去除一定要设置ViewPager的问题,修改成可设置或不设置;2.修改点击回调,回调previousPosition和currentPosition;3.添加两种使用方式的demo演示

上级 92d85f89
......@@ -9,6 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
......@@ -16,6 +17,12 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ViewPagerActivity">
</activity>
<activity android:name=".FragmentManagerActivity">
</activity>
</application>
</manifest>
\ No newline at end of file
package com.chaychan.bottombarlayout;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import java.util.ArrayList;
import java.util.List;
public class FragmentManagerActivity extends AppCompatActivity {
private List<TabFragment> mFragmentList = new ArrayList<>();
private FrameLayout mFlContent;
private BottomBarLayout mBottomBarLayout;
private RotateAnimation mRotateAnimation;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_manager);
initView();
initData();
initListener();
}
private void initView() {
mFlContent = (FrameLayout) findViewById(R.id.fl_content);
mBottomBarLayout = (BottomBarLayout) findViewById(R.id.bbl);
}
private void initData() {
TabFragment homeFragment = new TabFragment();
Bundle bundle1 = new Bundle();
bundle1.putString(TabFragment.CONTENT, "首页");
homeFragment.setArguments(bundle1);
mFragmentList.add(homeFragment);
TabFragment videoFragment = new TabFragment();
Bundle bundle2 = new Bundle();
bundle2.putString(TabFragment.CONTENT, "视频");
videoFragment.setArguments(bundle2);
mFragmentList.add(videoFragment);
TabFragment microFragment = new TabFragment();
Bundle bundle3 = new Bundle();
bundle3.putString(TabFragment.CONTENT, "微头条");
microFragment.setArguments(bundle3);
mFragmentList.add(microFragment);
TabFragment meFragment = new TabFragment();
Bundle bundle4 = new Bundle();
bundle4.putString(TabFragment.CONTENT, "我的");
meFragment.setArguments(bundle4);
mFragmentList.add(meFragment);
changeFragment(0); //默认显示第一页
}
private void initListener() {
mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
@Override
public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
Log.i("MainActivity", "position: " + currentPosition);
changeFragment(currentPosition);
if (currentPosition == 0) {
//如果是第一个,即首页
if (previousPosition == currentPosition) {
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);
//播放旋转动画
if (mRotateAnimation == null) {
mRotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateAnimation.setDuration(800);
mRotateAnimation.setRepeatCount(-1);
}
ImageView bottomImageView = bottomBarItem.getImageView();
bottomImageView.setAnimation(mRotateAnimation);
bottomImageView.startAnimation(mRotateAnimation);//播放旋转动画
//模拟数据刷新完毕
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setStatus(tabNotChanged);//刷新图标
cancelTabLoading(bottomBarItem);
}
}, 3000);
return;
}
}
//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
cancelTabLoading(bottomItem);//停止旋转动画
}
});
mBottomBarLayout.setUnread(0, 20);//设置第一个页签的未读数为20
mBottomBarLayout.setUnread(1, 1001);//设置第二个页签的未读数
mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
mBottomBarLayout.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字
}
private void changeFragment(int currentPosition) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fl_content, mFragmentList.get(currentPosition));
transaction.commit();
}
/**
* 停止首页页签的旋转动画
*/
private void cancelTabLoading(BottomBarItem bottomItem) {
Animation animation = bottomItem.getImageView().getAnimation();
if (animation != null) {
animation.cancel();
}
}
}
package com.chaychan.bottombarlayout;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
private ViewPager mVpContent;
private BottomBarLayout mBottomBarLayout;
private List<TabFragment> mFragmentList = new ArrayList<>();
private RotateAnimation mRotateAnimation;
private Handler mHandler = new Handler();
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initListener();
}
private void initView() {
mVpContent = (ViewPager) findViewById(R.id.vp_content);
mBottomBarLayout = (BottomBarLayout) findViewById(R.id.bbl);
}
private void initData() {
TabFragment homeFragment = new TabFragment();
Bundle bundle1 = new Bundle();
bundle1.putString(TabFragment.CONTENT,"首页");
homeFragment.setArguments(bundle1);
mFragmentList.add(homeFragment);
TabFragment videoFragment = new TabFragment();
Bundle bundle2 = new Bundle();
bundle2.putString(TabFragment.CONTENT,"视频");
videoFragment.setArguments(bundle2);
mFragmentList.add(videoFragment);
TabFragment microFragment = new TabFragment();
Bundle bundle3 = new Bundle();
bundle3.putString(TabFragment.CONTENT,"微头条");
microFragment.setArguments(bundle3);
mFragmentList.add(microFragment);
TabFragment meFragment = new TabFragment();
Bundle bundle4 = new Bundle();
bundle4.putString(TabFragment.CONTENT,"我的");
meFragment.setArguments(bundle4);
mFragmentList.add(meFragment);
public void useVp(View view){
startActivity(new Intent(this,ViewPagerActivity.class));
}
private void initListener() {
mVpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
mBottomBarLayout.setViewPager(mVpContent);
mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
@Override
public void onItemSelected(final BottomBarItem bottomBarItem, int position) {
Log.i("MainActivity","position: " + position);
if (position == 0){
//如果是第一个,即首页
if (mBottomBarLayout.getCurrentItem() == position){
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);
//播放旋转动画
if (mRotateAnimation == null) {
mRotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateAnimation.setDuration(800);
mRotateAnimation.setRepeatCount(-1);
}
ImageView bottomImageView = bottomBarItem.getImageView();
bottomImageView.setAnimation(mRotateAnimation);
bottomImageView.startAnimation(mRotateAnimation);//播放旋转动画
//模拟数据刷新完毕
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换成首页原来图标
bottomBarItem.setStatus(true);//刷新图标
cancelTabLoading(bottomBarItem);
}
},3000);
return;
}
}
//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
cancelTabLoading(bottomItem);//停止旋转动画
}
});
mBottomBarLayout.setUnread(0,20);//设置第一个页签的未读数为20
mBottomBarLayout.setUnread(1,1001);//设置第二个页签的未读数
mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
mBottomBarLayout.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字
}
/**停止首页页签的旋转动画*/
private void cancelTabLoading(BottomBarItem bottomItem) {
Animation animation = bottomItem.getImageView().getAnimation();
if (animation != null){
animation.cancel();
}
}
class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void noUseVp(View view){
startActivity(new Intent(this,FragmentManagerActivity.class));
}
}
package com.chaychan.bottombarlayout;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerActivity extends FragmentActivity {
private ViewPager mVpContent;
private BottomBarLayout mBottomBarLayout;
private List<TabFragment> mFragmentList = new ArrayList<>();
private RotateAnimation mRotateAnimation;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
initView();
initData();
initListener();
}
private void initView() {
mVpContent = (ViewPager) findViewById(R.id.vp_content);
mBottomBarLayout = (BottomBarLayout) findViewById(R.id.bbl);
}
private void initData() {
TabFragment homeFragment = new TabFragment();
Bundle bundle1 = new Bundle();
bundle1.putString(TabFragment.CONTENT,"首页");
homeFragment.setArguments(bundle1);
mFragmentList.add(homeFragment);
TabFragment videoFragment = new TabFragment();
Bundle bundle2 = new Bundle();
bundle2.putString(TabFragment.CONTENT,"视频");
videoFragment.setArguments(bundle2);
mFragmentList.add(videoFragment);
TabFragment microFragment = new TabFragment();
Bundle bundle3 = new Bundle();
bundle3.putString(TabFragment.CONTENT,"微头条");
microFragment.setArguments(bundle3);
mFragmentList.add(microFragment);
TabFragment meFragment = new TabFragment();
Bundle bundle4 = new Bundle();
bundle4.putString(TabFragment.CONTENT,"我的");
meFragment.setArguments(bundle4);
mFragmentList.add(meFragment);
}
private void initListener() {
mVpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
mBottomBarLayout.setViewPager(mVpContent);
mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
@Override
public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
Log.i("MainActivity","position: " + currentPosition);
if (currentPosition == 0){
//如果是第一个,即首页
if (previousPosition == currentPosition){
//如果是在原来位置上点击,更换首页图标并播放旋转动画
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_loading);//更换成加载图标
bottomBarItem.setStatus(true);
//播放旋转动画
if (mRotateAnimation == null) {
mRotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateAnimation.setDuration(800);
mRotateAnimation.setRepeatCount(-1);
}
ImageView bottomImageView = bottomBarItem.getImageView();
bottomImageView.setAnimation(mRotateAnimation);
bottomImageView.startAnimation(mRotateAnimation);//播放旋转动画
//模拟数据刷新完毕
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
bottomBarItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换成首页原来选中图标
bottomBarItem.setStatus(tabNotChanged);//刷新图标
cancelTabLoading(bottomBarItem);
}
},3000);
return;
}
}
//如果点击了其他条目
BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
bottomItem.setIconSelectedResourceId(R.mipmap.tab_home_selected);//更换为原来的图标
cancelTabLoading(bottomItem);//停止旋转动画
}
});
mBottomBarLayout.setUnread(0,20);//设置第一个页签的未读数为20
mBottomBarLayout.setUnread(1,1001);//设置第二个页签的未读数
mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
mBottomBarLayout.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字
}
/**停止首页页签的旋转动画*/
private void cancelTabLoading(BottomBarItem bottomItem) {
Animation animation = bottomItem.getImageView().getAnimation();
if (animation != null){
animation.cancel();
}
}
class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
}
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.chaychan.library.BottomBarLayout
android:id="@+id/bbl"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/tab_gb"
>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_home_normal"
app:iconSelected="@mipmap/tab_home_selected"
app:itemText="首页"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_video_normal"
app:iconSelected="@mipmap/tab_video_selected"
app:itemText="视频"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:unreadThreshold="999"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_micro_normal"
app:iconSelected="@mipmap/tab_micro_selected"
app:itemText="微头条"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_me_normal"
app:iconSelected="@mipmap/tab_me_selected"
app:itemText="我的"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
</com.chaychan.library.BottomBarLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
<com.chaychan.library.BottomBarLayout
android:id="@+id/bbl"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/tab_gb"
>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_home_normal"
app:iconSelected="@mipmap/tab_home_selected"
app:itemText="首页"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_video_normal"
app:iconSelected="@mipmap/tab_video_selected"
app:itemText="视频"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:unreadThreshold="999"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_micro_normal"
app:iconSelected="@mipmap/tab_micro_selected"
app:itemText="微头条"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_me_normal"
app:iconSelected="@mipmap/tab_me_selected"
app:itemText="我的"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
</com.chaychan.library.BottomBarLayout>
</LinearLayout>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用ViewPager"
android:textAllCaps="false"
android:layout_centerInParent="true"
android:onClick="useVp"
/>
<Button
android:layout_below="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不使用ViewPager"
android:textAllCaps="false"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:onClick="noUseVp"
/>
</RelativeLayout>
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
<com.chaychan.library.BottomBarLayout
android:id="@+id/bbl"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/tab_gb"
>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_home_normal"
app:iconSelected="@mipmap/tab_home_selected"
app:itemText="首页"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_video_normal"
app:iconSelected="@mipmap/tab_video_selected"
app:itemText="视频"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:unreadThreshold="999"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_micro_normal"
app:iconSelected="@mipmap/tab_micro_selected"
app:itemText="微头条"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
<com.chaychan.library.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_me_normal"
app:iconSelected="@mipmap/tab_me_selected"
app:itemText="我的"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="8sp"
app:itemMarginTop="-5dp"
app:openTouchBg="true"
app:touchDrawable="@drawable/selector_bg"
/>
</com.chaychan.library.BottomBarLayout>
</LinearLayout>
......@@ -42,7 +42,12 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarLayout);
mSmoothScroll = ta.getBoolean(R.styleable.BottomBarLayout_smoothScroll,false);
ta.recycle();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
init();
}
@Override
......@@ -56,14 +61,14 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
}
private void init() {
if (mViewPager == null) {
throw new IllegalArgumentException("参数不能为空");
}
mChildCount = getChildCount();
if (mViewPager.getAdapter().getCount() != mChildCount) {
throw new IllegalArgumentException("LinearLayout的子View数量必须和ViewPager条目数量一致");
if (mViewPager != null) {
if (mViewPager.getAdapter().getCount() != mChildCount) {
throw new IllegalArgumentException("LinearLayout的子View数量必须和ViewPager条目数量一致");
}
}
for (int i = 0; i < mChildCount; i++) {
if (getChildAt(i) instanceof BottomBarItem) {
BottomBarItem bottomBarItem = (BottomBarItem) getChildAt(i);
......@@ -76,7 +81,10 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
}
mItemViews.get(mCurrentItem).setStatus(true);//设置选中项
mViewPager.setOnPageChangeListener(this);
if (mViewPager != null){
mViewPager.setOnPageChangeListener(this);
}
}
@Override
......@@ -89,7 +97,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
resetState();
mItemViews.get(position).setStatus(true);
if (onItemSelectedListener != null){
onItemSelectedListener.onItemSelected(getBottomItem(position),position);
onItemSelectedListener.onItemSelected(getBottomItem(position),mCurrentItem,position);
}
mCurrentItem = position;//记录当前位置
}
......@@ -110,14 +118,33 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
@Override
public void onClick(View v) {
//回调点击的位置
if (onItemSelectedListener != null && currentIndex == mCurrentItem) {
onItemSelectedListener.onItemSelected(getBottomItem(currentIndex),currentIndex);
if(mViewPager != null){
//有设置viewPager
if (currentIndex == mCurrentItem){
//如果还是同个页签,使用setCurrentItem不会回调OnPageSelecte(),所以在此处需要回调点击监听
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(getBottomItem(currentIndex),mCurrentItem,currentIndex);
}
}else{
mViewPager.setCurrentItem(currentIndex, mSmoothScroll);
}
}else{
//没有设置viewPager
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(getBottomItem(currentIndex),mCurrentItem,currentIndex);
}
updateTabState(currentIndex);
}
mViewPager.setCurrentItem(currentIndex, mSmoothScroll);
}
}
private void updateTabState(int position){
resetState();
mCurrentItem = position;
mItemViews.get(mCurrentItem).setStatus(true);
}
/**
* 重置当前按钮的状态
*/
......@@ -128,7 +155,11 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
}
public void setCurrentItem(int currentItem) {
mViewPager.setCurrentItem(currentItem,mSmoothScroll);
if (mViewPager != null){
mViewPager.setCurrentItem(currentItem,mSmoothScroll);
}else{
updateTabState(currentItem);
}
}
/**
......@@ -217,7 +248,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
private OnItemSelectedListener onItemSelectedListener;
public interface OnItemSelectedListener {
void onItemSelected(BottomBarItem bottomBarItem, int position);
void onItemSelected(BottomBarItem bottomBarItem, int previousPosition,int currentPosition);
}
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册