每次ViewPager处于活动状态时动画MPAndroidChart

时间:2017-06-26 16:58:24

标签: android mpandroidchart

首先, the component很棒,谢谢

我有一个有3页的ViewPager,我正在使用Piechart,当我换到另一个页面时,我想为图表设置动画。 在第一页,动画工作。但是当改变到其他页面时却没有。 我的图表在列表中。

这是代码:

在片段中创建适配器:

listView.setAdapter(charts);
listView.setOffscreenPageLimit(1);

listView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
   @Override
   public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.e("DEBUG","CHART Reanimation" + position);
            charts.animateChart(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

这是适配器代码。

public class ChartMyFinancesAdapter extends PagerAdapter{
private Context mContext;
private List<View> itemViewList;
private List<Integer> month;
private List<PieChart> mChart;
private TypefacedTextView monthLabel, daysLabel;
private BtnClickListener listener;
private List<Integer> values = new ArrayList<>();
private double fator = 15.68f;

public ChartMyFinancesAdapter(Context mContext, BtnClickListener listener) {
    this.mContext = mContext;
    this.itemViewList = new ArrayList<>();
    this.listener = listener;
    this.month = createMonths(mContext);
    this.mChart = new ArrayList<>();
}


@Override
    public Object instantiateItem(final ViewGroup container, final int position) {

    mChart.add((PieChart) itemView.findViewById(R.id.chart));
    mChart.get(mChart.size()-1).getLegend().setEnabled(false);
    mChart.get(mChart.size()-1).getDescription().setEnabled(false);
    mChart.get(mChart.size()-1).setUsePercentValues(true);

    setupGraph(mChart.get(mChart.size() - 1), false, mChart.size() - 1);

}

private void setupGraph(PieChart chart,boolean destaque, int position){
    randomValues(100,10);
    ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

    // Here I format the chart data.....

    set.setValueTextSize(25);
    set.setDrawValues(false);


    PieData data = new PieData(set);
    mChart.get(position).setData(data);
    if(destaque)mChart.get(position).notifyDataSetChanged();
    mChart.get(position).invalidate();
    mChart.get(position).animateXY(2000,2000);
    mChart.get(position).setRotationEnabled(false);



    container.addView(itemView);
    itemViewList.add(itemView);
    return itemView;

}

public void animateChart(int page){
    Log.e("DEBUG","Animate chart:"+page);

    mChart.get(page).notifyDataSetChanged();
    mChart.get(page).invalidate();

    mChart.get(page).animateXY(2000, 2000);
    //mChart.get(page).invalidate();


    //setupGraph(mChart.get(page),false,page);

}
}

有人知道我做错了吗?

Best,Flávio

0 个答案:

没有答案