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;
}
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);
}
}
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);
}
Aggregations