猿问

Android MPAndroidChart LineChart 如何完全忽略 ViewPager

我试图让 mpandroidchart 中的折线图忽略 fragmentviewpager,并且在触摸视图时只移动折线图而不移动 viewpager。我的问题是如何存档?


我找到了这个解决方案,但不知道如何实施,也许你们可以帮助我了解如何实施它? https://github.com/PhilJay/MPAndroidChart/issues/1885#issuecomment-267568663


公共类 PagerAdapter 扩展 FragmentStatePagerAdapter {


int mNumberOfTabs;


public PagerAdapter(FragmentManager fm, int NumberOfTabs) {

    super(fm);

    this.mNumberOfTabs = NumberOfTabs;

}


@Override

public Fragment getItem(int position) {

    switch (position) {

        case 0:

            IncomeOutcomeOverviewFragment incomeOutcomeOverviewFragment = new IncomeOutcomeOverviewFragment();

            return incomeOutcomeOverviewFragment;

        case 1:

            IncomeFragment incomeFragment = new IncomeFragment();

            return incomeFragment;

        case 2:

            OutcomeFragment outcomeFragment = new OutcomeFragment();

            return outcomeFragment;

        default:

            return null;

    }

}




@Override

public int getCount() {

    return mNumberOfTabs;

}

}


杨魅力
浏览 109回答 2
2回答

千巷猫影

我认为您想要的不是可滑动的视图寻呼机,因为您必须像这样创建自定义视图寻呼机更新@anomynous建议的更改&nbsp;public class NonSwipeableViewPager extends ViewPager {&nbsp; &nbsp; &nbsp; &nbsp; boolean enabled = false;&nbsp; &nbsp; &nbsp; &nbsp; public NonSwipeableViewPager(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMyScroller();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public NonSwipeableViewPager(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMyScroller();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onInterceptTouchEvent(MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.enabled) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.onInterceptTouchEvent(event);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @SuppressLint("ClickableViewAccessibility")&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onTouchEvent(MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.enabled) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.onTouchEvent(event);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //down one is added for smooth scrolling&nbsp; &nbsp; &nbsp; &nbsp; private void setMyScroller() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> viewpager = ViewPager.class;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field scroller = viewpager.getDeclaredField("mScroller");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scroller.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scroller.set(this, new MyScroller(getContext()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class MyScroller extends Scroller {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public MyScroller(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(context, new DecelerateInterpolator());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void startScroll(int startX, int startY, int dx, int dy, int duration) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setPagingDisabled(boolean enabled) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.enabled = enabled;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }Then on my linechart I used onChartGestureStart and onChartGestureEnd to enable or disable the pageview:&nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nonSwipeableViewPager.setPagingDisabled(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nonSwipeableViewPager.setPagingDisabled(false);&nbsp; &nbsp; &nbsp; &nbsp; }用 xml 中的自定义 viewpager 替换默认 viewpager

阿晨1998

您需要为您的图表禁用触摸,您可以使用此代码,chart.setTouchEnabled(true);
随时随地看视频慕课网APP

相关分类

Java
我要回答