gpt4 book ai didi

java - 如何使用抽屉式导航栏选择 View 页面

转载 作者:行者123 更新时间:2023-12-01 04:18:31 27 4
gpt4 key购买 nike

我对 Android 应用程序开发完全陌生,目前正在开发一个学校项目。

据我目前的了解,您可以使用抽屉导航来交换 UI fragment 。我根据此处给出的教程制作了一个到目前为止工作正常的抽屉导航:

https://developer.android.com/training/implementing-navigation/nav-drawer.html

我试图让它为选定的每个项目切换 View 。我创建了一个 View 寻呼机 fragment Activity ,我希望程序在选择抽屉导航中的第四个项目(索引 3)时用我的 View 寻呼机替换 View 。我在 fragment 类中添加了一个 switch 语句来确定要加载的布局。

这是抽屉导航的 Activity :

package com.mkge.mkg;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mTitle;
private String[] mPageTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTitle = getTitle();
mPageTitles = getResources().getStringArray(R.array.nav_draw_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPageTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.drawer_open);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {
selectItem(0);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) return true;
return super.onOptionsItemSelected(item);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}

private void selectItem(int position) {
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt(PageFragment.ARG_PAGE_NUMBER, position);
fragment.setArguments(args);

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

mDrawerList.setItemChecked(position, true);
setTitle(mPageTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}

public static class PageFragment extends Fragment {
public static final String ARG_PAGE_NUMBER = "page_number";

public PageFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int i = getArguments().getInt(ARG_PAGE_NUMBER);
View rootView;
switch(i) {
case 0:
rootView = inflater.inflate(R.layout.page_home, container, false);
break;
case 1:
rootView = inflater.inflate(R.layout.page_services, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.page_packages, container, false);
break;
case 3:
View view = new MeetViewPager().getViewPager();
rootView = view;
break;
case 4:
rootView = inflater.inflate(R.layout.page_about, container, false);
break;
case 5:
rootView = inflater.inflate(R.layout.page_contact, container, false);
break;
case 6:
rootView = inflater.inflate(R.layout.page_extras, container, false);
break;
default:
rootView = null;
}

String page = getResources().getStringArray(R.array.nav_draw_array)[i];
getActivity().setTitle(page);
return rootView;
}
}

@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}

我在这里创建了这个 fragment Activity 来创建 View 寻呼机。我尝试获取我在此处创建的 View 寻呼机对象,并将其传递给我的抽屉导航 Activity 的 switch 语句。

package com.mkge.mkg;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("ValidFragment")
public class MeetViewPager extends FragmentActivity {
public ViewPager mViewPager;
public FragmentPagerAdapter mViewPagerAdapter;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_meet);
List<Fragment> fragments = getFragments();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPagerAdapter = new mPageAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(mViewPagerAdapter);
}

public View getViewPager() {
return mViewPager;
}

private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
Fragment fragment = new mFragment();
Bundle args = new Bundle();
args.putInt("position", 0);
fragment.setArguments(args);
fList.add(fragment);

fragment = new mFragment();
args = new Bundle();
args.putInt("position", 1);
fragment.setArguments(args);
fList.add(fragment);

return fList;
}

private class mPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public mPageAdapter(FragmentManager f, List<Fragment> fragments) {
super(f);
this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}

@Override
public int getCount() {
return this.fragments.size();
}
}

@SuppressLint("ValidFragment")
public static class mFragment extends Fragment {

public mFragment() {
}

public static final mFragment newInstance(int i) {
mFragment f = new mFragment();
Bundle args = new Bundle(1);
args.putInt("position", i);
f.setArguments(args);
return f;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v;
int i = getArguments().getInt("postion");
switch(i) {
case 0:
v = inflater.inflate(R.layout.page_meet_0, container, false);
break;
case 1:
v = inflater.inflate(R.layout.page_meet_1, container, false);
break;
default:
v = null;
}
return v;
}
}
}

如有任何帮助,我们将不胜感激。

最佳答案

mViewPager.setCurrentItem(0); 

这应该是使 ViewPager 更改到第一个选项卡所需的全部内容。

这将从您的 selectItem() 方法内部调用。

关于java - 如何使用抽屉式导航栏选择 View 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212774/

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