use of org.telegram.ui.Components.ScrollSlidingTextTabStrip in project Telegram-FOSS by Telegram-FOSS-Team.
the class DialogOrContactPickerActivity method createView.
@Override
public View createView(Context context) {
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setTitle(LocaleController.getString("BlockUserMultiTitle", R.string.BlockUserMultiTitle));
if (AndroidUtilities.isTablet()) {
actionBar.setOccupyStatusBar(false);
}
actionBar.setExtraHeight(AndroidUtilities.dp(44));
actionBar.setAllowOverlayTitle(false);
actionBar.setAddToContainer(false);
actionBar.setClipContent(true);
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
}
}
});
hasOwnBackground = true;
ActionBarMenu menu = actionBar.createMenu();
searchItem = menu.addItem(search_button, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
@Override
public void onSearchExpand() {
dialogsActivity.getActionBar().openSearchField("", false);
contactsActivity.getActionBar().openSearchField("", false);
searchItem.getSearchField().requestFocus();
}
@Override
public void onSearchCollapse() {
dialogsActivity.getActionBar().closeSearchField(false);
contactsActivity.getActionBar().closeSearchField(false);
}
@Override
public void onTextChanged(EditText editText) {
dialogsActivity.getActionBar().setSearchFieldText(editText.getText().toString());
contactsActivity.getActionBar().setSearchFieldText(editText.getText().toString());
}
});
searchItem.setSearchFieldHint(LocaleController.getString("Search", R.string.Search));
scrollSlidingTextTabStrip = new ScrollSlidingTextTabStrip(context);
scrollSlidingTextTabStrip.setUseSameWidth(true);
actionBar.addView(scrollSlidingTextTabStrip, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 44, Gravity.LEFT | Gravity.BOTTOM));
scrollSlidingTextTabStrip.setDelegate(new ScrollSlidingTextTabStrip.ScrollSlidingTabStripDelegate() {
@Override
public void onPageSelected(int id, boolean forward) {
if (viewPages[0].selectedType == id) {
return;
}
swipeBackEnabled = id == scrollSlidingTextTabStrip.getFirstTabId();
viewPages[1].selectedType = id;
viewPages[1].setVisibility(View.VISIBLE);
switchToCurrentSelectedMode(true);
animatingForward = forward;
}
@Override
public void onPageScrolled(float progress) {
if (progress == 1 && viewPages[1].getVisibility() != View.VISIBLE) {
return;
}
if (animatingForward) {
viewPages[0].setTranslationX(-progress * viewPages[0].getMeasuredWidth());
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() - progress * viewPages[0].getMeasuredWidth());
} else {
viewPages[0].setTranslationX(progress * viewPages[0].getMeasuredWidth());
viewPages[1].setTranslationX(progress * viewPages[0].getMeasuredWidth() - viewPages[0].getMeasuredWidth());
}
if (progress == 1) {
ViewPage tempPage = viewPages[0];
viewPages[0] = viewPages[1];
viewPages[1] = tempPage;
viewPages[1].setVisibility(View.GONE);
}
}
});
ViewConfiguration configuration = ViewConfiguration.get(context);
maximumVelocity = configuration.getScaledMaximumFlingVelocity();
FrameLayout frameLayout;
fragmentView = frameLayout = new FrameLayout(context) {
private int startedTrackingPointerId;
private boolean startedTracking;
private boolean maybeStartTracking;
private int startedTrackingX;
private int startedTrackingY;
private VelocityTracker velocityTracker;
private boolean globalIgnoreLayout;
private boolean prepareForMoving(MotionEvent ev, boolean forward) {
int id = scrollSlidingTextTabStrip.getNextPageId(forward);
if (id < 0) {
return false;
}
getParent().requestDisallowInterceptTouchEvent(true);
maybeStartTracking = false;
startedTracking = true;
startedTrackingX = (int) ev.getX();
actionBar.setEnabled(false);
scrollSlidingTextTabStrip.setEnabled(false);
viewPages[1].selectedType = id;
viewPages[1].setVisibility(View.VISIBLE);
animatingForward = forward;
switchToCurrentSelectedMode(true);
if (forward) {
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth());
} else {
viewPages[1].setTranslationX(-viewPages[0].getMeasuredWidth());
}
return true;
}
@Override
public void forceHasOverlappingRendering(boolean hasOverlappingRendering) {
super.forceHasOverlappingRendering(hasOverlappingRendering);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
measureChildWithMargins(actionBar, widthMeasureSpec, 0, heightMeasureSpec, 0);
int actionBarHeight = actionBar.getMeasuredHeight();
globalIgnoreLayout = true;
for (int a = 0; a < viewPages.length; a++) {
if (viewPages[a] == null) {
continue;
}
if (viewPages[a].listView != null) {
viewPages[a].listView.setPadding(0, actionBarHeight, 0, 0);
}
if (viewPages[a].listView2 != null) {
viewPages[a].listView2.setPadding(0, actionBarHeight, 0, 0);
}
}
globalIgnoreLayout = false;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child == null || child.getVisibility() == GONE || child == actionBar) {
continue;
}
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (parentLayout != null) {
parentLayout.drawHeaderShadow(canvas, actionBar.getMeasuredHeight() + (int) actionBar.getTranslationY());
}
}
@Override
public void requestLayout() {
if (globalIgnoreLayout) {
return;
}
super.requestLayout();
}
public boolean checkTabsAnimationInProgress() {
if (tabsAnimationInProgress) {
boolean cancel = false;
if (backAnimation) {
if (Math.abs(viewPages[0].getTranslationX()) < 1) {
viewPages[0].setTranslationX(0);
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() * (animatingForward ? 1 : -1));
cancel = true;
}
} else if (Math.abs(viewPages[1].getTranslationX()) < 1) {
viewPages[0].setTranslationX(viewPages[0].getMeasuredWidth() * (animatingForward ? -1 : 1));
viewPages[1].setTranslationX(0);
cancel = true;
}
if (cancel) {
if (tabsAnimation != null) {
tabsAnimation.cancel();
tabsAnimation = null;
}
tabsAnimationInProgress = false;
}
return tabsAnimationInProgress;
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return checkTabsAnimationInProgress() || scrollSlidingTextTabStrip.isAnimatingIndicator() || onTouchEvent(ev);
}
@Override
protected void onDraw(Canvas canvas) {
backgroundPaint.setColor(Theme.getColor(Theme.key_windowBackgroundWhite));
canvas.drawRect(0, actionBar.getMeasuredHeight() + actionBar.getTranslationY(), getMeasuredWidth(), getMeasuredHeight(), backgroundPaint);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!parentLayout.checkTransitionAnimation() && !checkTabsAnimationInProgress()) {
if (ev != null) {
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
}
velocityTracker.addMovement(ev);
}
if (ev != null && ev.getAction() == MotionEvent.ACTION_DOWN && !startedTracking && !maybeStartTracking) {
startedTrackingPointerId = ev.getPointerId(0);
maybeStartTracking = true;
startedTrackingX = (int) ev.getX();
startedTrackingY = (int) ev.getY();
velocityTracker.clear();
} else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId) {
int dx = (int) (ev.getX() - startedTrackingX);
int dy = Math.abs((int) ev.getY() - startedTrackingY);
if (startedTracking && (animatingForward && dx > 0 || !animatingForward && dx < 0)) {
if (!prepareForMoving(ev, dx < 0)) {
maybeStartTracking = true;
startedTracking = false;
viewPages[0].setTranslationX(0);
viewPages[1].setTranslationX(animatingForward ? viewPages[0].getMeasuredWidth() : -viewPages[0].getMeasuredWidth());
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, 0);
}
}
if (maybeStartTracking && !startedTracking) {
float touchSlop = AndroidUtilities.getPixelsInCM(0.3f, true);
if (Math.abs(dx) >= touchSlop && Math.abs(dx) > dy) {
prepareForMoving(ev, dx < 0);
}
} else if (startedTracking) {
viewPages[0].setTranslationX(dx);
if (animatingForward) {
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() + dx);
} else {
viewPages[1].setTranslationX(dx - viewPages[0].getMeasuredWidth());
}
float scrollProgress = Math.abs(dx) / (float) viewPages[0].getMeasuredWidth();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, scrollProgress);
}
} else if (ev == null || ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
float velX;
float velY;
if (ev != null && ev.getAction() != MotionEvent.ACTION_CANCEL) {
velX = velocityTracker.getXVelocity();
velY = velocityTracker.getYVelocity();
if (!startedTracking) {
if (Math.abs(velX) >= 3000 && Math.abs(velX) > Math.abs(velY)) {
prepareForMoving(ev, velX < 0);
}
}
} else {
velX = 0;
velY = 0;
}
if (startedTracking) {
float x = viewPages[0].getX();
tabsAnimation = new AnimatorSet();
backAnimation = Math.abs(x) < viewPages[0].getMeasuredWidth() / 3.0f && (Math.abs(velX) < 3500 || Math.abs(velX) < Math.abs(velY));
float distToMove;
float dx;
if (backAnimation) {
dx = Math.abs(x);
if (animatingForward) {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, 0), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, viewPages[1].getMeasuredWidth()));
} else {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, 0), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, -viewPages[1].getMeasuredWidth()));
}
} else {
dx = viewPages[0].getMeasuredWidth() - Math.abs(x);
if (animatingForward) {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, -viewPages[0].getMeasuredWidth()), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, 0));
} else {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, viewPages[0].getMeasuredWidth()), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, 0));
}
}
tabsAnimation.setInterpolator(interpolator);
int width = getMeasuredWidth();
int halfWidth = width / 2;
float distanceRatio = Math.min(1.0f, 1.0f * dx / (float) width);
float distance = (float) halfWidth + (float) halfWidth * AndroidUtilities.distanceInfluenceForSnapDuration(distanceRatio);
velX = Math.abs(velX);
int duration;
if (velX > 0) {
duration = 4 * Math.round(1000.0f * Math.abs(distance / velX));
} else {
float pageDelta = dx / getMeasuredWidth();
duration = (int) ((pageDelta + 1.0f) * 100.0f);
}
duration = Math.max(150, Math.min(duration, 600));
tabsAnimation.setDuration(duration);
tabsAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animator) {
tabsAnimation = null;
if (backAnimation) {
viewPages[1].setVisibility(View.GONE);
} else {
ViewPage tempPage = viewPages[0];
viewPages[0] = viewPages[1];
viewPages[1] = tempPage;
viewPages[1].setVisibility(View.GONE);
swipeBackEnabled = viewPages[0].selectedType == scrollSlidingTextTabStrip.getFirstTabId();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[0].selectedType, 1.0f);
}
tabsAnimationInProgress = false;
maybeStartTracking = false;
startedTracking = false;
actionBar.setEnabled(true);
scrollSlidingTextTabStrip.setEnabled(true);
}
});
tabsAnimation.start();
tabsAnimationInProgress = true;
startedTracking = false;
} else {
maybeStartTracking = false;
actionBar.setEnabled(true);
scrollSlidingTextTabStrip.setEnabled(true);
}
if (velocityTracker != null) {
velocityTracker.recycle();
velocityTracker = null;
}
}
return startedTracking;
}
return false;
}
};
frameLayout.setWillNotDraw(false);
dialogsActivity.setParentFragment(this);
contactsActivity.setParentFragment(this);
for (int a = 0; a < viewPages.length; a++) {
viewPages[a] = new ViewPage(context) {
@Override
public void setTranslationX(float translationX) {
super.setTranslationX(translationX);
if (tabsAnimationInProgress) {
if (viewPages[0] == this) {
float scrollProgress = Math.abs(viewPages[0].getTranslationX()) / (float) viewPages[0].getMeasuredWidth();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, scrollProgress);
}
}
}
};
frameLayout.addView(viewPages[a], LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
if (a == 0) {
viewPages[a].parentFragment = dialogsActivity;
viewPages[a].listView = dialogsActivity.getListView();
viewPages[a].listView2 = dialogsActivity.getSearchListView();
} else if (a == 1) {
viewPages[a].parentFragment = contactsActivity;
viewPages[a].listView = contactsActivity.getListView();
viewPages[a].setVisibility(View.GONE);
}
viewPages[a].listView.setScrollingTouchSlop(RecyclerView.TOUCH_SLOP_PAGING);
viewPages[a].fragmentView = (FrameLayout) viewPages[a].parentFragment.getFragmentView();
viewPages[a].actionBar = viewPages[a].parentFragment.getActionBar();
viewPages[a].addView(viewPages[a].fragmentView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
viewPages[a].addView(viewPages[a].actionBar, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
viewPages[a].actionBar.setVisibility(View.GONE);
for (int i = 0; i < 2; i++) {
RecyclerListView listView = i == 0 ? viewPages[a].listView : viewPages[a].listView2;
if (listView == null) {
continue;
}
listView.setClipToPadding(false);
RecyclerView.OnScrollListener onScrollListener = listView.getOnScrollListener();
listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
onScrollListener.onScrollStateChanged(recyclerView, newState);
if (newState != RecyclerView.SCROLL_STATE_DRAGGING) {
int scrollY = (int) -actionBar.getTranslationY();
int actionBarHeight = ActionBar.getCurrentActionBarHeight();
if (scrollY != 0 && scrollY != actionBarHeight) {
if (scrollY < actionBarHeight / 2) {
viewPages[0].listView.smoothScrollBy(0, -scrollY);
if (viewPages[0].listView2 != null) {
viewPages[0].listView2.smoothScrollBy(0, -scrollY);
}
} else {
viewPages[0].listView.smoothScrollBy(0, actionBarHeight - scrollY);
if (viewPages[0].listView2 != null) {
viewPages[0].listView2.smoothScrollBy(0, actionBarHeight - scrollY);
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
onScrollListener.onScrolled(recyclerView, dx, dy);
if (recyclerView == viewPages[0].listView || recyclerView == viewPages[0].listView2) {
float currentTranslation = actionBar.getTranslationY();
float newTranslation = currentTranslation - dy;
if (newTranslation < -ActionBar.getCurrentActionBarHeight()) {
newTranslation = -ActionBar.getCurrentActionBarHeight();
} else if (newTranslation > 0) {
newTranslation = 0;
}
if (newTranslation != currentTranslation) {
setScrollY(newTranslation);
}
}
}
});
}
}
frameLayout.addView(actionBar, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
updateTabs();
switchToCurrentSelectedMode(false);
swipeBackEnabled = scrollSlidingTextTabStrip.getCurrentTabId() == scrollSlidingTextTabStrip.getFirstTabId();
return fragmentView;
}
use of org.telegram.ui.Components.ScrollSlidingTextTabStrip in project Telegram-FOSS by Telegram-FOSS-Team.
the class DataUsageActivity method createView.
@Override
public View createView(Context context) {
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setTitle(LocaleController.getString("NetworkUsage", R.string.NetworkUsage));
if (AndroidUtilities.isTablet()) {
actionBar.setOccupyStatusBar(false);
}
actionBar.setExtraHeight(AndroidUtilities.dp(44));
actionBar.setAllowOverlayTitle(false);
actionBar.setAddToContainer(false);
actionBar.setClipContent(true);
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
}
}
});
hasOwnBackground = true;
mobileAdapter = new ListAdapter(context, 0);
wifiAdapter = new ListAdapter(context, 1);
roamingAdapter = new ListAdapter(context, 2);
scrollSlidingTextTabStrip = new ScrollSlidingTextTabStrip(context);
scrollSlidingTextTabStrip.setUseSameWidth(true);
actionBar.addView(scrollSlidingTextTabStrip, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 44, Gravity.LEFT | Gravity.BOTTOM));
scrollSlidingTextTabStrip.setDelegate(new ScrollSlidingTextTabStrip.ScrollSlidingTabStripDelegate() {
@Override
public void onPageSelected(int id, boolean forward) {
if (viewPages[0].selectedType == id) {
return;
}
swipeBackEnabled = id == scrollSlidingTextTabStrip.getFirstTabId();
viewPages[1].selectedType = id;
viewPages[1].setVisibility(View.VISIBLE);
switchToCurrentSelectedMode(true);
animatingForward = forward;
}
@Override
public void onPageScrolled(float progress) {
if (progress == 1 && viewPages[1].getVisibility() != View.VISIBLE) {
return;
}
if (animatingForward) {
viewPages[0].setTranslationX(-progress * viewPages[0].getMeasuredWidth());
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() - progress * viewPages[0].getMeasuredWidth());
} else {
viewPages[0].setTranslationX(progress * viewPages[0].getMeasuredWidth());
viewPages[1].setTranslationX(progress * viewPages[0].getMeasuredWidth() - viewPages[0].getMeasuredWidth());
}
if (progress == 1) {
ViewPage tempPage = viewPages[0];
viewPages[0] = viewPages[1];
viewPages[1] = tempPage;
viewPages[1].setVisibility(View.GONE);
}
}
});
ViewConfiguration configuration = ViewConfiguration.get(context);
maximumVelocity = configuration.getScaledMaximumFlingVelocity();
FrameLayout frameLayout;
fragmentView = frameLayout = new FrameLayout(context) {
private int startedTrackingPointerId;
private boolean startedTracking;
private boolean maybeStartTracking;
private int startedTrackingX;
private int startedTrackingY;
private VelocityTracker velocityTracker;
private boolean globalIgnoreLayout;
private boolean prepareForMoving(MotionEvent ev, boolean forward) {
int id = scrollSlidingTextTabStrip.getNextPageId(forward);
if (id < 0) {
return false;
}
getParent().requestDisallowInterceptTouchEvent(true);
maybeStartTracking = false;
startedTracking = true;
startedTrackingX = (int) ev.getX();
actionBar.setEnabled(false);
scrollSlidingTextTabStrip.setEnabled(false);
viewPages[1].selectedType = id;
viewPages[1].setVisibility(View.VISIBLE);
animatingForward = forward;
switchToCurrentSelectedMode(true);
if (forward) {
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth());
} else {
viewPages[1].setTranslationX(-viewPages[0].getMeasuredWidth());
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
measureChildWithMargins(actionBar, widthMeasureSpec, 0, heightMeasureSpec, 0);
int actionBarHeight = actionBar.getMeasuredHeight();
globalIgnoreLayout = true;
for (int a = 0; a < viewPages.length; a++) {
if (viewPages[a] == null) {
continue;
}
if (viewPages[a].listView != null) {
viewPages[a].listView.setPadding(0, actionBarHeight, 0, AndroidUtilities.dp(4));
}
}
globalIgnoreLayout = false;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child == null || child.getVisibility() == GONE || child == actionBar) {
continue;
}
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (parentLayout != null) {
parentLayout.drawHeaderShadow(canvas, actionBar.getMeasuredHeight() + (int) actionBar.getTranslationY());
}
}
@Override
public void requestLayout() {
if (globalIgnoreLayout) {
return;
}
super.requestLayout();
}
public boolean checkTabsAnimationInProgress() {
if (tabsAnimationInProgress) {
boolean cancel = false;
if (backAnimation) {
if (Math.abs(viewPages[0].getTranslationX()) < 1) {
viewPages[0].setTranslationX(0);
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() * (animatingForward ? 1 : -1));
cancel = true;
}
} else if (Math.abs(viewPages[1].getTranslationX()) < 1) {
viewPages[0].setTranslationX(viewPages[0].getMeasuredWidth() * (animatingForward ? -1 : 1));
viewPages[1].setTranslationX(0);
cancel = true;
}
if (cancel) {
if (tabsAnimation != null) {
tabsAnimation.cancel();
tabsAnimation = null;
}
tabsAnimationInProgress = false;
}
return tabsAnimationInProgress;
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return checkTabsAnimationInProgress() || scrollSlidingTextTabStrip.isAnimatingIndicator() || onTouchEvent(ev);
}
@Override
protected void onDraw(Canvas canvas) {
backgroundPaint.setColor(Theme.getColor(Theme.key_windowBackgroundGray));
canvas.drawRect(0, actionBar.getMeasuredHeight() + actionBar.getTranslationY(), getMeasuredWidth(), getMeasuredHeight(), backgroundPaint);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!parentLayout.checkTransitionAnimation() && !checkTabsAnimationInProgress()) {
if (ev != null) {
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
}
velocityTracker.addMovement(ev);
}
if (ev != null && ev.getAction() == MotionEvent.ACTION_DOWN && !startedTracking && !maybeStartTracking) {
startedTrackingPointerId = ev.getPointerId(0);
maybeStartTracking = true;
startedTrackingX = (int) ev.getX();
startedTrackingY = (int) ev.getY();
velocityTracker.clear();
} else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId) {
int dx = (int) (ev.getX() - startedTrackingX);
int dy = Math.abs((int) ev.getY() - startedTrackingY);
if (startedTracking && (animatingForward && dx > 0 || !animatingForward && dx < 0)) {
if (!prepareForMoving(ev, dx < 0)) {
maybeStartTracking = true;
startedTracking = false;
viewPages[0].setTranslationX(0);
viewPages[1].setTranslationX(animatingForward ? viewPages[0].getMeasuredWidth() : -viewPages[0].getMeasuredWidth());
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, 0);
}
}
if (maybeStartTracking && !startedTracking) {
float touchSlop = AndroidUtilities.getPixelsInCM(0.3f, true);
if (Math.abs(dx) >= touchSlop && Math.abs(dx) > dy) {
prepareForMoving(ev, dx < 0);
}
} else if (startedTracking) {
if (animatingForward) {
viewPages[0].setTranslationX(dx);
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() + dx);
} else {
viewPages[0].setTranslationX(dx);
viewPages[1].setTranslationX(dx - viewPages[0].getMeasuredWidth());
}
float scrollProgress = Math.abs(dx) / (float) viewPages[0].getMeasuredWidth();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, scrollProgress);
}
} else if (ev == null || ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
float velX;
float velY;
if (ev != null && ev.getAction() != MotionEvent.ACTION_CANCEL) {
velX = velocityTracker.getXVelocity();
velY = velocityTracker.getYVelocity();
if (!startedTracking) {
if (Math.abs(velX) >= 3000 && Math.abs(velX) > Math.abs(velY)) {
prepareForMoving(ev, velX < 0);
}
}
} else {
velX = 0;
velY = 0;
}
if (startedTracking) {
float x = viewPages[0].getX();
tabsAnimation = new AnimatorSet();
backAnimation = Math.abs(x) < viewPages[0].getMeasuredWidth() / 3.0f && (Math.abs(velX) < 3500 || Math.abs(velX) < Math.abs(velY));
float distToMove;
float dx;
if (backAnimation) {
dx = Math.abs(x);
if (animatingForward) {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, 0), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, viewPages[1].getMeasuredWidth()));
} else {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, 0), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, -viewPages[1].getMeasuredWidth()));
}
} else {
dx = viewPages[0].getMeasuredWidth() - Math.abs(x);
if (animatingForward) {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, -viewPages[0].getMeasuredWidth()), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, 0));
} else {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, viewPages[0].getMeasuredWidth()), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, 0));
}
}
tabsAnimation.setInterpolator(interpolator);
int width = getMeasuredWidth();
int halfWidth = width / 2;
float distanceRatio = Math.min(1.0f, 1.0f * dx / (float) width);
float distance = (float) halfWidth + (float) halfWidth * AndroidUtilities.distanceInfluenceForSnapDuration(distanceRatio);
velX = Math.abs(velX);
int duration;
if (velX > 0) {
duration = 4 * Math.round(1000.0f * Math.abs(distance / velX));
} else {
float pageDelta = dx / getMeasuredWidth();
duration = (int) ((pageDelta + 1.0f) * 100.0f);
}
duration = Math.max(150, Math.min(duration, 600));
tabsAnimation.setDuration(duration);
tabsAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animator) {
tabsAnimation = null;
if (backAnimation) {
viewPages[1].setVisibility(View.GONE);
} else {
ViewPage tempPage = viewPages[0];
viewPages[0] = viewPages[1];
viewPages[1] = tempPage;
viewPages[1].setVisibility(View.GONE);
swipeBackEnabled = viewPages[0].selectedType == scrollSlidingTextTabStrip.getFirstTabId();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[0].selectedType, 1.0f);
}
tabsAnimationInProgress = false;
maybeStartTracking = false;
startedTracking = false;
actionBar.setEnabled(true);
scrollSlidingTextTabStrip.setEnabled(true);
}
});
tabsAnimation.start();
tabsAnimationInProgress = true;
startedTracking = false;
} else {
maybeStartTracking = false;
actionBar.setEnabled(true);
scrollSlidingTextTabStrip.setEnabled(true);
}
if (velocityTracker != null) {
velocityTracker.recycle();
velocityTracker = null;
}
}
return startedTracking;
}
return false;
}
};
frameLayout.setWillNotDraw(false);
int scrollToPositionOnRecreate = -1;
int scrollToOffsetOnRecreate = 0;
for (int a = 0; a < viewPages.length; a++) {
if (a == 0) {
if (viewPages[a] != null && viewPages[a].layoutManager != null) {
scrollToPositionOnRecreate = viewPages[a].layoutManager.findFirstVisibleItemPosition();
if (scrollToPositionOnRecreate != viewPages[a].layoutManager.getItemCount() - 1) {
RecyclerListView.Holder holder = (RecyclerListView.Holder) viewPages[a].listView.findViewHolderForAdapterPosition(scrollToPositionOnRecreate);
if (holder != null) {
scrollToOffsetOnRecreate = holder.itemView.getTop();
} else {
scrollToPositionOnRecreate = -1;
}
} else {
scrollToPositionOnRecreate = -1;
}
}
}
final ViewPage ViewPage = new ViewPage(context) {
@Override
public void setTranslationX(float translationX) {
super.setTranslationX(translationX);
if (tabsAnimationInProgress) {
if (viewPages[0] == this) {
float scrollProgress = Math.abs(viewPages[0].getTranslationX()) / (float) viewPages[0].getMeasuredWidth();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, scrollProgress);
}
}
}
};
frameLayout.addView(ViewPage, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
viewPages[a] = ViewPage;
final LinearLayoutManager layoutManager = viewPages[a].layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) {
@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}
};
RecyclerListView listView = new RecyclerListView(context);
viewPages[a].listView = listView;
viewPages[a].listView.setScrollingTouchSlop(RecyclerView.TOUCH_SLOP_PAGING);
viewPages[a].listView.setItemAnimator(null);
viewPages[a].listView.setClipToPadding(false);
viewPages[a].listView.setSectionsType(2);
viewPages[a].listView.setLayoutManager(layoutManager);
viewPages[a].addView(viewPages[a].listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
viewPages[a].listView.setOnItemClickListener((view, position) -> {
if (getParentActivity() == null) {
return;
}
ListAdapter adapter = (ListAdapter) listView.getAdapter();
if (position == adapter.resetRow) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setTitle(LocaleController.getString("ResetStatisticsAlertTitle", R.string.ResetStatisticsAlertTitle));
builder.setMessage(LocaleController.getString("ResetStatisticsAlert", R.string.ResetStatisticsAlert));
builder.setPositiveButton(LocaleController.getString("Reset", R.string.Reset), (dialogInterface, i) -> {
StatsController.getInstance(currentAccount).resetStats(adapter.currentType);
adapter.notifyDataSetChanged();
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
AlertDialog dialog = builder.create();
showDialog(dialog);
TextView button = (TextView) dialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (button != null) {
button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
}
}
});
viewPages[a].listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState != RecyclerView.SCROLL_STATE_DRAGGING) {
int scrollY = (int) -actionBar.getTranslationY();
int actionBarHeight = ActionBar.getCurrentActionBarHeight();
if (scrollY != 0 && scrollY != actionBarHeight) {
if (scrollY < actionBarHeight / 2) {
viewPages[0].listView.smoothScrollBy(0, -scrollY);
} else {
viewPages[0].listView.smoothScrollBy(0, actionBarHeight - scrollY);
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (recyclerView == viewPages[0].listView) {
float currentTranslation = actionBar.getTranslationY();
float newTranslation = currentTranslation - dy;
if (newTranslation < -ActionBar.getCurrentActionBarHeight()) {
newTranslation = -ActionBar.getCurrentActionBarHeight();
} else if (newTranslation > 0) {
newTranslation = 0;
}
if (newTranslation != currentTranslation) {
setScrollY(newTranslation);
}
}
}
});
if (a == 0 && scrollToPositionOnRecreate != -1) {
layoutManager.scrollToPositionWithOffset(scrollToPositionOnRecreate, scrollToOffsetOnRecreate);
}
if (a != 0) {
viewPages[a].setVisibility(View.GONE);
}
}
frameLayout.addView(actionBar, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
updateTabs();
switchToCurrentSelectedMode(false);
swipeBackEnabled = scrollSlidingTextTabStrip.getCurrentTabId() == scrollSlidingTextTabStrip.getFirstTabId();
return fragmentView;
}
use of org.telegram.ui.Components.ScrollSlidingTextTabStrip in project Telegram-FOSS by Telegram-FOSS-Team.
the class PhotoPickerSearchActivity method createView.
@Override
public View createView(Context context) {
actionBar.setBackgroundColor(Theme.getColor(Theme.key_dialogBackground));
actionBar.setTitleColor(Theme.getColor(Theme.key_dialogTextBlack));
actionBar.setItemsColor(Theme.getColor(Theme.key_dialogTextBlack), false);
actionBar.setItemsBackgroundColor(Theme.getColor(Theme.key_dialogButtonSelector), false);
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
if (AndroidUtilities.isTablet()) {
actionBar.setOccupyStatusBar(false);
}
actionBar.setExtraHeight(AndroidUtilities.dp(44));
actionBar.setAllowOverlayTitle(false);
actionBar.setAddToContainer(false);
actionBar.setClipContent(true);
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
}
}
});
hasOwnBackground = true;
ActionBarMenu menu = actionBar.createMenu();
searchItem = menu.addItem(search_button, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
@Override
public void onSearchExpand() {
imagesSearch.getActionBar().openSearchField("", false);
gifsSearch.getActionBar().openSearchField("", false);
searchItem.getSearchField().requestFocus();
}
@Override
public boolean canCollapseSearch() {
finishFragment();
return false;
}
@Override
public void onTextChanged(EditText editText) {
imagesSearch.getActionBar().setSearchFieldText(editText.getText().toString());
gifsSearch.getActionBar().setSearchFieldText(editText.getText().toString());
}
@Override
public void onSearchPressed(EditText editText) {
imagesSearch.getActionBar().onSearchPressed();
gifsSearch.getActionBar().onSearchPressed();
}
});
searchItem.setSearchFieldHint(LocaleController.getString("SearchImagesTitle", R.string.SearchImagesTitle));
EditTextBoldCursor editText = searchItem.getSearchField();
editText.setTextColor(Theme.getColor(Theme.key_dialogTextBlack));
editText.setCursorColor(Theme.getColor(Theme.key_dialogTextBlack));
editText.setHintTextColor(Theme.getColor(Theme.key_chat_messagePanelHint));
scrollSlidingTextTabStrip = new ScrollSlidingTextTabStrip(context);
scrollSlidingTextTabStrip.setUseSameWidth(true);
scrollSlidingTextTabStrip.setColors(Theme.key_chat_attachActiveTab, Theme.key_chat_attachActiveTab, Theme.key_chat_attachUnactiveTab, Theme.key_dialogButtonSelector);
actionBar.addView(scrollSlidingTextTabStrip, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 44, Gravity.LEFT | Gravity.BOTTOM));
scrollSlidingTextTabStrip.setDelegate(new ScrollSlidingTextTabStrip.ScrollSlidingTabStripDelegate() {
@Override
public void onPageSelected(int id, boolean forward) {
if (viewPages[0].selectedType == id) {
return;
}
swipeBackEnabled = id == scrollSlidingTextTabStrip.getFirstTabId();
viewPages[1].selectedType = id;
viewPages[1].setVisibility(View.VISIBLE);
switchToCurrentSelectedMode(true);
animatingForward = forward;
if (id == 0) {
searchItem.setSearchFieldHint(LocaleController.getString("SearchImagesTitle", R.string.SearchImagesTitle));
} else {
searchItem.setSearchFieldHint(LocaleController.getString("SearchGifsTitle", R.string.SearchGifsTitle));
}
}
@Override
public void onPageScrolled(float progress) {
if (progress == 1 && viewPages[1].getVisibility() != View.VISIBLE) {
return;
}
if (animatingForward) {
viewPages[0].setTranslationX(-progress * viewPages[0].getMeasuredWidth());
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() - progress * viewPages[0].getMeasuredWidth());
} else {
viewPages[0].setTranslationX(progress * viewPages[0].getMeasuredWidth());
viewPages[1].setTranslationX(progress * viewPages[0].getMeasuredWidth() - viewPages[0].getMeasuredWidth());
}
if (progress == 1) {
ViewPage tempPage = viewPages[0];
viewPages[0] = viewPages[1];
viewPages[1] = tempPage;
viewPages[1].setVisibility(View.GONE);
}
}
});
ViewConfiguration configuration = ViewConfiguration.get(context);
maximumVelocity = configuration.getScaledMaximumFlingVelocity();
SizeNotifierFrameLayout sizeNotifierFrameLayout;
fragmentView = sizeNotifierFrameLayout = new SizeNotifierFrameLayout(context) {
private int startedTrackingPointerId;
private boolean startedTracking;
private boolean maybeStartTracking;
private int startedTrackingX;
private int startedTrackingY;
private VelocityTracker velocityTracker;
private boolean globalIgnoreLayout;
private boolean prepareForMoving(MotionEvent ev, boolean forward) {
int id = scrollSlidingTextTabStrip.getNextPageId(forward);
if (id < 0) {
return false;
}
getParent().requestDisallowInterceptTouchEvent(true);
maybeStartTracking = false;
startedTracking = true;
startedTrackingX = (int) ev.getX();
actionBar.setEnabled(false);
scrollSlidingTextTabStrip.setEnabled(false);
viewPages[1].selectedType = id;
viewPages[1].setVisibility(View.VISIBLE);
animatingForward = forward;
switchToCurrentSelectedMode(true);
if (forward) {
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth());
} else {
viewPages[1].setTranslationX(-viewPages[0].getMeasuredWidth());
}
return true;
}
@Override
public void forceHasOverlappingRendering(boolean hasOverlappingRendering) {
super.forceHasOverlappingRendering(hasOverlappingRendering);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
measureChildWithMargins(actionBar, widthMeasureSpec, 0, heightMeasureSpec, 0);
int keyboardSize = SharedConfig.smoothKeyboard ? 0 : measureKeyboardHeight();
if (keyboardSize <= AndroidUtilities.dp(20)) {
if (!AndroidUtilities.isInMultiwindow) {
heightSize -= commentTextView.getEmojiPadding();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
}
} else {
globalIgnoreLayout = true;
commentTextView.hideEmojiView();
globalIgnoreLayout = false;
}
int actionBarHeight = actionBar.getMeasuredHeight();
globalIgnoreLayout = true;
for (int a = 0; a < viewPages.length; a++) {
if (viewPages[a] == null) {
continue;
}
if (viewPages[a].listView != null) {
viewPages[a].listView.setPadding(AndroidUtilities.dp(4), actionBarHeight + AndroidUtilities.dp(4), AndroidUtilities.dp(4), AndroidUtilities.dp(4));
}
}
globalIgnoreLayout = false;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child == null || child.getVisibility() == GONE || child == actionBar) {
continue;
}
if (commentTextView != null && commentTextView.isPopupView(child)) {
if (AndroidUtilities.isInMultiwindow || AndroidUtilities.isTablet()) {
if (AndroidUtilities.isTablet()) {
child.measure(MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(Math.min(AndroidUtilities.dp(AndroidUtilities.isTablet() ? 200 : 320), heightSize - AndroidUtilities.statusBarHeight + getPaddingTop()), MeasureSpec.EXACTLY));
} else {
child.measure(MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSize - AndroidUtilities.statusBarHeight + getPaddingTop(), MeasureSpec.EXACTLY));
}
} else {
child.measure(MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(child.getLayoutParams().height, MeasureSpec.EXACTLY));
}
} else {
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
int keyboardSize = SharedConfig.smoothKeyboard ? 0 : measureKeyboardHeight();
int paddingBottom = keyboardSize <= AndroidUtilities.dp(20) && !AndroidUtilities.isInMultiwindow && !AndroidUtilities.isTablet() ? commentTextView.getEmojiPadding() : 0;
setBottomClip(paddingBottom);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
int childLeft;
int childTop;
int gravity = lp.gravity;
if (gravity == -1) {
gravity = Gravity.TOP | Gravity.LEFT;
}
final int absoluteGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
switch(absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft = (r - l - width) / 2 + lp.leftMargin - lp.rightMargin;
break;
case Gravity.RIGHT:
childLeft = (r - l) - width - lp.rightMargin - getPaddingRight();
break;
case Gravity.LEFT:
default:
childLeft = lp.leftMargin + getPaddingLeft();
}
switch(verticalGravity) {
case Gravity.TOP:
childTop = lp.topMargin + getPaddingTop();
break;
case Gravity.CENTER_VERTICAL:
childTop = ((b - paddingBottom) - t - height) / 2 + lp.topMargin - lp.bottomMargin;
break;
case Gravity.BOTTOM:
childTop = ((b - paddingBottom) - t) - height - lp.bottomMargin;
break;
default:
childTop = lp.topMargin;
}
if (commentTextView != null && commentTextView.isPopupView(child)) {
if (AndroidUtilities.isTablet()) {
childTop = getMeasuredHeight() - child.getMeasuredHeight();
} else {
childTop = getMeasuredHeight() + keyboardSize - child.getMeasuredHeight();
}
}
child.layout(childLeft, childTop, childLeft + width, childTop + height);
}
notifyHeightChanged();
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (parentLayout != null) {
parentLayout.drawHeaderShadow(canvas, actionBar.getMeasuredHeight() + (int) actionBar.getTranslationY());
}
}
@Override
public void requestLayout() {
if (globalIgnoreLayout) {
return;
}
super.requestLayout();
}
public boolean checkTabsAnimationInProgress() {
if (tabsAnimationInProgress) {
boolean cancel = false;
if (backAnimation) {
if (Math.abs(viewPages[0].getTranslationX()) < 1) {
viewPages[0].setTranslationX(0);
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() * (animatingForward ? 1 : -1));
cancel = true;
}
} else if (Math.abs(viewPages[1].getTranslationX()) < 1) {
viewPages[0].setTranslationX(viewPages[0].getMeasuredWidth() * (animatingForward ? -1 : 1));
viewPages[1].setTranslationX(0);
cancel = true;
}
if (cancel) {
if (tabsAnimation != null) {
tabsAnimation.cancel();
tabsAnimation = null;
}
tabsAnimationInProgress = false;
}
return tabsAnimationInProgress;
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return checkTabsAnimationInProgress() || scrollSlidingTextTabStrip.isAnimatingIndicator() || onTouchEvent(ev);
}
@Override
protected void onDraw(Canvas canvas) {
backgroundPaint.setColor(Theme.getColor(Theme.key_windowBackgroundGray));
canvas.drawRect(0, actionBar.getMeasuredHeight() + actionBar.getTranslationY(), getMeasuredWidth(), getMeasuredHeight(), backgroundPaint);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!parentLayout.checkTransitionAnimation() && !checkTabsAnimationInProgress()) {
if (ev != null) {
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
}
velocityTracker.addMovement(ev);
}
if (ev != null && ev.getAction() == MotionEvent.ACTION_DOWN && !startedTracking && !maybeStartTracking) {
startedTrackingPointerId = ev.getPointerId(0);
maybeStartTracking = true;
startedTrackingX = (int) ev.getX();
startedTrackingY = (int) ev.getY();
velocityTracker.clear();
} else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId) {
int dx = (int) (ev.getX() - startedTrackingX);
int dy = Math.abs((int) ev.getY() - startedTrackingY);
if (startedTracking && (animatingForward && dx > 0 || !animatingForward && dx < 0)) {
if (!prepareForMoving(ev, dx < 0)) {
maybeStartTracking = true;
startedTracking = false;
viewPages[0].setTranslationX(0);
viewPages[1].setTranslationX(animatingForward ? viewPages[0].getMeasuredWidth() : -viewPages[0].getMeasuredWidth());
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, 0);
}
}
if (maybeStartTracking && !startedTracking) {
float touchSlop = AndroidUtilities.getPixelsInCM(0.3f, true);
if (Math.abs(dx) >= touchSlop && Math.abs(dx) > dy) {
prepareForMoving(ev, dx < 0);
}
} else if (startedTracking) {
viewPages[0].setTranslationX(dx);
if (animatingForward) {
viewPages[1].setTranslationX(viewPages[0].getMeasuredWidth() + dx);
} else {
viewPages[1].setTranslationX(dx - viewPages[0].getMeasuredWidth());
}
float scrollProgress = Math.abs(dx) / (float) viewPages[0].getMeasuredWidth();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, scrollProgress);
}
} else if (ev == null || ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
float velX;
float velY;
if (ev != null && ev.getAction() != MotionEvent.ACTION_CANCEL) {
velX = velocityTracker.getXVelocity();
velY = velocityTracker.getYVelocity();
if (!startedTracking) {
if (Math.abs(velX) >= 3000 && Math.abs(velX) > Math.abs(velY)) {
prepareForMoving(ev, velX < 0);
}
}
} else {
velX = 0;
velY = 0;
}
if (startedTracking) {
float x = viewPages[0].getX();
tabsAnimation = new AnimatorSet();
backAnimation = Math.abs(x) < viewPages[0].getMeasuredWidth() / 3.0f && (Math.abs(velX) < 3500 || Math.abs(velX) < Math.abs(velY));
float distToMove;
float dx;
if (backAnimation) {
dx = Math.abs(x);
if (animatingForward) {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, 0), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, viewPages[1].getMeasuredWidth()));
} else {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, 0), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, -viewPages[1].getMeasuredWidth()));
}
} else {
dx = viewPages[0].getMeasuredWidth() - Math.abs(x);
if (animatingForward) {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, -viewPages[0].getMeasuredWidth()), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, 0));
} else {
tabsAnimation.playTogether(ObjectAnimator.ofFloat(viewPages[0], View.TRANSLATION_X, viewPages[0].getMeasuredWidth()), ObjectAnimator.ofFloat(viewPages[1], View.TRANSLATION_X, 0));
}
}
tabsAnimation.setInterpolator(interpolator);
int width = getMeasuredWidth();
int halfWidth = width / 2;
float distanceRatio = Math.min(1.0f, 1.0f * dx / (float) width);
float distance = (float) halfWidth + (float) halfWidth * AndroidUtilities.distanceInfluenceForSnapDuration(distanceRatio);
velX = Math.abs(velX);
int duration;
if (velX > 0) {
duration = 4 * Math.round(1000.0f * Math.abs(distance / velX));
} else {
float pageDelta = dx / getMeasuredWidth();
duration = (int) ((pageDelta + 1.0f) * 100.0f);
}
duration = Math.max(150, Math.min(duration, 600));
tabsAnimation.setDuration(duration);
tabsAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animator) {
tabsAnimation = null;
if (backAnimation) {
viewPages[1].setVisibility(View.GONE);
} else {
ViewPage tempPage = viewPages[0];
viewPages[0] = viewPages[1];
viewPages[1] = tempPage;
viewPages[1].setVisibility(View.GONE);
swipeBackEnabled = viewPages[0].selectedType == scrollSlidingTextTabStrip.getFirstTabId();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[0].selectedType, 1.0f);
}
tabsAnimationInProgress = false;
maybeStartTracking = false;
startedTracking = false;
actionBar.setEnabled(true);
scrollSlidingTextTabStrip.setEnabled(true);
}
});
tabsAnimation.start();
tabsAnimationInProgress = true;
startedTracking = false;
} else {
maybeStartTracking = false;
actionBar.setEnabled(true);
scrollSlidingTextTabStrip.setEnabled(true);
}
if (velocityTracker != null) {
velocityTracker.recycle();
velocityTracker = null;
}
}
return startedTracking;
}
return false;
}
};
sizeNotifierFrameLayout.setWillNotDraw(false);
imagesSearch.setParentFragment(this);
commentTextView = imagesSearch.commentTextView;
commentTextView.setSizeNotifierLayout(sizeNotifierFrameLayout);
for (int a = 0; a < 4; a++) {
View view;
switch(a) {
case 0:
view = imagesSearch.frameLayout2;
break;
case 1:
view = imagesSearch.writeButtonContainer;
break;
case 2:
view = imagesSearch.selectedCountView;
break;
case 3:
default:
view = imagesSearch.shadow;
break;
}
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
gifsSearch.setLayoutViews(imagesSearch.frameLayout2, imagesSearch.writeButtonContainer, imagesSearch.selectedCountView, imagesSearch.shadow, imagesSearch.commentTextView);
gifsSearch.setParentFragment(this);
for (int a = 0; a < viewPages.length; a++) {
viewPages[a] = new ViewPage(context) {
@Override
public void setTranslationX(float translationX) {
super.setTranslationX(translationX);
if (tabsAnimationInProgress) {
if (viewPages[0] == this) {
float scrollProgress = Math.abs(viewPages[0].getTranslationX()) / (float) viewPages[0].getMeasuredWidth();
scrollSlidingTextTabStrip.selectTabWithId(viewPages[1].selectedType, scrollProgress);
}
}
}
};
sizeNotifierFrameLayout.addView(viewPages[a], LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
if (a == 0) {
viewPages[a].parentFragment = imagesSearch;
viewPages[a].listView = imagesSearch.getListView();
} else if (a == 1) {
viewPages[a].parentFragment = gifsSearch;
viewPages[a].listView = gifsSearch.getListView();
viewPages[a].setVisibility(View.GONE);
}
viewPages[a].listView.setScrollingTouchSlop(RecyclerView.TOUCH_SLOP_PAGING);
viewPages[a].fragmentView = (FrameLayout) viewPages[a].parentFragment.getFragmentView();
viewPages[a].listView.setClipToPadding(false);
viewPages[a].actionBar = viewPages[a].parentFragment.getActionBar();
viewPages[a].addView(viewPages[a].fragmentView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
viewPages[a].addView(viewPages[a].actionBar, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
viewPages[a].actionBar.setVisibility(View.GONE);
RecyclerView.OnScrollListener onScrollListener = viewPages[a].listView.getOnScrollListener();
viewPages[a].listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
onScrollListener.onScrollStateChanged(recyclerView, newState);
if (newState != RecyclerView.SCROLL_STATE_DRAGGING) {
int scrollY = (int) -actionBar.getTranslationY();
int actionBarHeight = ActionBar.getCurrentActionBarHeight();
if (scrollY != 0 && scrollY != actionBarHeight) {
if (scrollY < actionBarHeight / 2) {
viewPages[0].listView.smoothScrollBy(0, -scrollY);
} else {
viewPages[0].listView.smoothScrollBy(0, actionBarHeight - scrollY);
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
onScrollListener.onScrolled(recyclerView, dx, dy);
if (recyclerView == viewPages[0].listView) {
float currentTranslation = actionBar.getTranslationY();
float newTranslation = currentTranslation - dy;
if (newTranslation < -ActionBar.getCurrentActionBarHeight()) {
newTranslation = -ActionBar.getCurrentActionBarHeight();
} else if (newTranslation > 0) {
newTranslation = 0;
}
if (newTranslation != currentTranslation) {
setScrollY(newTranslation);
}
}
}
});
}
sizeNotifierFrameLayout.addView(actionBar, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
sizeNotifierFrameLayout.addView(imagesSearch.shadow, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 3, Gravity.BOTTOM | Gravity.LEFT, 0, 0, 0, 48));
sizeNotifierFrameLayout.addView(imagesSearch.frameLayout2, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.BOTTOM));
sizeNotifierFrameLayout.addView(imagesSearch.writeButtonContainer, LayoutHelper.createFrame(60, 60, Gravity.RIGHT | Gravity.BOTTOM, 0, 0, 12, 10));
sizeNotifierFrameLayout.addView(imagesSearch.selectedCountView, LayoutHelper.createFrame(42, 24, Gravity.RIGHT | Gravity.BOTTOM, 0, 0, -2, 9));
updateTabs();
switchToCurrentSelectedMode(false);
swipeBackEnabled = scrollSlidingTextTabStrip.getCurrentTabId() == scrollSlidingTextTabStrip.getFirstTabId();
return fragmentView;
}
Aggregations