我的项目有一个NavigationDrawer和一个TabLayout。我只想在特定标签的特定菜单中设置特定片段。
我的意思是,几乎每个菜单项都有3个不同的标签(不是特别相同),我想在每个菜单项的每个标签中设置一个不同的片段。
每个菜单项中的3个标签相同。
我该怎么办?我虽然关于setContentView(),但我不能让它正常工作。
以下是我的应用的截图:
Navigation Drawer with all menu items
这是我的MainActivity.java:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
findViewById(R.id.viewpager).setVisibility(View.GONE);
findViewById(R.id.sliding_tabs).setVisibility(View.GONE);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Pourquoi tu touches à ce bouton ?", Snackbar.LENGTH_SHORT)
.setAction("Action", null).show();
}
});
/*
Début Drawer Menu
*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
/*
Fin Drawer Menu
*/
}
@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.content_main) {
findViewById(R.id.viewpager).setVisibility(View.GONE);
findViewById(R.id.sliding_tabs).setVisibility(View.GONE);
} else if (id == R.id.nav_equations) {
findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
} else if (id == R.id.nav_lois) {
findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
} else if (id == R.id.nav_stats) {
findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
} else if (id == R.id.nav_tests) {
findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
} else if (id == R.id.nav_partager) {
} else if (id == R.id.nav_parametres) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.slabre.mathix/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.slabre.mathix/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
} }
这是SampleFragmentPagerAdapter.java,用于管理片段,但仅限于每个标签:
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Calcul", "Résultat", "Infos" };
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
所以PageFragment.java:
public class PageFragment extends Fragment{
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
最后,这是activity_main.xml包含的app_bar_main.xml:
<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"
android:fitsSystemWindows="true"
tools:context="com.example.slabre.mathix.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/sliding_tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<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"
android:src="@drawable/ic_add_black_24dp" />
</android.support.design.widget.CoordinatorLayout>
我真的不知道你是否需要更多文件,只要问我是否需要更多文件。
答案 0 :(得分:0)
如果我正确理解了这个问题,您只需要在ViewPager适配器中实现getItem(int position)
,以便为适配器中的每个可能位置返回不同的片段。目前它只返回1种类型的片段,但是作为参数传递位置,因此所有菜单项都显示&#34;片段#X&#34;。
您可以做的第一步是创建具有不同布局的不同Fragment类,并在getItem
方法中返回它们。
例如,复制整个PageFragment
类并创建一个PageTwoFragment
类。然后在PagerAdapter类中,您可以执行类似
@Override
public int getCount() {
return PAGE_COUNT; // TODO: Change how many pages
}
@Override
public Fragment getItem(int position) {
if (position == 1) {
return new PageTwoFragment(); // TODO: Implement
} else {
return PageFragment.newInstance(position);
}
// TODO: Return other Fragments for other positions
}
下一步是为NavigationView中的每个菜单项创建新的ViewPager Adapter类,并在onNavigationItemSelected
方法中重置ViewPager的适配器,如此
if (id == R.id.nav_equations) {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// TODO: Implement EquationsFragmentPagerAdapter
viewPager.setAdapter(new EquationsFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this));
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
注意:如果为ViewPager和TabLayout创建了字段变量,那么重复的代码就会少一些
这应该为每个菜单项提供不同数量的标签和各种片段。