自定义navgateBar

效果选中边缘item 自动滑动露出下一个item

原理:

scrollview 嵌套LinearLayout

scrollView.smoothScrollBy(scrollBy,0)

判断view位置 调用

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pic_filter_tags_layout"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:scrollbars="none"
    android:background="#f4f4f4">

    <com.elong.globalhotel.widget.HotelPicTagFilterHListView
        android:id="@+id/pic_filter_tags_hlv"
        android:clipToPadding="false"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@color/transparent" />
</HorizontalScrollView>
public class HotelPicTagFilterHListView extends LinearLayout {

    private BaseAdapter adapter;
    private OnItemClickListener onItemClickListener;

    public HotelPicTagFilterHListView(Context context) {
        super(context);
    }

    public HotelPicTagFilterHListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAdapter(BaseAdapter adapter) {
        this.adapter = adapter;
        bindView();
    }

    public BaseAdapter getAdapter() {
        return adapter;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

    }

    /**
     * 绑定 adapter 中所有的 view
     */
    private void bindView() {
        if (adapter == null) {
            return;
        }

        for (int i = 0; i < adapter.getCount(); i++) {
            final View v = adapter.getView(i, null, this);
            final int tmp = i;
            final Object obj = adapter.getItem(i);

            // view 点击事件触发时回调我们自己的接口
            v.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClicked(v, obj, tmp);
                    }
                }
            });

            addView(v);
        }
    }

    public void updateView() {
        if (adapter == null) {
            return;
        }

        if (this.getChildCount() != adapter.getCount()) {
            return;
        }

        for (int i = 0; i < getChildCount(); i++) {
            adapter.getView(i, this.getChildAt(i), this);
        }
    }


    /**
     * 回调接口
     */
    public interface OnItemClickListener {
        /**
         * @param v        点击的 view
         * @param obj      点击的 view 所绑定的对象
         * @param position 点击位置的 index
         */
        public void onItemClicked(View v, Object obj, int position);
    }

    public void setSelect(int position) {
        //无adapter
        if (adapter == null) {
            return;
        }
        //不是特定adapter
        if (!(adapter instanceof IHotelPicTagFilterHAdapter)) {
            return;
        }

        IHotelPicTagFilterHAdapter iHotelPicTagFilterHAdapter = (IHotelPicTagFilterHAdapter) adapter;
        //再次点选
        if (iHotelPicTagFilterHAdapter.getSelect() == position) {
            return;
        }

        iHotelPicTagFilterHAdapter.setSelect(position);
        updateView();

        //开始执行滑动距离

        if (!(getParent() instanceof HorizontalScrollView)) {
            return;
        }

        HorizontalScrollView scrollView = (HorizontalScrollView) getParent();

        int length = scrollView.getWidth() - scrollView.getPaddingLeft() - scrollView.getPaddingRight();

        if (length <= 0) {
            return;
        }

        int scrollX = scrollView.getScrollX();
        int scrollBy = 0;

        if (position <= 1) {
            scrollBy = -scrollX;
        } else if (position >= adapter.getCount() - 2) {
            View endView = getChildAt(adapter.getCount() - 1);
            scrollBy = endView.getRight() - length - scrollX;
        } else {

            //计算当前view的位置
            View curView = getChildAt(position);
            Point curP = new Point((int) (curView.getLeft() - scrollX), (int) (curView.getRight() - scrollX));

            View lastView = getChildAt(position-1);
            Point lastP = new Point((int) (lastView.getLeft() - scrollX), (int) (lastView.getRight() - scrollX));

            View nextView = getChildAt(position+1);
            Point nextP = new Point((int) (nextView.getLeft() - scrollX), (int) (nextView.getRight() - scrollX));

            if(lastP.x>=0 && nextP.y-length<=0){
                //不操作
            }else if(lastP.x<0){
                //左边view
                scrollBy = lastP.x;
            }else if(nextP.y-length>0){
                scrollBy = nextP.y-length;
            }
        }


        if(scrollBy!=0){
            //滑动
            scrollView.smoothScrollBy(scrollBy,0);
        }


        return;
    }


    public interface IHotelPicTagFilterHAdapter {

        void setSelect(int position);

        int getSelect();
    }
}

 

posted on 2017-06-27 19:11  wjw334  阅读(158)  评论(0编辑  收藏  举报

导航