簡體   English   中英

為什么片段沒有在導航抽屜中打開?

[英]Why is the fragment not opening in Navigation drawer?

我的 MainActivity.java 包含

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.calendar:
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_container,  new Calender());
                fragmentTransaction.commit();
                getSupportActionBar().setTitle("Calender");
                item.setChecked(true);
                break;
           // drawer.closeDrawers();
        }
        return true;
    }
});

我的 Calender.java 包含

public class Calender extends android.support.v4.app.Fragment{
    Activity a;
    CalendarView calendar;
    public Calender() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)  {
        // Inflate the layout for this fragment
        a = new Activity();
        a.setContentView(R.layout.fragment_calender);

        initializeCalendar();
        return inflater.inflate(R.layout.fragment_calender, container, false);

    }

    public void initializeCalendar() {

        calendar = (CalendarView) a.findViewById(R.id.calendar);
        // sets whether to show the week number.
        calendar.setShowWeekNumber(false);
        // sets the first day of week according to Calendar.
        // here we set Monday as the first day of the Calendar
        calendar.setFirstDayOfWeek(2);
        //The background color for the selected week.
        calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
        //sets the color for the dates of an unfocused month.
        calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
        //sets the color for the separator line between weeks.
        calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
        //sets the color for the vertical bar shown at the beginning and at the end of the selected date.
        calendar.setSelectedDateVerticalBar(R.color.darkgreen);
        //sets the listener to be notified upon selected date change.
        calendar.setOnDateChangeListener(new OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                Toast.makeText(a.getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });

    }
}

我的片段有問題嗎? 還是在主要活動中? 請盡快幫助。應用程序打開但我嘗試單擊日歷按鈕的地方,沒有顯示任何內容。

為什么你的片段中有一個活動實例? 將您的 onCreateView 更改為:

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.content_main, parent, false);
    calendar = (CalendarView) view.findViewById(R.id.calendar);
    return view;
}

然后在 onViewCreated 方法中執行以下操作:

initializeCalendar();

確保 content_main 中日歷視圖的 ID 是日歷。

如果使用Navigation Architecture Component ,則不必手動將片段連接到抽屜MenuItem。 The system will automatically tie you MenuItem to specific fragment如果menu/xyz_menu.xml中的MenuItem的ID與navigation/xyz_nav.xml中的目標的ID相匹配,則NavController可以導航至該目標。

請遵循官方文檔中提到的正確步驟: https : //developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer

注意:他們有一個示例應用程序鏈接到您可以參考的同一篇文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM