Search in sources :

Example 1 with CenterSnapHelper

use of com.example.library.banner.layoutmanager.CenterSnapHelper in project RecyclerBanner by renjianan.

the class BannerLayout method initView.

protected void initView(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BannerLayout);
    showIndicator = a.getBoolean(R.styleable.BannerLayout_showIndicator, true);
    autoPlayDuration = a.getInt(R.styleable.BannerLayout_interval, 4000);
    isAutoPlaying = a.getBoolean(R.styleable.BannerLayout_autoPlaying, true);
    itemSpace = a.getInt(R.styleable.BannerLayout_itemSpace, 20);
    centerScale = a.getFloat(R.styleable.BannerLayout_centerScale, 1.2f);
    moveSpeed = a.getFloat(R.styleable.BannerLayout_moveSpeed, 1.0f);
    if (mSelectedDrawable == null) {
        // 绘制默认选中状态图形
        GradientDrawable selectedGradientDrawable = new GradientDrawable();
        selectedGradientDrawable.setShape(GradientDrawable.OVAL);
        selectedGradientDrawable.setColor(Color.RED);
        selectedGradientDrawable.setSize(dp2px(5), dp2px(5));
        selectedGradientDrawable.setCornerRadius(dp2px(5) / 2);
        mSelectedDrawable = new LayerDrawable(new Drawable[] { selectedGradientDrawable });
    }
    if (mUnselectedDrawable == null) {
        // 绘制默认未选中状态图形
        GradientDrawable unSelectedGradientDrawable = new GradientDrawable();
        unSelectedGradientDrawable.setShape(GradientDrawable.OVAL);
        unSelectedGradientDrawable.setColor(Color.GRAY);
        unSelectedGradientDrawable.setSize(dp2px(5), dp2px(5));
        unSelectedGradientDrawable.setCornerRadius(dp2px(5) / 2);
        mUnselectedDrawable = new LayerDrawable(new Drawable[] { unSelectedGradientDrawable });
    }
    indicatorMargin = dp2px(4);
    int marginLeft = dp2px(16);
    int marginRight = dp2px(0);
    int marginBottom = dp2px(11);
    int gravity = GravityCompat.START;
    int o = a.getInt(R.styleable.BannerLayout_orientation, 0);
    int orientation = 0;
    if (o == 0) {
        orientation = OrientationHelper.HORIZONTAL;
    } else if (o == 1) {
        orientation = OrientationHelper.VERTICAL;
    }
    a.recycle();
    // 轮播图部分
    mRecyclerView = new RecyclerView(context);
    LayoutParams vpLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    addView(mRecyclerView, vpLayoutParams);
    mLayoutManager = new BannerLayoutManager(getContext(), orientation);
    mLayoutManager.setItemSpace(itemSpace);
    mLayoutManager.setCenterScale(centerScale);
    mLayoutManager.setMoveSpeed(moveSpeed);
    mRecyclerView.setLayoutManager(mLayoutManager);
    new CenterSnapHelper().attachToRecyclerView(mRecyclerView);
    // 指示器部分
    indicatorContainer = new RecyclerView(context);
    LinearLayoutManager indicatorLayoutManager = new LinearLayoutManager(context, orientation, false);
    indicatorContainer.setLayoutManager(indicatorLayoutManager);
    indicatorAdapter = new IndicatorAdapter();
    indicatorContainer.setAdapter(indicatorAdapter);
    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.BOTTOM | gravity;
    params.setMargins(marginLeft, 0, marginRight, marginBottom);
    addView(indicatorContainer, params);
    if (!showIndicator) {
        indicatorContainer.setVisibility(GONE);
    }
}
Also used : CenterSnapHelper(com.example.library.banner.layoutmanager.CenterSnapHelper) TypedArray(android.content.res.TypedArray) LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) Drawable(android.graphics.drawable.Drawable) GradientDrawable(android.graphics.drawable.GradientDrawable) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) BannerLayoutManager(com.example.library.banner.layoutmanager.BannerLayoutManager) GradientDrawable(android.graphics.drawable.GradientDrawable)

Aggregations

TypedArray (android.content.res.TypedArray)1 Drawable (android.graphics.drawable.Drawable)1 GradientDrawable (android.graphics.drawable.GradientDrawable)1 LayerDrawable (android.graphics.drawable.LayerDrawable)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 BannerLayoutManager (com.example.library.banner.layoutmanager.BannerLayoutManager)1 CenterSnapHelper (com.example.library.banner.layoutmanager.CenterSnapHelper)1