gpt4 book ai didi

Android-ObservableScrollView - 使用 ViewPagerTabListViewFragment 时意外触摸会更改子 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:30 28 4
gpt4 key购买 nike

我正在使用 Android-ObservableScrollView在我的项目中。我面临一个问题,即使我只是触摸 viewpager,SlidingTab 也会捕获此事件,并且 View 寻呼机的当前 fragment 会发生变化。我粘贴了我项目中的所有代码,但大部分代码与示例项目相似。

我在网上调查了这个问题,发现this issue有用,但我尝试了这个解决方案,但它对我不起作用。请帮忙。

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)类 ViewPagerTabListViewFragment 扩展 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"/>

请帮忙!

提前致谢。

最佳答案

看来是 ObservableListView 内部的问题。
为了拦截触摸事件,ObservableListView 被设计为调用其父级的 onInterceptTouchEvent() 并且父级 - 在本例中,父级是 ViewPager - 意外地处理了触摸事件,这导致了这种行为。

无论如何,我找到了解决方法。
您可以在您的应用中试试这个吗?

  1. 添加一个 ID 到根 ViewGroup在你的 MainActivity-Layout.xml 中,比如 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.
    -->
  2. 然后将这个FrameLayout设置为TouchInterceptionViewGroupViewPagerTabListViewFragment.java 中的 ObservableListView。也许围绕 listView.setScrollViewCallbacks() 是好的。

    listView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.root));
    listView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);

关于Android-ObservableScrollView - 使用 ViewPagerTabListViewFragment 时意外触摸会更改子 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991358/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com