您好,登錄后才能下訂單哦!
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Activity {
private List<View> listView = new ArrayList<View>();
private ViewPager viewPager;
private Button title1;
private Button title2;
private Button title3;
private Button title4;
private Button title5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initAdapter();
}
//加載控件
private void initView() {
title1 = (Button) findViewById(R.id.bt_title1);
title2 = (Button) findViewById(R.id.bt_title2);
title3 = (Button) findViewById(R.id.bt_title3);
title4 = (Button) findViewById(R.id.bt_title4);
title5 = (Button) findViewById(R.id.bt_title5);
}
public void clickTitle1(View v) {
//修改未選中按鈕背景顏色
title2.setBackgroundResource(R.drawable.kfa);
title3.setBackgroundResource(R.drawable.kfa);
title4.setBackgroundResource(R.drawable.kfa);
title5.setBackgroundResource(R.drawable.kfa);
//修改選中按鈕背景顏色
title1.setBackgroundResource(R.drawable.fasd);
//跳轉到對應界面
viewPager.setCurrentItem(0);
}
//Button點擊事件
public void clickTitle2(View v) {
title1.setBackgroundResource(R.drawable.kfa);
title3.setBackgroundResource(R.drawable.kfa);
title4.setBackgroundResource(R.drawable.kfa);
title5.setBackgroundResource(R.drawable.kfa);
title2.setBackgroundResource(R.drawable.fasd);
viewPager.setCurrentItem(1);
}
//Button點擊事件
public void clickTitle3(View v) {
title1.setBackgroundResource(R.drawable.kfa);
title2.setBackgroundResource(R.drawable.kfa);
title4.setBackgroundResource(R.drawable.kfa);
title5.setBackgroundResource(R.drawable.kfa);
title3.setBackgroundResource(R.drawable.fasd);
viewPager.setCurrentItem(2);
}
//Button點擊事件
public void clickTitle4(View v) {
title1.setBackgroundResource(R.drawable.kfa);
title2.setBackgroundResource(R.drawable.kfa);
title3.setBackgroundResource(R.drawable.kfa);
title5.setBackgroundResource(R.drawable.kfa);
title4.setBackgroundResource(R.drawable.fasd);
viewPager.setCurrentItem(3);
}
//Button點擊事件
public void clickTitle5(View v) {
title1.setBackgroundResource(R.drawable.kfa);
title2.setBackgroundResource(R.drawable.kfa);
title3.setBackgroundResource(R.drawable.kfa);
title4.setBackgroundResource(R.drawable.kfa);
title5.setBackgroundResource(R.drawable.fasd);
viewPager.setCurrentItem(4);
}
private void initAdapter() {
// 獲取頁面
LayoutInflater inflater = LayoutInflater.from(this);
View v1 = View.inflate(getApplicationContext(),
R.layout.activity_title1, null);
View v2 = View.inflate(getApplicationContext(),
R.layout.activity_title2, null);
View v3 = View.inflate(getApplicationContext(),
R.layout.activity_title3, null);
View v4 = View.inflate(getApplicationContext(),
R.layout.activity_title4, null);
View v5 = View.inflate(getApplicationContext(),
R.layout.activity_title5, null);
// 將頁面添加到集合中
listView.add(v1);
listView.add(v2);
listView.add(v3);
listView.add(v4);
listView.add(v5);
PagerAdapter pagerAdapter = new PagerAdapter() {
// PagerAdapter只緩存2個要顯示的tab,如果滑動的tab超出了緩存的范圍,就會調用這個方法,將tab銷毀
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
view.removeView(listView.get(position));
}
// 當要顯示的tab可以進行緩存的時候,會調用這個方法進行顯示圖片的初始化,我們將要顯示的tab加入到ViewGroup中,然后作為返回值返回即可
@Override
public Object instantiateItem(ViewGroup view, int position) {
view.addView(listView.get(position));
return listView.get(position);
}
// 獲取要滑動的控件的數量,在這里我們以tab為例,那么這里就應該是展示tab數量
@Override
public int getCount() {
return listView.size();
}
// 來判斷顯示的是否是同一tab,這里我們將兩個參數相比較返回即可
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
};
viewPager = (ViewPager) findViewById(R.id.vp_body);
viewPager.setAdapter(pagerAdapter);
/**
* 標題欄響應界面滑動事件
*/
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
title1.setBackgroundResource(R.drawable.kfa);
title2.setBackgroundResource(R.drawable.kfa);
title3.setBackgroundResource(R.drawable.kfa);
title4.setBackgroundResource(R.drawable.kfa);
title5.setBackgroundResource(R.drawable.kfa);
switch (viewPager.getCurrentItem()) {
case 0:
title1.setBackgroundResource(R.drawable.fasd);
break;
case 1:
title2.setBackgroundResource(R.drawable.fasd);
break;
case 2:
title3.setBackgroundResource(R.drawable.fasd);
break;
case 3:
title4.setBackgroundResource(R.drawable.fasd);
break;
case 4:
title5.setBackgroundResource(R.drawable.fasd);
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。