提交 022c7991 编写于 作者: C chaychan

添加bottomBarLayout

上级 eb9901a5
......@@ -28,9 +28,11 @@
</activity>
<activity android:name=".activity.EllDefaultBottomDemoActivity">
</activity>
<activity android:name=".activity.EllCustomBottomDemoActivity"></activity>
<activity android:name=".activity.EllCustomBottomDemoActivity"/>
<activity android:name=".activity.ExpandableLinearLayoutChooseActivity">
</activity>
<activity android:name=".activity.BottomBarLayoutDemoActivity">
</activity>
</application>
</manifest>
\ No newline at end of file
package com.chaychan.powerfulviewlibrary.activity;
import android.os.Bundle;
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 com.chaychan.powerfulviewlibrary.R;
import com.chaychan.powerfulviewlibrary.fragment.TabFragment;
import com.chaychan.viewlib.bottombarlayout.BottomBarLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class BottomBarLayoutDemoActivity extends FragmentActivity {
@Bind(R.id.vp_content)
ViewPager vpContent;
@Bind(R.id.bbl)
BottomBarLayout bbl;
private List<TabFragment> mFragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_bar_layout_demo);
ButterKnife.bind(this);
initData();
initListener();
}
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() {
vpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
bbl.setmViewPager(vpContent);
}
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();
}
}
}
......@@ -16,7 +16,8 @@ public class MainActivity extends ListActivity {
new DemoBean("ExpandableTextViewDemo",ExpandableTextViewDemoActivity.class),
new DemoBean("PieChartViewDemo",PieChartViewDemoActivity.class),
new DemoBean("RunningTextViewDemo",RunningTextViewDemoActivity.class),
new DemoBean("ExpandableLinearLayoutDemo",ExpandableLinearLayoutChooseActivity.class)
new DemoBean("ExpandableLinearLayoutDemo",ExpandableLinearLayoutChooseActivity.class),
new DemoBean("BottomBarLayoutDemoActivity",BottomBarLayoutDemoActivity.class)
};
@Override
......
package com.chaychan.powerfulviewlibrary.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* @author ChayChan
* @date 2017/6/23 11:22
*/
public class TabFragment extends Fragment {
public static final String CONTENT = "content";
private TextView mTextView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle bundle) {
mTextView = new TextView(getActivity());
mTextView.setGravity(Gravity.CENTER);
String content = getArguments().getString(CONTENT);
mTextView.setText(content);
return mTextView;
}
}
<?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.viewlib.bottombarlayout.BottomBarLayout
android:id="@+id/bbl"
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/tab_gb"
>
<com.chaychan.viewlib.bottombarlayout.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="10sp"
app:itemMarginTop="0dp"
/>
<com.chaychan.viewlib.bottombarlayout.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="10sp"
app:itemMarginTop="0dp"
/>
<com.chaychan.viewlib.bottombarlayout.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="10sp"
app:itemMarginTop="0dp"
/>
<com.chaychan.viewlib.bottombarlayout.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="10sp"
app:itemMarginTop="0dp"
/>
</com.chaychan.viewlib.bottombarlayout.BottomBarLayout>
</LinearLayout>
......@@ -3,4 +3,9 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="tab_gb">#F3F5F4</color>
<color name="tab_normal_color">#515051</color>
<color name="tab_selected_color">#D33D3C</color>
</resources>
package com.chaychan.viewlib.bottombarlayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.chaychan.viewlib.R;
import com.chaychan.viewlib.utils.UIUtils;
/**
* @author ChayChan
* @description: 底部tab条目
* @date 2017/6/23 9:14
*/
public class BottomBarItem extends LinearLayout {
private Context mContext;
private int mIconNormalResourceId;//普通状态图标的资源id
private int mIconSelectedResourceId;//选中状态图标的资源id
private String mText;//文本
private int mTextSize = 12;//文字大小 默认为12sp
private int mTextColorNormal = 0xFF999999; //描述文本的默认显示颜色
private int mTextColorSelected = 0xFF46C01B; //述文本的默认选中显示颜色
private int mMarginTop = 5;//文字和图标的距离,默认5dp
private TextView mTextView;
private ImageView mImageView;
public BottomBarItem(Context context) {
this(context, null);
}
public BottomBarItem(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarItem);
mIconNormalResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconNormal, -1);
mIconSelectedResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconSelected, -1);
mText = ta.getString(R.styleable.BottomBarItem_itemText);
mTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(mContext,mTextSize));
mTextColorNormal = ta.getColor(R.styleable.BottomBarItem_textColorNormal, mTextColorNormal);
mTextColorSelected = ta.getColor(R.styleable.BottomBarItem_textColorSelected, mTextColorSelected);
mMarginTop = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemMarginTop, UIUtils.dip2Px(mContext, mMarginTop));
ta.recycle();
checkValues();
init();
}
/**
* 检查传入的值是否完善
*/
private void checkValues() {
if (mIconNormalResourceId == -1) {
throw new IllegalStateException("您还没有设置默认状态下的图标,请指定iconNormal的图标");
}
if (mIconSelectedResourceId == -1) {
throw new IllegalStateException("您还没有设置选中状态下的图标,请指定iconSelected的图标");
}
}
private void init() {
setOrientation(VERTICAL);
setGravity(Gravity.CENTER);
View view = View.inflate(mContext, R.layout.item_bottom_bar, null);
mImageView = (ImageView) view.findViewById(R.id.iv_icon);
mTextView = (TextView) view.findViewById(R.id.tv_text);
mImageView.setImageResource(mIconNormalResourceId);
mTextView.getPaint().setTextSize(mTextSize);
mTextView.setText(mText);
mTextView.setTextColor(mTextColorNormal);
LinearLayout.LayoutParams layoutParams = (LayoutParams) mTextView.getLayoutParams();
layoutParams.topMargin = mMarginTop;
mTextView.setLayoutParams(layoutParams);
addView(view);
}
private ImageView createImageView() {
mImageView = new ImageView(mContext);
mImageView.setImageResource(mIconNormalResourceId);
return mImageView;
}
private TextView creatTextView() {
mTextView = new TextView(mContext);
mTextView.setGravity(Gravity.CENTER);
return mTextView;
}
public void setStatus(boolean isSelected){
mImageView.setImageResource(isSelected?mIconSelectedResourceId:mIconNormalResourceId);
mTextView.setTextColor(isSelected?mTextColorSelected:mTextColorNormal);
}
}
package com.chaychan.viewlib.bottombarlayout;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
/**
* @author ChayChan
* @description: 底部页签根节点
* @date 2017/6/23 11:02
*/
public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageChangeListener {
private static final String STATE_INSTANCE = "instance_state";
private static final String STATE_ITEM = "state_item";
private ViewPager mViewPager;
private int mChildCount;//子条目个数
private List<BottomBarItem> itemViews = new ArrayList<>();
private int mCurrentItem;//当前条目的索引
private boolean mSmoothScroll;
public BottomBarLayout(Context context) {
this(context, null);
}
public BottomBarLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BottomBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(HORIZONTAL);
}
@Override
public void setOrientation(int orientation) {
if (LinearLayout.VERTICAL == orientation) {
throw new IllegalArgumentException("BottomBarLayout only supports Horizontal Orientation.");
}
super.setOrientation(orientation);
}
public void setmViewPager(ViewPager mViewPager) {
this.mViewPager = mViewPager;
init();
}
private void init() {
if (mViewPager == null) {
throw new IllegalArgumentException("参数不能为空");
}
mChildCount = getChildCount();
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);
itemViews.add(bottomBarItem);
//设置点击监听
bottomBarItem.setOnClickListener(new MyOnClickListener(i));
} else {
throw new IllegalArgumentException("AlphaIndicator的子View必须是AlphaView");
}
}
itemViews.get(mCurrentItem).setStatus(true);//设置选中项
mViewPager.setOnPageChangeListener(this);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mCurrentItem = position;//记录当前位置
resetState();
itemViews.get(position).setStatus(true);
mViewPager.setCurrentItem(position, mSmoothScroll);
}
@Override
public void onPageScrollStateChanged(int state) {
}
private class MyOnClickListener implements OnClickListener {
private int currentIndex;
public MyOnClickListener(int i) {
this.currentIndex = i;
}
@Override
public void onClick(View v) {
//回调点击的位置
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(currentIndex);
}
//点击前先重置所有按钮的状态
resetState();
itemViews.get(currentIndex).setStatus(true);//设置为选中状态
//不能使用平滑滚动,否者颜色改变会乱
mViewPager.setCurrentItem(currentIndex, false);
//点击是保存当前按钮索引
mCurrentItem = currentIndex;
}
}
/**
* 重置所有按钮的状态
*/
private void resetState() {
for (int i = 0; i < mChildCount; i++) {
itemViews.get(i).setStatus(false);
}
}
public int getCurrentItem() {
return mCurrentItem;
}
public void setSmoothScroll(boolean mSmoothScroll) {
this.mSmoothScroll = mSmoothScroll;
}
/**
* @return 当View被销毁的时候,保存数据
*/
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(STATE_INSTANCE, super.onSaveInstanceState());
bundle.putInt(STATE_ITEM, mCurrentItem);
return bundle;
}
/**
* @param state 用于恢复数据使用
*/
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
mCurrentItem = bundle.getInt(STATE_ITEM);
//重置所有按钮状态
resetState();
//恢复点击的条目颜色
itemViews.get(mCurrentItem).setStatus(true);
super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE));
} else {
super.onRestoreInstanceState(state);
}
}
private OnItemSelectedListener onItemSelectedListener;
public interface OnItemSelectedListener {
void onItemSelected(int position);
}
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
\ No newline at end of file
......@@ -97,4 +97,15 @@
</declare-styleable>
<!--底部TabView-->
<declare-styleable name="BottomBarItem">
<attr name="iconNormal" format="reference"/>
<attr name="iconSelected" format="reference"/>
<attr name="itemText" format="string"/>
<attr name="itemTextSize" format="dimension"/>
<attr name="textColorNormal" format="color"/>
<attr name="textColorSelected" format="color"/>
<attr name="itemMarginTop" format="dimension"/>
</declare-styleable>
</resources>
\ No newline at end of file
Markdown is supported
0% .