package com.example.qqviewpager; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { private ImageView mImageView1, mImageView2, mImageView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView1 = (ImageView) this.findViewById(R.id.iv_image1); mImageView2 = (ImageView) this.findViewById(R.id.iv_image2); mImageView3 = (ImageView) this.findViewById(R.id.iv_image3); mImageView1.setOnClickListener(this); mImageView2.setOnClickListener(this); mImageView3.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub // switch (v.getId()) { // case R.id.iv_image1: // // break; // case R.id.iv_image2: // // break; // case R.id.iv_image3: // // break; // // default: // break; // } Intent mIntent = new Intent(); mIntent.setClass(this, ViewpagerDemo.class); this.startActivity(mIntent); } }
//这个是重点
package com.example.qqviewpager; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { private ImageView mImageView1, mImageView2, mImageView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView1 = (ImageView) this.findViewById(R.id.iv_image1); mImageView2 = (ImageView) this.findViewById(R.id.iv_image2); mImageView3 = (ImageView) this.findViewById(R.id.iv_image3); mImageView1.setOnClickListener(this); mImageView2.setOnClickListener(this); mImageView3.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub // switch (v.getId()) { // case R.id.iv_image1: // // break; // case R.id.iv_image2: // // break; // case R.id.iv_image3: // // break; // // default: // break; // } Intent mIntent = new Intent(); mIntent.setClass(this, ViewpagerDemo.class); this.startActivity(mIntent); } }
//布局文件
<?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" > <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="测试有声资讯的viewpager" /> <LinearLayout android:layout_width="match_parent" android:layout_height="432dp" > <ImageView android:id="@+id/iv_image1" android:layout_width="48dp" android:layout_height="48dp" android:layout_margin="4dip" android:layout_weight="1.0" android:paddingLeft="4dip" android:scaleType="fitXY" android:src="@drawable/a" /> <ImageView android:id="@+id/iv_image2" android:layout_width="48dp" android:layout_height="48dp" android:layout_margin="4dip" android:layout_weight="1.0" android:scaleType="fitXY" android:src="@drawable/b" /> <ImageView android:id="@+id/iv_image3" android:layout_width="48dp" android:layout_height="48dp" android:layout_margin="4dip" android:layout_weight="1.0" android:paddingRight="4dip" android:scaleType="fitXY" android:src="@drawable/c" /> </LinearLayout> </LinearLayout>
//viewpager布局
<?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/vp_test" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>