package com.wxw.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.wxw.adpter.MyPagerAdapter;
public class TabHostDemoActivity extends Activity {
/** Called when the activity is first created. */
private ViewPager myViewPager;
private MyPagerAdapter myAdapter;
private LayoutInflater mInflater;
private List<View> listViews;
private EditText editText1, editText2, editText3;
private View layout1 = null;
private View layout2 = null;
private View layout3 = null;
private View viewElement = null;
private TextView textView1, textView2, textView3;
private RelativeLayout relativeLayout1, relativeLayout2, relativeLayout3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getViewById();
relativeLayout1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setImageBackground(0);
myViewPager.setCurrentItem(0);
}
});
relativeLayout2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setImageBackground(1);
myViewPager.setCurrentItem(1);
}
});
relativeLayout3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setImageBackground(2);
myViewPager.setCurrentItem(2);
}
});
listViews = new ArrayList<View>();
mInflater = getLayoutInflater();
layout1 = mInflater.inflate(R.layout.help1, null);
layout2 = mInflater.inflate(R.layout.help2, null);
layout3 = mInflater.inflate(R.layout.help3, null);
listViews.add(layout1);
listViews.add(layout2);
listViews.add(layout3);
myAdapter = new MyPagerAdapter(listViews);
myViewPager.setAdapter(myAdapter);
myViewPager.setCurrentItem(0);
/**
* viewPage页面改變监听器
*/
myViewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
viewElement = listViews.get(arg0);
// activity从1到2滑动,2被加载后掉用此方法
switch (arg0) {
case 0:
editText1 = (EditText) findViewById(R.id.one);
editText1.setText("常规查询");
setImageBackground(0);
break;
case 1:
editText2 = (EditText) findViewById(R.id.two);
editText2.setText("周边查询");
setImageBackground(1);
break;
case 2:
editText3 = (EditText) findViewById(R.id.three);
editText3.setText("快速通道");
setImageBackground(2);
break;
default:
break;
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// 从1到2滑动,在1滑动前调用
}
public void onPageScrollStateChanged(int arg0) {
Log.d("k", "onPageScrollStateChanged - " + arg0);
// 状态有三个0空闲,1是增在滑行中,2目标加载完毕
}
});
}
/**
* 通过id获取控件
*/
private void getViewById() {
// 顶部的三个tab标签
relativeLayout1 = (RelativeLayout) findViewById(R.id.rmml1);
relativeLayout2 = (RelativeLayout) findViewById(R.id.rmml2);
relativeLayout3 = (RelativeLayout) findViewById(R.id.rmml3);
textView1 = (TextView) relativeLayout1.findViewById(R.id.textView1);
textView2 = (TextView) relativeLayout2.findViewById(R.id.textView2);
textView3 = (TextView) relativeLayout3.findViewById(R.id.textView3);
myViewPager = (ViewPager) findViewById(R.id.myMesPager);
}
private void setImageBackground(int position) {
switch (position) {
case 0:
relativeLayout1.setBackgroundResource(R.drawable.focus);
relativeLayout2.setBackgroundResource(R.drawable.no_focus);
relativeLayout3.setBackgroundResource(R.drawable.no_focus);
textView1.setTextColor(Color.WHITE);
textView2.setTextColor(Color.GRAY);
textView3.setTextColor(Color.GRAY);
break;
case 1:
relativeLayout1.setBackgroundResource(R.drawable.no_focus);
relativeLayout2.setBackgroundResource(R.drawable.focus);
relativeLayout3.setBackgroundResource(R.drawable.no_focus);
textView1.setTextColor(Color.GRAY);
textView2.setTextColor(Color.WHITE);
textView3.setTextColor(Color.GRAY);
break;
case 2:
relativeLayout1.setBackgroundResource(R.drawable.no_focus);
relativeLayout2.setBackgroundResource(R.drawable.no_focus);
relativeLayout3.setBackgroundResource(R.drawable.focus);
textView1.setTextColor(Color.GRAY);
textView2.setTextColor(Color.GRAY);
textView3.setTextColor(Color.WHITE);
break;
default:
break;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页