Search in sources :

Example 1 with PagerSnapHelper

use of android.support.v7.widget.PagerSnapHelper in project Shuttle by timusus.

the class QueuePagerFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_queue_pager, container, false);
    unbinder = ButterKnife.bind(this, rootView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    if (ShuttleUtils.isLandscape()) {
        textProtectionScrim.setVisibility(View.GONE);
    }
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(viewModelAdapter);
    SnapHelper snapHelper = new PagerSnapHelper() {

        @Override
        public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
            int snapPosition = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
            if (snapPosition < viewModelAdapter.items.size()) {
                Observable.defer(() -> {
                    if (MusicUtils.getQueuePosition() != snapPosition) {
                        MusicUtils.setQueuePosition(snapPosition);
                    }
                    return Observable.empty();
                }).delaySubscription(150, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).subscribe();
            }
            return snapPosition;
        }
    };
    snapHelper.attachToRecyclerView(recyclerView);
    recyclerView.addOnScrollListener(new RecyclerViewPreloader<>(new ListPreloader.PreloadModelProvider<QueuePagerItemView>() {

        @Override
        public List<QueuePagerItemView> getPreloadItems(int position) {
            QueuePagerItemView queuePagerItemView = (QueuePagerItemView) viewModelAdapter.items.get(position);
            return Collections.singletonList(queuePagerItemView);
        }

        @Override
        public GenericRequestBuilder getPreloadRequestBuilder(QueuePagerItemView item) {
            return requestManager.load(item.song).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(PlaceholderProvider.getInstance().getPlaceHolderDrawable(item.song.name, true));
        }
    }, (item, adapterPosition, perItemPosition) -> imageSize, 3));
    recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            // This null check doesn't make sense to me, but there was an NPE here..
            if (recyclerView != null) {
                imageSize = new int[] { recyclerView.getWidth(), recyclerView.getHeight() };
                recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
            }
            return false;
        }
    });
    return rootView;
}
Also used : R(com.simplecity.amp_library.R) Bundle(android.os.Bundle) SnapHelper(android.support.v7.widget.SnapHelper) ButterKnife(butterknife.ButterKnife) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) BindView(butterknife.BindView) Inject(javax.inject.Inject) GenericRequestBuilder(com.bumptech.glide.GenericRequestBuilder) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) RequestManager(com.bumptech.glide.RequestManager) ListPreloader(com.bumptech.glide.ListPreloader) DiskCacheStrategy(com.bumptech.glide.load.engine.DiskCacheStrategy) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Unbinder(butterknife.Unbinder) PlaceholderProvider(com.simplecity.amp_library.utils.PlaceholderProvider) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) QueuePagerView(com.simplecity.amp_library.ui.views.QueuePagerView) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewGroup(android.view.ViewGroup) RecyclerViewPreloader(com.simplecity.amp_library.glide.preloader.RecyclerViewPreloader) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) ViewTreeObserver(android.view.ViewTreeObserver) QueuePagerPresenter(com.simplecity.amp_library.ui.presenters.QueuePagerPresenter) Collections(java.util.Collections) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) BindView(butterknife.BindView) View(android.view.View) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) QueuePagerView(com.simplecity.amp_library.ui.views.QueuePagerView) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) SnapHelper(android.support.v7.widget.SnapHelper) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) ViewTreeObserver(android.view.ViewTreeObserver)

Example 2 with PagerSnapHelper

use of android.support.v7.widget.PagerSnapHelper in project RecyclerBanner by renjianan.

the class RecyclerViewBannerBase method initView.

protected void initView(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecyclerViewBannerBase);
    showIndicator = a.getBoolean(R.styleable.RecyclerViewBannerBase_showIndicator, true);
    autoPlayDuration = a.getInt(R.styleable.RecyclerViewBannerBase_interval, 4000);
    isAutoPlaying = a.getBoolean(R.styleable.RecyclerViewBannerBase_autoPlaying, true);
    mSelectedDrawable = a.getDrawable(R.styleable.RecyclerViewBannerBase_indicatorSelectedSrc);
    mUnselectedDrawable = a.getDrawable(R.styleable.RecyclerViewBannerBase_indicatorUnselectedSrc);
    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 = a.getDimensionPixelSize(R.styleable.RecyclerViewBannerBase_indicatorSpace, dp2px(4));
    int marginLeft = a.getDimensionPixelSize(R.styleable.RecyclerViewBannerBase_indicatorMarginLeft, dp2px(16));
    int marginRight = a.getDimensionPixelSize(R.styleable.RecyclerViewBannerBase_indicatorMarginRight, dp2px(0));
    int marginBottom = a.getDimensionPixelSize(R.styleable.RecyclerViewBannerBase_indicatorMarginBottom, dp2px(11));
    int g = a.getInt(R.styleable.RecyclerViewBannerBase_indicatorGravity, 0);
    int gravity;
    if (g == 0) {
        gravity = GravityCompat.START;
    } else if (g == 2) {
        gravity = GravityCompat.END;
    } else {
        gravity = Gravity.CENTER;
    }
    int o = a.getInt(R.styleable.RecyclerViewBannerBase_orientation, 0);
    int orientation = 0;
    if (o == 0) {
        orientation = LinearLayoutManager.HORIZONTAL;
    } else if (o == 1) {
        orientation = LinearLayoutManager.VERTICAL;
    }
    a.recycle();
    // recyclerView部分
    mRecyclerView = new RecyclerView(context);
    new PagerSnapHelper().attachToRecyclerView(mRecyclerView);
    mLayoutManager = getLayoutManager(context, orientation);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            onBannerScrolled(recyclerView, dx, dy);
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            onBannerScrollStateChanged(recyclerView, newState);
        }
    });
    LayoutParams vpLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    addView(mRecyclerView, vpLayoutParams);
    // 指示器部分
    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 : 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) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) GradientDrawable(android.graphics.drawable.GradientDrawable)

