我是安卓新手。。
我按照本教程了解如何将 viewpager 实现为图像幻灯片库滑动库。现在,我也想实现相同的图像滑动fragment
。我不知道该怎么做。因为它跟在 Fragment 之后,所以还有另一个函数叫做onCreateView
. 不知道下一步怎么走!
MainActivity.java(在列表视图中实现了具有 2 个项目的导航抽屉)
package com.example.sidemenutabstutorial;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
String[] subtitle;
int[] icon;
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_main);
// Generate title
title = new String[] { "Title Fragment 1", "Title Fragment 2"};
// Generate subtitle
subtitle = new String[] { "Subtitle Fragment 1", "Subtitle Fragment 2"};
// Generate icon
icon = new int[] { R.drawable.action_about, R.drawable.action_settings};
// Locate DrawerLayout in drawer_main.xml
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Locate ListView in drawer_main.xml
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Pass results to MenuListAdapter Class
mMenuAdapter = new MenuListAdapter(this, title, subtitle, icon);
// Set the MenuListAdapter to the ListView
mDrawerList.setAdapter(mMenuAdapter);
// Capture button clicks on side menu
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
// The click listener for ListView in the navigation drawer
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) {
android.support.v4.app.FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.content_frame, fragment1);
break;
case 1:
ft.replace(R.id.content_frame, fragment2);
break;
}
ft.commit();
mDrawerList.setItemChecked(position, true);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
片段1.java
public class Fragment1 extends SherlockFragment {
ViewPager mViewPager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
mViewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(getSupportFragmentManager());
mViewPager.setAdapter(adapter);
return rootView;
}
}
ImageAdapter.java
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
public ImageAdapter(Fragment1 fragment1) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
在 Logcat 中:
10-24 17:15:24.545: D/AndroidRuntime(791): Shutting down VM
10-24 17:15:24.555: W/dalvikvm(791): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-24 17:15:24.595: E/AndroidRuntime(791): FATAL EXCEPTION: main
10-24 17:15:24.595: E/AndroidRuntime(791): java.lang.NullPointerException
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.<init>(View.java:3236)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.ImageView.<init>(ImageView.java:105)
10-24 17:15:24.595: E/AndroidRuntime(791): at com.example.sidemenutabstutorial.Fragment1$ImageAdapter.instantiateItem(Fragment1.java:113)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:639)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 17:15:24.595: E/AndroidRuntime(791): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.View.measure(View.java:15518)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.os.Handler.handleCallback(Handler.java:725)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:137)
10-24 17:15:24.595: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-24 17:15:24.595: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 17:15:24.595: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 17:15:24.595: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-24 17:15:24.595: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
请帮忙!
提前致谢。