喜(我是新來這個,所以你得原諒我)從父活動訪問選項卡片段方法?
我現在有一個標籤式的活動,其中MainActivity.java
是父類和Absences.java
和Notices.java
這些片段。我在MainActivity
裏面有一個日期選擇器片段,當我點擊一個選項菜單按鈕時,它會運行。
當我成功選擇我的日期時,我想將信息傳遞給Absences片段中的方法。但是,我真的很難實現這個部分。
我已經做了研究似乎在暗示這是解決方案:
Absences fragment= (Absences) getSupportFragmentManager().findFragmentById(R.id.Absences);
((Absences)fragment).updateAbsences();
然而,getSupportFragmentManager()
不能從靜態上下文引用,我似乎無法找到我的片段的id 。
在做了一些更多的研究之後,似乎發現這種方法不適用於標籤式活動,所以我現在處於虧損狀態。
有人能夠指出我在正確的方向嗎? - 我已經在這個工作了很長一段時間了!
SectionsPageAdapter類
Fragment one, two;
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
if(one==null){
one = new Absences();
}
return one;
case 1:
if(two==null){
two = new Notices();
}
return two;
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "ABSENCES";
case 1:
return "NOTICES";
}
return null;
}
}
onOptionsItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.calendar) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
return super.onOptionsItemSelected(item);
}
DatePickerFragment
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
dialog.setTitle("");
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Absences fragment= (Absences) getSupportFragmentManager().findFragmentById(R.id.Absences);
((Absences)fragment).updateAbsences();
}
}
- 編輯 -
更新內部MainActivity
public void onDatePicked() {
Absences fragment = (Absences) getSupportFragmentManager().findFragmentById(R.id.absences);
fragment.updateAbsences();
}
代碼按下面的建議
法新DatePickerFragment類
package com.alexwoohouse.heartofengland;
import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;
import java.util.Date;
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public interface OnDatePickedListener {
void onDatePicked(); //This method seems to be greyed out!
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
dialog.setTitle("");
return dialog;
}
@TargetApi(Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
}
}
你對「這是灰色」的評論 - 這是因爲你實際上並沒有使用界面。查看我鏈接到的文檔,它顯示瞭如何在附加到Activity時將接口的實例保存在片段中,然後在需要時調用它(在本例中,在您的'onDateSet'方法中) – dominicoder