Android observablescrollview – accidental touch to change sub fragments when using viewpagertablistviewfragment
I'm using Android observablescrollview in my project. I'm facing a problem. Even if I just touch the viewpager, the slidingtab captures this event, and the current fragment of the view pager will change. I pasted all the code in my project, but most of the code is similar to the sample project
I investigated this problem online and found this issue very useful, but I tried this solution, but it didn't work for me. Please help
THIS IS THE ISSUE
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
mToolBar = (Toolbar) findViewById(R.id.toolbar);
refreshJobsData = true;
setUpViewPager();
}
public void setUpViewPager() {
mHeaderView = findViewById(R.id.header);
ViewCompat.setElevation(mHeaderView, getResources().getDimension(R.dimen.toolbar_elevation));
mToolbarView = findViewById(R.id.toolbar);
mPagerAdapter = new NavigationAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mPagerAdapter);
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
slidingTabLayout.setCustomTabView(R.layout.tab_indicator, android.R.id.text1);
slidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.theme_green_dark));
slidingTabLayout.setDistributeEvenly(false);
slidingTabLayout.setViewPager(mPager);
// When the page is selected, other fragments' scrollY should be adjusted
// according to the toolbar status(shown/hidden)
slidingTabLayout.setOnPagechangelistener(new ViewPager.OnPagechangelistener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
propagateToolbarState(toolbarIsShown());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
propagateToolbarState(toolbarIsShown());
}
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
if (dragging) {
int toolbarHeight = mToolbarView.getHeight();
float currentHeaderTranslationY = ViewHelper.getTranslationY(mHeaderView);
if (firstScroll) {
if (-toolbarHeight < currentHeaderTranslationY) {
mBaseTranslationY = scrollY;
}
}
float headerTranslationY = ScrollUtils.getFloat(-(scrollY - mBaseTranslationY), -toolbarHeight, 0);
ViewPropertyAnimator.animate(mHeaderView).cancel();
ViewHelper.setTranslationY(mHeaderView, headerTranslationY);
}
}
@Override
public void onDownMotionEvent() {
}
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
mBaseTranslationY = 0;
Fragment fragment = getCurrentFragment();
if (fragment == null) {
return;
}
View view = fragment.getView();
if (view == null) {
return;
}
int toolbarHeight = mToolbarView.getHeight();
final ObservableListView listView = (ObservableListView) view.findViewById(R.id.scroll);
if (listView == null) {
return;
}
int scrollY = listView.getCurrentScrollY();
if (scrollState == ScrollState.DOWN) {
showToolbar();
} else if (scrollState == ScrollState.UP) {
if (toolbarHeight <= scrollY) {
hideToolbar();
} else {
showToolbar();
}
} else {
// Even if onScrollChanged occurs without scrollY changing, toolbar should be adjusted
if (toolbarIsShown() || toolbarIsHidden()) {
// Toolbar is completely moved, so just keep its state
// and propagate it to other pages
propagateToolbarState(toolbarIsShown());
} else {
// Toolbar is moving but doesn't kNow which to move:
// you can change this to hideToolbar()
showToolbar();
}
}
}
private Fragment getCurrentFragment() {
return mPagerAdapter.getItemAt(mPager.getCurrentItem());
}
private void propagateToolbarState(boolean isShown) {
int toolbarHeight = mToolbarView.getHeight();
// Set scrollY for the fragments that are not created yet
mPagerAdapter.setScrollY(isShown ? 0 : toolbarHeight);
// Set scrollY for the active fragments
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
// Skip current item
if (i == mPager.getCurrentItem()) {
continue;
}
// Skip destroyed or not created item
Fragment f = mPagerAdapter.getItemAt(i);
if (f == null) {
continue;
}
ObservableListView listView = (ObservableListView) f.getView().findViewById(R.id.scroll);
if (isShown) {
// Scroll up
if (0 < listView.getCurrentScrollY()) {
listView.setSelection(0);
}
} else {
// Scroll down (to hide padding)
if (listView.getCurrentScrollY() < toolbarHeight) {
listView.setSelection(1);
}
}
}
}
private boolean toolbarIsShown() {
return ViewHelper.getTranslationY(mHeaderView) == 0;
}
private boolean toolbarIsHidden() {
return ViewHelper.getTranslationY(mHeaderView) == -mToolbarView.getHeight();
}
private void showToolbar() {
float headerTranslationY = ViewHelper.getTranslationY(mHeaderView);
if (headerTranslationY != 0) {
ViewPropertyAnimator.animate(mHeaderView).cancel();
ViewPropertyAnimator.animate(mHeaderView).translationY(0).setDuration(200).start();
}
propagateToolbarState(true);
}
private void hideToolbar() {
float headerTranslationY = ViewHelper.getTranslationY(mHeaderView);
int toolbarHeight = mToolbarView.getHeight();
if (headerTranslationY != -toolbarHeight) {
ViewPropertyAnimator.animate(mHeaderView).cancel();
ViewPropertyAnimator.animate(mHeaderView).translationY(-toolbarHeight).setDuration(200).start();
}
propagateToolbarState(false);
}
private static class NavigationAdapter extends CacheFragmentStatePagerAdapter {
private static final String[] TITLES = new String[]{"Applepie", "Butter Cookie", "Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop"};
private int mScrollY;
public NavigationAdapter(FragmentManager fm) {
super(fm);
}
public void setScrollY(int scrollY) {
mScrollY = scrollY;
}
@Override
protected Fragment createItem(int position) {
Fragment f = new ViewPagerTabListViewFragment();
if (0 < mScrollY) {
Bundle args = new Bundle();
args.putInt(ViewPagerTabListViewFragment.ARG_INITIAL_POSITION, 1);
f.setArguments(args);
}
return f;
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
}
ViewPagerTabListViewFragment.java
public class ViewPagerTabListViewFragment extends Fragment {
public static final String ARG_INITIAL_POSITION = "ARG_INITIAL_POSITION";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview, container, false);
Activity parentActivity = getActivity();
final ObservableListView listView = (ObservableListView) view.findViewById(R.id.scroll);
setDummyDataWithHeader(listView, inflater.inflate(R.layout.padding, null));
if (parentActivity instanceof ObservableScrollViewCallbacks) {
// Scroll to the specified position after layout
Bundle args = getArguments();
if (args != null && args.containsKey(ARG_INITIAL_POSITION)) {
final int initialPosition = args.getInt(ARG_INITIAL_POSITION, 0);
ScrollUtils.addOnGlobalLayoutListener(listView, new Runnable() {
@Override
public void run() {
// scrollTo() doesn't work, should use setSelection()
listView.setSelection(initialPosition);
}
});
}
listView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
}
return view;
}
protected void setDummyDataWithHeader(ListView listView, View headerView) {
listView.addHeaderView(headerView);
final ArrayList<String> list = Utils.getDummyList();
String[] array = list.toArray(new String[list.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Utils.showToast(getActivity(), list.get(position).toUpperCase());
}
});
}
}
MainActivity- Layout.xml
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
Padding for ViewPager must be set outside the ViewPager itself
because with padding, EdgeEffect of ViewPager become strange.
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="48dp">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/theme_green_dark"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
app:theme="@style/Toolbar"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
<!--<ImageView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="@dimen/dimen_160_dp"-->
<!--android:background="@color/loader_dark_color"/>-->
<com.callathome.consumer.widgets.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height"
android:background="@color/white"/>
</LinearLayout>
</FrameLayout>
<fragment
android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.callathome.consumer.fragment.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"/>
Please help!
Thank you in advance
resolvent:
It seems that this is a problem in observablelistview. To intercept touch events, observablelistview aims to call its parent's onintercepttouchevent() and its parent - in this case, the parent is viewpager - to handle touch events unexpectedly, which will lead to this behavior
Anyway, I found a solution. Can you try this in your application?
>Add an ID similar to the root ViewGroup in your mainactivity-layout.xml, such as Android: id = "@ ID / root":
<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
Padding for ViewPager must be set outside the ViewPager itself
because with padding, EdgeEffect of ViewPager become strange.
-->
>Then set this FrameLayout to touchinterceptionviewgroup to observablelistview in viewpagertablistviewfragment.java. Maybe it's good to surround listview. Setscrollviewcallbacks()
listView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.root));
listView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);