import java.util.ArrayList; import java.util.List;java import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;android public class MainActivity extends Activity implements OnClickListener {app private List<View> list; private ViewPager viewpager; private Button button1; private Button button2; private Button button3;ide @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setData(); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button1 .setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); viewpager = (ViewPager) findViewById(R.id.viewpager); viewpager.setAdapter(new MyPagerAdapter(list)); viewpager.setCurrentItem(1); setButtonColor(viewpager.getCurrentItem()); viewpager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { setButtonColor(position); } 佈局 @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); }this private void setData() { list = new ArrayList<View>(); View view1=View.inflate(MainActivity.this,R.layout.viewpager_1, null); View view2=View.inflate(MainActivity.this,R.layout.viewpager_2,null); View view3=View.inflate(MainActivity.this,R.layout.viewpager_3,null); list.add(view1); list.add(view2); list.add(view3); } private void setButtonColor(int position) { // TODO Auto-generated method stub switch(position){ case 0: button1.setTextColor(Color.WHITE); button2.setTextColor(Color.BLACK); button3.setTextColor(Color.BLACK); break; case 1: button2.setTextColor(Color.WHITE); button1.setTextColor(Color.BLACK); button3.setTextColor(Color.BLACK); break; case 2: button3.setTextColor(Color.WHITE); button2.setTextColor(Color.BLACK); button1.setTextColor(Color.BLACK); break; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: viewpager.setCurrentItem(0); break; case R.id.button2: viewpager.setCurrentItem(1); break; case R.id.button3: viewpager.setCurrentItem(2); break; } }rem }get |