当前位置: 代码迷 >> Android >> 第八十七讲:Android之ViewPager的用法(2)
  详细解决方案

第八十七讲:Android之ViewPager的用法(2)

热度:18   发布时间:2016-04-28 02:01:10.0
第八十七讲:Android之ViewPager的用法(二)

过错是暂时的遗憾,而错过则是永远的遗憾!


本讲内容:ViewPager实现引导界面以及进入下一个activity的办法


示例一

              


下面是res/layout/activity_main.xml 布局文件:   

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >	<android.support.v4.view.ViewPager	    android:id="@+id/guide_view"	    android:layout_width="match_parent"	    android:layout_height="match_parent"/></LinearLayout>

下面是res/layout/start.xml 布局文件:  

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/guide_item"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="5" />    <Button        android:id="@+id/start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="开始使用"        android:textSize="20sp"        android:visibility="gone" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1" /><span style="font-size:18px;"></LinearLayout></span>

android:visibility="gone"此属性意思是此视图是否显示

其有三个属性:visible显示;invisible显示黑背景条;gone不显示


下面是res/layout/other.xml 布局文件:   

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/guide_item"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="5" />    <Button        android:id="@+id/start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="开始使用"        android:textSize="20sp"        android:visibility="gone" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1" /></LinearLayout>

下面是OtherActivity.java界面文件:(要注册)

public class OtherActivity extends Activity{	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.other);	}}

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity {      private ViewPager viewPager;      private List<View> mImageViews; // 滑动的图片集合      private int[] imageResId; // 图片ID      private int currentItem = 0; // 当前图片的索引号      private GestureDetector gestureDetector; // 用户滑动      private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          gestureDetector = new GestureDetector(new GuideViewTouch());            // 获取分辨率          DisplayMetrics dm = new DisplayMetrics();          getWindowManager().getDefaultDisplay().getMetrics(dm);          flaggingWidth = dm.widthPixels / 3;            imageResId = new int[] { R.drawable.guide_1, R.drawable.guide_2 };            mImageViews = new ArrayList<View>();            // 初始化图片资源          LayoutInflater viewInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);          // 0          View convertView0 = viewInflater.inflate(R.layout.start, null);          LinearLayout linearLayout0 = (LinearLayout) convertView0.findViewById(R.id.guide_item);          linearLayout0.setBackgroundResource(imageResId[0]);          mImageViews.add(linearLayout0);          // 1          View convertView1 = viewInflater.inflate(R.layout.start, null);          LinearLayout linearLayout1 = (LinearLayout) convertView1  .findViewById(R.id.guide_item);          linearLayout1.setBackgroundResource(imageResId[1]);          Button btn = (Button) convertView1.findViewById(R.id.start);          btn.setVisibility(View.VISIBLE);          btn.setOnClickListener(new OnClickListener() {                public void onClick(View v) {                  GoToMainActivity();              }          });          mImageViews.add(linearLayout1);            viewPager = (ViewPager) findViewById(R.id.guide_view);          viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器          // 设置一个监听器,当ViewPager中的页面改变时调用          viewPager.setOnPageChangeListener(new MyPageChangeListener());      }        @Override      public boolean dispatchTouchEvent(MotionEvent event) {          if (gestureDetector.onTouchEvent(event)) {              event.setAction(MotionEvent.ACTION_CANCEL);          }          return super.dispatchTouchEvent(event);      }        private class GuideViewTouch extends SimpleOnGestureListener {          @Override          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                  float velocityY) {              if (currentItem == 1) {                  if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()                          - e2.getY())                          && (e1.getX() - e2.getX() <= (-flaggingWidth) || e1                                  .getX() - e2.getX() >= flaggingWidth)) {                      if (e1.getX() - e2.getX() >= flaggingWidth) {                          GoToMainActivity();                          return true;                      }                  }              }              return false;          }      }        //进入主界面     void GoToMainActivity() {          Intent i = new Intent(MainActivity.this, OtherActivity.class);          startActivity(i);          finish();      }         // 当ViewPager中页面的状态发生改变时调用     private class MyPageChangeListener implements OnPageChangeListener {            public void onPageSelected(int position) {              currentItem = position;          }            public void onPageScrollStateChanged(int arg0) {          }            public void onPageScrolled(int arg0, float arg1, int arg2) {          }      }        /**      * 填充ViewPager页面的适配器      *       * @author Administrator      *       */      private class MyAdapter extends PagerAdapter {            @Override          public int getCount() {              return imageResId.length;          }            @Override          public Object instantiateItem(View arg0, int arg1) {              ((ViewPager) arg0).addView(mImageViews.get(arg1));              return mImageViews.get(arg1);          }            @Override          public void destroyItem(View arg0, int arg1, Object arg2) {              ((ViewPager) arg0).removeView((View) arg2);          }            @Override          public boolean isViewFromObject(View arg0, Object arg1) {              return arg0 == arg1;          }            @Override          public void restoreState(Parcelable arg0, ClassLoader arg1) {            }            @Override          public Parcelable saveState() {              return null;          }            @Override          public void startUpdate(View arg0) {            }            @Override          public void finishUpdate(View arg0) {            }      }        @Override      public boolean onKeyDown(int keyCode, KeyEvent event) {          if (keyCode == KeyEvent.KEYCODE_BACK) {              GoToMainActivity();              return false;          }          return super.onKeyDown(keyCode, event);      }    } 

Take your time and enjoy it


  相关解决方案