我正在尝试为我的应用程序创建一个包含四个窗格的开放教程,但由于我是
Android的新手,我想确保在将此任务标记为“完成”之前我正在考虑所有选项.我知道三种方式,我只能真正完成一种方式.本教程没有任何要求,但是一些“想要的功能”将是每个窗格的滑动动作,以及图像和底部(导航圆圈)不移动且顶部的标题不移动.
单程
4个独立的活动和4个独立的布局
红色圆圈项目是水平居中并从顶部推出的textView.
白色圆圈项目是水平和垂直居中的imageViews.
紫色圆圈是imageViews水平居中并从底部推出.
第二种方式
一项活动有4个片段
片段很难学,但我读的越多/看到它们的教程,它们似乎只能用于平板电脑.这是实现这一目标的有效方法吗?
第三种方式
ViewPager?
http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html
我之前从未使用过这个,但我知道这是一个选择.
最后的问题
更常使用哪种方式/实现此方法的正确方法是什么?有没有办法让中间部分(图像)滑入,但标题(顶部)和导航图像(底部)只是在图像滑入后才会改变?
解决方法
以下是使用ViewPager方式执行的一些代码(这正是你如何做到的)
package com.test.viewpager; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class WalkthroughActivity extends Activity { private static final int MAX_VIEWS = 5; ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.walkthrough_activity); mViewPager = (ViewPager) findViewById(R.id.view_pager); mViewPager.setAdapter(new WalkthroughPagerAdapter()); mViewPager.setOnPageChangeListener(new WalkthroughPageChangeListener()); } class WalkthroughPagerAdapter extends PagerAdapter { @Override public int getCount() { return MAX_VIEWS; } @Override public boolean isViewFromObject(View view,Object object) { return view == (View) object; } @Override public Object instantiateItem(View container,int position) { Log.e("walkthrough","instantiateItem(" + position + ");"); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View imageViewContainer = inflater.inflate(R.layout.walkthrough_single_view,null); ImageView imageView = (ImageView) imageViewContainer.findViewById(R.id.image_view); switch(position) { case 0: imageView.setImageResource(R.drawable.image1); break; case 1: imageView.setImageResource(R.drawable.image2); break; case 2: imageView.setImageResource(R.drawable.image3); break; case 3: imageView.setImageResource(R.drawable.image4); break; case 4: imageView.setImageResource(R.drawable.image5); break; } ((ViewPager) container).addView(imageViewContainer,0); return imageViewContainer; } @Override public void destroyItem(ViewGroup container,int position,Object object) { ((ViewPager)container).removeView((View)object); } } class WalkthroughPageChangeListener implements ViewPager.OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0,float arg1,int arg2) { } @Override public void onPageSelected(int position) { // Here is where you should show change the view of page indicator switch(position) { case MAX_VIEWS - 1: break; default: } } } }
这是我的walkthrough_activity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/screen_navigation_button" /> <!-- This TextView will not swipe when you swipe in the ViewPager --> <TextView android:id="@id/screen_navigation_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" android:padding="10dip" android:text="Hello,Walkthrough!" android:textSize="18sp" /> </RelativeLayout>
而walkthrough_single_view.xml就像这样简单 –
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image_view" android:layout_width="fill_parent" android:layout_height="fill_parent" />
希望有帮助:)