gpt4 book ai didi

java - 使用选项卡和 fragment 处理导航项

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:16 25 4
gpt4 key购买 nike

我创建了一个抽屉导航并添加了一个像这样的选项卡布局

activity_main

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_main

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:layout_gravity="bottom"
app:tabSelectedTextColor="#fffaf0"/>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

我得到的结果如下所示: enter image description here

但是,我尝试将 fragment 添加到选项卡,以便每个导航项打开一个屏幕,其中有两个选项卡“第一学期”和“第二学期”,其内容保存在 fragment xml 中,但不起作用。

主要 Activity

    package com.lini.bdevelopers.geomaticshandbook;

//import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

ViewPager viewPager;
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//SectionsPagerAdapter mSectionPagerAdapter =

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

viewPager = findViewById(R.id.viewpager);
//setupViewPager(viewPager);

tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

// Create adapter for the view pager
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstSemester(), "First Semester");
adapter.addFragment(new SecondSemester(), "Second Semester");
viewPager.setAdapter(adapter);
}
private void setupViewPager2(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstSemester2(), "First Semester");
adapter.addFragment(new SecondSemester2(), "Second Semester");
viewPager.setAdapter(adapter);
}
private void setupViewPager3(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstSemester3(), "First Semester");
adapter.addFragment(new SecondSemester3(), "Second Semester");
viewPager.setAdapter(adapter);
}
private void setupViewPager4(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstSemeste4r(), "First Semester");
adapter.addFragment(new SecondSemester4(), "Second Semester");
viewPager.setAdapter(adapter);
}
private void setupViewPager5(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstSemester5(), "First Semester");
adapter.addFragment(new SecondSemester5(), "Second Semester");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {

return mFragmentList.get(position);
}

@Override
public int getCount() {

return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {

return mFragmentTitleList.get(position);
}
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.year1) {
setupViewPager(viewPager);
} else if (id == R.id.year2) {
setupViewPager2(viewPager);
} else if (id == R.id.year3) {
setupViewPager3(viewPager);
} else if (id == R.id.year4) {
setupViewPager4(viewPager);
} else if (id == R.id.year5) {
setupViewPager5(viewPager);
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

上面的代码将 FirstSemester() 和 SecondSemester() fragment 绑定(bind)到所有导航项。这可能不是正确的方法,所以我需要建议和帮助,谢谢。

其次如何将 AppBar 标题更改为所选导航项的标题?

最佳答案

Secondly How do I change the AppBar title to the title of the Navigation item selected?

在 MainActivity 类的 onNavigationItemSelected() 方法中..您可以执行以下操作:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.year1) {
getSupportActionBar().setTitle("Year 1");
setupViewPager(viewPager);
} else if (id == R.id.year2) {
getSupportActionBar().setTitle("Year 2");
setupViewPager2(viewPager);
} else if (id == R.id.year3) {
getSupportActionBar().setTitle("Year 3");
setupViewPager3(viewPager);
} else if (id == R.id.year4) {
getSupportActionBar().setTitle("Year 4");
setupViewPager4(viewPager);
} else if (id == R.id.year5) {
getSupportActionBar().setTitle("Year 5");
setupViewPager5(viewPager);
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

关于你的第一个问题,你正在为抽屉导航中单击的任何项目显示一个 View 页面(第一和第二学期),因此你不应该期待一个看起来正常的代码。虽然方法比较粗暴,但是效率也不算太差。

我希望这会有所帮助......快乐编码!

关于java - 使用选项卡和 fragment 处理导航项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51343296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com