自定义HorizontalScrollView实现水平滚动导航条

核心代码:

package com.liuyk.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import java.util.ArrayList;

/**
 * 水平滚动栏
 *
 * @author liuyk
 */
public abstract class HorizontalNavigationBar<T> extends HorizontalScrollView {
    //分割线颜色
    protected int mSplitColor = Color.RED;
    private int mCurrentPosition = -1;
    private LinearLayout mItemViewContainer;
    private OnHorizontalNavigationSelectListener mOnHorizontalNavigationSelectListener;
    /**
     * 是否有下划线
     */
    private boolean isChannelSplit;

    private ArrayList<T> mItems;

    public HorizontalNavigationBar(Context paramContext) {
        this(paramContext, null);
    }

    public HorizontalNavigationBar(Context paramContext, AttributeSet paramAttributeSet) {
        this(paramContext, paramAttributeSet, 0);
    }

    public HorizontalNavigationBar(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.horizontal_navigation_container, this);
        mItemViewContainer = (LinearLayout) view.findViewById(R.id.horizontal_navigation_container);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setItems(ArrayList<T> items) {
        if (items == null) return;

        this.mItems = items;
        this.mItemViewContainer.removeAllViews();
        for (int i = 0; i < items.size(); i++) {
            final HorizontalNavigationItemView itemView = new HorizontalNavigationItemView(getContext());
            itemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
            itemView.setChannelSplit(isChannelSplit);
            itemView.setSplitColor(mSplitColor);
            renderingItemView(itemView, i, mCurrentPosition);

            final int index = i;
            itemView.setOnClickListener(new OnClickListener() {
                public void onClick(View paramAnonymousView) {
                    HorizontalNavigationBar.this.setCurrentChannelItem(index);
                    if (mOnHorizontalNavigationSelectListener != null) {
                        mOnHorizontalNavigationSelectListener.select(index);
                    }
                }
            });
            this.mItemViewContainer.addView(itemView);
        }
        scrollTo(0, 0);
    }

    public void setCurrentChannelItem(int index) {
        int childCount = this.mItemViewContainer.getChildCount();
        if (index > childCount - 1) {
            throw new RuntimeException("position more size");
        }
        if (index == this.mCurrentPosition) {
            return;
        }
        this.mCurrentPosition = index;
        for (int i = 0; i < childCount; i++) {
            HorizontalNavigationItemView itemView = (HorizontalNavigationItemView) this.mItemViewContainer.getChildAt(i);
            itemView.setChecked(i == mCurrentPosition);
        }
        if (mCurrentPosition == 0) {
            scrollTo(0, 0);
        } else {
            int left = this.mItemViewContainer.getChildAt(index).getLeft();
            smoothScrollTo(left, 0);
        }
    }

    public boolean isChannelSplit() {
        return isChannelSplit;
    }

    //分割线颜色
    public void setSplitColor(int resId) {
        mSplitColor = resId;
    }

    //是否有下划线
    public void setChannelSplit(boolean channelSplit) {
        isChannelSplit = channelSplit;
    }

    public void addOnHorizontalNavigationSelectListener(OnHorizontalNavigationSelectListener onHorizontalNavigationSelectListener) {
        this.mOnHorizontalNavigationSelectListener = onHorizontalNavigationSelectListener;
    }

    public T getItem(int position) {
        return mItems == null ? null : mItems.get(position);
    }

    /**
     * 渲染子ItemView
     *
     * @param itemView        子View
     * @param index           子View的索引位置
     * @param currentPosition 当前选中位置
     */
    public abstract void renderingItemView(HorizontalNavigationItemView itemView, int index, int currentPosition);

    public interface OnHorizontalNavigationSelectListener {
        void select(int index);
    }
}

源码地址:

https://github.com/liuyak/HorizontalNavigationBar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值