我是JAVA和Android编程的初学者。这是我对应用程序结构的概念。
↑→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→↓
NavigationDrawer --------------------------------------------- Home --------------------------详情
(从所有ViewPager的实例中向左滑动) - (ViewPager Instance 1)-----(ViewPager Instance 2)
我使用Eclipse创建代码,但问题是我无法将NavicationDrawer的片段连接到Detail页面。它连接到作为原始来源的主页。
这是我的代码。
MainActivity.java
package com.example.ex_tabb;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity implements Main_NavigationDrawerFragment.NavigationDrawerCallbacks {
Main_PagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
ImageView homebtn;
private Main_NavigationDrawerFragment mNavigationDrawerFragment;
private Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new Main_PagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
homebtn = (ImageView) findViewById(R.id.btn_home);
mViewPager.setAdapter(mSectionsPagerAdapter);
mNavigationDrawerFragment = (Main_NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
homebtnOnClicklistener();
Button tempbt = (Button) findViewById(R.id.button1);
tempbt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (mViewPager.getCurrentItem()) {
case 0:
mViewPager.setCurrentItem(1);
break;
case 1:
mViewPager.setCurrentItem(2);
break;
case 2:
mViewPager.setCurrentItem(3);
break;
default:
break;
}
}
});
}
private void homebtnOnClicklistener() {
// TODO Auto-generated method stub
homebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(0);
}
});
}
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
default:
case 0:
fragment = new PlaceholderFragment();
break;
case 1:
fragment = new PlaceholderFragment2();
break;
case 2:
fragment = new PlaceholderFragment2();
break;
}
fragmentManager.beginTransaction().add(R.id.container, fragment).commit();
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment2 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment2 newInstance(int sectionNumber) {
PlaceholderFragment2 fragment = new PlaceholderFragment2();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Main_NavigationDrawerFragment.java
package com.example.ex_tabb;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main_NavigationDrawerFragment extends Fragment {
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
/**
* A pointer to the current callbacks instance (the Activity).
*/
private NavigationDrawerCallbacks mCallbacks;
/**
* Helper component that ties the action bar to the navigation drawer.
*/
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public Main_NavigationDrawerFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Read in the flag indicating whether or not the user has demonstrated
// awareness of the
// drawer. See PREF_USER_LEARNED_DRAWER for details.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
// Select either the default item (0) or the last selected item.
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Indicate that this fragment would like to influence the set of
// actions in the action bar.
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(
new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_activated_1,
android.R.id.text1, new String[] { getString(R.string.title_section1),
getString(R.string.title_section2), getString(R.string.title_section3), }));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
public static interface NavigationDrawerCallbacks {
/**
* Called when an item in the navigation drawer is selected.
*/
void onNavigationDrawerItemSelected(int position);
}
}
Main_PagerAdapter.java
package com.example.ex_tabb;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class Main_PagerAdapter extends FragmentPagerAdapter {
public Main_PagerAdapter(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:
return Fragment_mainpage.newInstance(0);
case 1:
return Fragment_Result_1.newInstance(1);
case 2:
return Fragment_Result_1.newInstance(2);
case 3:
return Fragment_Result_1.newInstance(3);
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.ex_tabb.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
style="@style/global_header"
android:layout_width="match_parent" >
<ImageButton
android:id="@+id/btn_home"
style="@style/global_header_button"
android:layout_centerInParent="true"
android:contentDescription="@string/__empty"
android:src="@drawable/selector_header_bt_home" />
</RelativeLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<com.example.ex_tabb.Main_ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ex_tabb.MainActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</com.example.ex_tabb.Main_ViewPager>
</LinearLayout>
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.example.ex_tabb.Main_NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
进步就在这里。我不确定一切都很好。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
**com.example.ex_tabb.MainActivity.mViewPager.setCurrentItem(1);
selectItem(position);**
}
});
mDrawerListView.setAdapter(
new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_activated_1,
android.R.id.text1, new String[] { getString(R.string.title_section1),
getString(R.string.title_section2), getString(R.string.title_section3), }));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}