您好,登錄后才能下訂單哦!
第一步
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > <android.support.v4.view.PagerTitleStrip android:id="@+id/PagerTitleStrip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" > </android.support.v4.view.PagerTitleStrip> </android.support.v4.view.ViewPager> </RelativeLayout>
第二步建立三個(gè)tab頁,用最簡單的線性布局即可。
第三部
package xiaocool.net.viewpager2; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerTitleStrip; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; public class MainActivity extends ActionBarActivity { private ViewPager viewPager; private PagerTitleStrip pagerTitleStrip; private MyAdapter adapter=null; private List<View> list=null; //裝載已加載的界面 private List<String> title=null;//裝載已加載的界面對(duì)應(yīng)的標(biāo)題 private LayoutInflater inflater=null;//聲明一個(gè)動(dòng)態(tài)布局對(duì)象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager=(ViewPager)this.findViewById(R.id.ViewPager); pagerTitleStrip=(PagerTitleStrip)this.findViewById(R.id.PagerTitleStrip); adapter=new MyAdapter(); //加載布局 inflater= LayoutInflater.from(MainActivity.this); //第一次被加載的布局對(duì)象 View tab=inflater.inflate(R.layout.tab1,null); list=new ArrayList<View>(); list.add(tab); title=new ArrayList<String>(); title.add("title"); //先初始化頁面 viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { } //當(dāng)點(diǎn)擊當(dāng)前頁面,還沒有產(chǎn)生側(cè)滑是 主要再次完成界面的加載 //在這里開啟線程去加載網(wǎng)絡(luò)的數(shù)據(jù),更新ui的操作 @Override public void onPageScrollStateChanged(int i) { View view=inflater.inflate(R.layout.tab1,null); list.add(view); title.add("new view"); //刷新數(shù)據(jù) adapter.notifyDataSetChanged(); } }); } //布局適配器 public class MyAdapter extends PagerAdapter{ @Override public int getCount() { return list.size(); } //初始化適配器 @Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager)container).addView(list.get(position)); return list.get(position); } //從一個(gè)布局到另一個(gè)布局,銷毀前一個(gè)布局 @Override public void destroyItem(ViewGroup container, int position, Object object) { //super.destroyItem(container, position, object); ((ViewPager)container).removeView(list.get(position)); } //得到每一個(gè)界面的布局 @Override public CharSequence getPageTitle(int position) { return title.get(position); } @Override public boolean isViewFromObject(View view, Object o) { return view==o;//判斷當(dāng)前view和Object是否是同一個(gè)對(duì)象 } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。