Example 3 with PagerSnapHelper

use of android.support.v7.widget.PagerSnapHelper in project Andela-Med-Manager by jumaallan.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    User user = new User(Long.valueOf("1"), "Data Binding", "User");
    binding.setUser(user);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ButterKnife.bind(this);
    profileDialog = ProfileDialog.newInstance(((dialog, which) -> logout()));
    calendar = Calendar.getInstance();
    simpleDateFormat = new SimpleDateFormat("EEEE, MMM d, yyyy");
    String currentDate = simpleDateFormat.format(calendar.getTime());
    date.setText(currentDate);
    menuViewList = getMenuOptions();
    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(this, R.dimen.item_offset);
    recyclerView.addItemDecoration(itemDecoration);
    MainDashboardAdapter mainDashboardAdapter = new MainDashboardAdapter(this, menuViewList, (v, position) -> {
        MenuView role = menuViewList.get(position);
        String menuName = role.getName();
        switch(menuName) {
            case "Add Medicine":
                Intent addMedicine = new Intent(getApplicationContext(), AddMedicineActivity.class);
                startActivity(addMedicine);
                break;
            case "Profile":
                break;
            case "Reminders":
                break;
            case "Monthly Intake":
                break;
            default:
                Toast.makeText(MainActivity.this, "Sorry, It's Under development", Toast.LENGTH_SHORT).show();
                break;
        }
    });
    recyclerView.setAdapter(mainDashboardAdapter);
    DailyMedicineStatisticsAdapter dailyMedicineStatisticsAdapter = new DailyMedicineStatisticsAdapter(this, new ArrayList<>());
    recyclerViewDailyMedicineStatistics.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    // add pager behavior
    PagerSnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerViewDailyMedicineStatistics);
    // pager indicator
    recyclerViewDailyMedicineStatistics.addItemDecoration(new CirclePagerIndicatorDecoration());
    recyclerViewDailyMedicineStatistics.setAdapter(dailyMedicineStatisticsAdapter);
// MedicineViewModel medicineViewModel = ViewModelProviders.of(this).get(MedicineViewModel.class);
// medicineViewModel.getMedicineList().observe(MainActivity.this, dailyMedicineStatisticsAdapter::addItems);
}
Also used : User(com.androidstudy.medmanager.data.model.User) DailyMedicineStatisticsAdapter(com.androidstudy.medmanager.ui.adapter.DailyMedicineStatisticsAdapter) Intent(android.content.Intent) ItemOffsetDecoration(com.androidstudy.medmanager.util.ItemOffsetDecoration) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) GridLayoutManager(android.support.v7.widget.GridLayoutManager) MainDashboardAdapter(com.androidstudy.medmanager.ui.adapter.MainDashboardAdapter) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) MenuView(com.androidstudy.medmanager.data.model.MenuView) CirclePagerIndicatorDecoration(com.androidstudy.medmanager.util.CirclePagerIndicatorDecoration) ActivityMainBinding(com.androidstudy.medmanager.databinding.ActivityMainBinding) SimpleDateFormat(java.text.SimpleDateFormat) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 PagerSnapHelper (android.support.v7.widget.PagerSnapHelper)3 RecyclerView (android.support.v7.widget.RecyclerView)2 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 Drawable (android.graphics.drawable.Drawable)1 GradientDrawable (android.graphics.drawable.GradientDrawable)1 LayerDrawable (android.graphics.drawable.LayerDrawable)1 Bundle (android.os.Bundle)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 SnapHelper (android.support.v7.widget.SnapHelper)1 Toolbar (android.support.v7.widget.Toolbar)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ViewTreeObserver (android.view.ViewTreeObserver)1 BindView (butterknife.BindView)1 ButterKnife (butterknife.ButterKnife)1 Unbinder (butterknife.Unbinder)1 MenuView (com.androidstudy.medmanager.data.model.MenuView)1