MainActivity:java
簡單的三個button
android
public class MainActivity extends Activity implements View.OnClickListener { Button btn_fragment_viewpager; Button btn_fragment; Button btn_viewpager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); } /** * 初始化事件 */ private void initEvent() { btn_viewpager.setOnClickListener(this); btn_fragment.setOnClickListener(this); btn_fragment_viewpager.setOnClickListener(this); } /** * 初始化佈局 */ private void initView() { btn_fragment_viewpager = (Button)findViewById(R.id.btn_fragment_viewpager); btn_fragment = (Button)findViewById(R.id.btn_fragment); btn_viewpager = (Button)findViewById(R.id.btn_viewpager); } /** * 點擊 * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_viewpager: startActivity(new Intent(this, MyViewPagerActivity.class)); break; case R.id.btn_fragment: startActivity(new Intent(this, MyFragmentActivity.class)); break; case R.id.btn_fragment_viewpager: startActivity(new Intent(this, MyFragmentViewPagerActivity.class)); break; } } }
mainActivity效果:app
MyViewPagerActivity:ide
package com.hong.testfragmentandpager; import android.app.Activity; import android.graphics.Color; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.hong.testfragmentandpager.pages.Page1; import com.hong.testfragmentandpager.pages.Page2; import java.util.ArrayList; public class MyViewPagerActivity extends Activity implements View.OnClickListener { TextView tv_first;//底部tab TextView tv_second; TextView tv_third; ViewPager viewpager; private MyPagerAdapter adapter; ArrayList<Object> pagers;//pager的集合 private MyOnPageChangeListener myOnPageChangeListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager); initView();//初始化佈局 initData();//初始化數據 initEvent();//添加事件 } /** * 初始化數據 */ private void initData() { tv_first.setTextColor(Color.RED);//使頁面進入時就讓tab爲選中狀態 addPagerData();//給viewpager添加數據(TextView)內容 adapter = new MyPagerAdapter();//設置適配器 myOnPageChangeListener = new MyOnPageChangeListener(); } /** * 給pager添加data; */ private void addPagerData() { pagers = new ArrayList<Object>(); TextView pager3 = new TextView(this); pager3.setText("第三頁"); Page1 pager1 = new Page1(this); Page2 pager2 = new Page2(this); pagers.add(pager1); pagers.add(pager2); pagers.add(pager3); } /** * 添加事件 */ private void initEvent() { tv_first.setOnClickListener(this); tv_second.setOnClickListener(this); tv_third.setOnClickListener(this); viewpager.setAdapter(adapter); viewpager.addOnPageChangeListener(myOnPageChangeListener); } /** * 初始化佈局 */ private void initView() { viewpager = (ViewPager)findViewById(R.id.viewpager); tv_first = (TextView) findViewById(R.id.tv_first); tv_second = (TextView) findViewById(R.id.tv_second); tv_third = (TextView) findViewById(R.id.tv_third); } @Override public void onClick(View v) { removeTextColor();//點擊tab時從新設置tab標題顏色 switch (v.getId()) { case R.id.tv_first: //顯示第一頁 viewpager.setCurrentItem(0); tv_first.setTextColor(Color.RED); break; case R.id. tv_second: //顯示第二頁 viewpager.setCurrentItem(1); tv_second.setTextColor(Color.GRAY); break; case R.id.tv_third: //顯示第三頁 viewpager.setCurrentItem(2); tv_third.setTextColor(Color.BLUE); break; } } /** * 改變頁面時先把tab樣式初始化 */ private void removeTextColor() { tv_first.setTextColor(Color.parseColor("#000000")); tv_second.setTextColor(Color.parseColor("#000000")); tv_third.setTextColor(Color.parseColor("#000000")); } /** * 給viewpager設定數據的適配器 */ private class MyPagerAdapter extends PagerAdapter { @Override public Object instantiateItem(ViewGroup container, int position) { if (position == 0) {//判斷不一樣的頁面進行加載相應的頁面: Page1 page1 = (Page1) pagers.get(position); container.addView(page1.rootView); return page1.rootView; }else if (position == 1) { Page2 page2 = (Page2) pagers.get(position); container.addView(page2.rootView); return page2.rootView; }else{ TextView view = (TextView) pagers.get(position); container.addView(view); return pagers.get(position); } } @Override public void destroyItem(ViewGroup container, int position, Object object) { if (position == 0) {//一樣判斷不一樣的view 進行相應的操做 Page1 page1 = (Page1) pagers.get(position); container.removeView(page1.rootView); }else if (position == 1) { Page2 page2 = (Page2) pagers.get(position); container.removeView(page2.rootView); }else{ container.removeView((View) pagers.get(position)); } } @Override public int getCount() { return pagers.size(); } @Override public boolean isViewFromObject(View view, Object object) { return object == view; } } /** * 給viewpager設置監聽, 當item被選中後相應的tab也要改變 */ private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //position = currentPage; removeTextColor(); switch (position) { case 0: tv_first.setTextColor(Color.RED); break; case 1: tv_second.setTextColor(Color.RED); break; case 2: tv_third.setTextColor(Color.RED); break; } } @Override public void onPageScrollStateChanged(int state) { } } }
MainFragmentActivity佈局
package com.hong.testfragmentandpager; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import com.hong.testfragmentandpager.fragment.Fragment1; import com.hong.testfragmentandpager.fragment.Fragment2; import com.hong.testfragmentandpager.fragment.Fragment3; import java.util.ArrayList; public class MyFragmentActivity extends FragmentActivity implements View.OnClickListener { LinearLayout ll_content; TextView tv_first;//底部tab TextView tv_second; TextView tv_third; ArrayList<Fragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); initView();//初始化佈局 initData(); initEvent();//添加事件 } private void initData() { fragments = new ArrayList<>(); fragments.add(new Fragment1(this)); fragments.add(new Fragment2(this)); fragments.add(new Fragment3(this)); //給ll_content添加fragment selectFragment(0); } /** * 選擇fragment頁面 * * @param i */ private void selectFragment(int i) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); if (fragments.get(i) != null) { transaction.replace(R.id.ll_content,fragments.get(i)); } else { transaction.add(R.id.ll_content, fragments.get(i)); } transaction.commit(); } private void initEvent() { tv_first.setOnClickListener(this); tv_second.setOnClickListener(this); tv_third.setOnClickListener(this); } /** * 初始化佈局 */ private void initView() { tv_first = (TextView) findViewById(R.id.tv_first); tv_second = (TextView) findViewById(R.id.tv_second); tv_third = (TextView) findViewById(R.id.tv_third); ll_content = (LinearLayout) findViewById(R.id.ll_content); tv_first.setTextColor(Color.RED); } @Override public void onClick(View v) { removeTextColor(); switch (v.getId()) { case R.id.tv_first: tv_first.setTextColor(Color.RED); selectFragment(0); break; case R.id.tv_second: tv_second.setTextColor(Color.RED); selectFragment(1); break; case R.id.tv_third: tv_third.setTextColor(Color.RED); selectFragment(2); break; default: break; } } /** * 改變頁面時先把tab樣式初始化 */ private void removeTextColor() { tv_first.setTextColor(Color.parseColor("#000000")); tv_second.setTextColor(Color.parseColor("#000000")); tv_third.setTextColor(Color.parseColor("#000000")); } }
MyFragmentViewPagerActivity
this
package com.hong.testfragmentandpager; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; import com.hong.testfragmentandpager.fragment.Fragment1; import com.hong.testfragmentandpager.fragment.Fragment2; import com.hong.testfragmentandpager.fragment.Fragment3; import java.util.ArrayList; public class MyFragmentViewPagerActivity extends FragmentActivity implements View.OnClickListener { FrameLayout fl_content; ViewPager viewPager; TextView tv_first;//底部tab TextView tv_second; TextView tv_third; FragmentPagerAdapter adapter; ArrayList<Fragment> fragments; private MyPagerListener myOnPageChangeListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_view_pager); initView(); initData(); initEvent(); } /** * 初始化數據 */ private void initData() { fragments = new ArrayList<>(); fragments.add(new Fragment1(this)); fragments.add(new Fragment2(this)); fragments.add(new Fragment3(this)); tv_first.setTextColor(Color.RED); viewPager.setCurrentItem(1); adapter = new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }; viewPager.setAdapter(adapter); } /** * 增長事件 */ private void initEvent() { tv_first.setOnClickListener(this); tv_second.setOnClickListener(this); tv_third.setOnClickListener(this); myOnPageChangeListener = new MyPagerListener(); viewPager.addOnPageChangeListener(myOnPageChangeListener); } /** * 初始化佈局 */ private void initView() { fl_content = (FrameLayout) findViewById(R.id.fl_content); viewPager = (ViewPager) findViewById(R.id.viewpager); tv_first = (TextView) findViewById(R.id.tv_first); tv_second = (TextView) findViewById(R.id.tv_second); tv_third = (TextView) findViewById(R.id.tv_third); } @Override public void onClick(View v) { removeTextColor(); switch (v.getId()) { case R.id.tv_first: tv_first.setTextColor(Color.RED); viewPager.setCurrentItem(0); break; case R.id.tv_second: tv_second.setTextColor(Color.RED); viewPager.setCurrentItem(1); break; case R.id.tv_third: tv_third.setTextColor(Color.RED); viewPager.setCurrentItem(2); break; default: break; } } /** * 改變頁面時先把tab樣式初始化 */ private void removeTextColor() { tv_first.setTextColor(Color.parseColor("#000000")); tv_second.setTextColor(Color.parseColor("#000000")); tv_third.setTextColor(Color.parseColor("#000000")); } private class MyPagerListener implements ViewPager.OnPageChangeListener{ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { removeTextColor(); switch (position) { case 0: tv_first.setTextColor(Color.RED); break; case 1: tv_second.setTextColor(Color.RED); break; case 2: tv_third.setTextColor(Color.RED); break; } } @Override public void onPageScrollStateChanged(int state) { } } }
第一頁是 listview.net
第二頁是viewpagercode
第三頁是textview事件
固然代碼知識貼了第一階段,還有裏面的嵌套,還有嵌套的嵌套都沒有貼出來,忽然以爲好麻煩。哎。。。rem
下載地址:http://download.csdn.net/download/qq_33363534/9466845