我正在开发一个应用程序,用户可以在其中输入血糖水平并将其显示在图表中。对于这个项目,我使用选项卡式导航,图表在Tab1上,输入在Tab2上。
我想这样做,以便当用户在Tab2上添加数据时,图表会自动更新Tab1。我会使用onStart()进行图形更新;功能,但我不能,因为碎片不会关闭/重新打开,除非我在选项卡式导航中转到Tab3,因为android保存了最后2个打开的碎片。
Tab1
public class tab1 extends Fragment {
DatabaseHelper myDb;
LineGraphSeries<DataPoint> series;
SQLiteDatabase sqLiteDatabase;
GraphView graph;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false);
}
public void onStart() {
super.onStart();
initControls();
initGraph();
}
private void initControls() {
myDb = new DatabaseHelper(getActivity());
sqLiteDatabase = myDb.getReadableDatabase();
}
public void initGraph() {
graph = getView().findViewById(R.id.graph);
series = new LineGraphSeries<DataPoint>(getData());
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setScrollable(true);
graph.getViewport().setScalable(true);
}
public void showMessage(String title, String Message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
public DataPoint[] getData() {
Cursor res = myDb.getAllData();
DataPoint[] dp = new DataPoint[res.getCount()];
if (res.getCount() == 0) {
showMessage("Error", "Nothing Found");
}
for (int i = 0; i < res.getCount(); i++) {
res.moveToNext();
dp[i] = new DataPoint(res.getInt(0), res.getFloat(1));
}
return dp;
}
}
标签2
public class tab2 extends Fragment {
DatabaseHelper myDb;
private tab1 tb1 = new tab1();
EditText editText_Glucose;
Button btn_Add;
//Time today = new Time(Time.getCurrentTimezone());
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2, container, false);
}
public void onStart(){
super.onStart();
initControls();
}
private void initControls() {
myDb = new DatabaseHelper(getActivity());
editText_Glucose = getView().findViewById(R.id.editText_Glucose);
btn_Add = getView().findViewById(R.id.btn_Add);
AddData();
}
public void AddData() {
btn_Add.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editText_Glucose.getText().toString());
if (isInserted)
showToast(getActivity(), "Data Inserted");
else
showToast(getActivity(), "Data not Inserted");
}
}
);
}
public static void showToast(Context mContext, String message){
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
为什么不为此使用TabLayout?
为此,请在主布局中添加:
<android.support.design.widget.TabLayout
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
然后在你的活动或片段中onViewCreated():
/* ... */
mTabs = (TabLayout) view.findViewById(android.R.id.tabs);
Tab tab1 = mTabs.newTab();
Tab tab2 = mTabs.newTab();
mTabs.addTab(tab1 , true);
mTabs.addTab(tab2 , false);
/* ... */
您可以查看本教程,了解有关TabLayout的更多详细信息: http://abhiandroid.com/materialdesign/tablayout-example-android-studio.html