我正在使用MPAndroidChart来显示一些数据。我在不同的菜单选项上显示不同的图表。每个菜单选项都调用一个片段。饼图显示正常但导航切换不起作用。我在LogCat中没有收到任何错误。
以下是代码。
饼图片段XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chartPie"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
PieChartActivity.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
root = (RelativeLayout) inflater.inflate(R.layout.fragment_piechart, container, false);
getActivity().setContentView(R.layout.fragment_piechart);
mainLayout = (RelativeLayout) root.findViewById(
R.id.mainLayout);
mChart = (PieChart) getActivity().findViewById(R.id.chartPie);
// add pie chart to main layout
//mainLayout.addView(mChart);
//mainLayout.setBackgroundColor(Color.parseColor("#55656C"));
// configure pie chart
mChart.setUsePercentValues(true);
mChart.setDescription("OEE composition");
// enable hole and configure
mChart.setDrawHoleEnabled(true);
//mChart.setHoleColor(Color.BLACK);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);
// enable rotation of the chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);
// set a chart value selected listener
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex,
Highlight h) {
// display msg when value selected
if (e == null)
return;
Toast.makeText(PieChartActivity.this.getActivity(),
xData[e.getXIndex()] + " = " + e.getVal() + "%",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
});
// customize legends
Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7);
l.setYEntrySpace(5);
} catch (Exception ex) {
Log.d("Error", ex.getMessage());
}
// Quality
makeHTTPCall(objGlobal, "574c22cd7625423b05a2cbcc",
prod_Count_UBI_link_QLTY);
// Performance
makeHTTPCall(objGlobal, "574c22ad76254239d6a6bc56",
prod_Count_UBI_link_PERF);
// Availability
makeHTTPCall(objGlobal, "574c1e027625420f0e4401ff",
prod_Count_UBI_link_AVAIL);
return root;
}
PieChartActivity扩展片段
public class PieChartActivity extends Fragment implements
OnChartValueSelectedListener
有趣的是,导航切换在不同片段上的其他图表上运行良好。
如果需要更多详细信息,请与我们联系。 我是Android新手,告诉我在任何基础知识上是否错了......
答案 0 :(得分:1)
将饼图布局放置在@ layout / app_bar_index活动中。