您好,登錄后才能下訂單哦!
第一步
<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>
第二步建立三個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;//裝載已加載的界面對應的標題 private LayoutInflater inflater=null;//聲明一個動態布局對象 @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); //第一次被加載的布局對象 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) { } //當點擊當前頁面,還沒有產生側滑是 主要再次完成界面的加載 //在這里開啟線程去加載網絡的數據,更新ui的操作 @Override public void onPageScrollStateChanged(int i) { View view=inflater.inflate(R.layout.tab1,null); list.add(view); title.add("new view"); //刷新數據 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); } //從一個布局到另一個布局,銷毀前一個布局 @Override public void destroyItem(ViewGroup container, int position, Object object) { //super.destroyItem(container, position, object); ((ViewPager)container).removeView(list.get(position)); } //得到每一個界面的布局 @Override public CharSequence getPageTitle(int position) { return title.get(position); } @Override public boolean isViewFromObject(View view, Object o) { return view==o;//判斷當前view和Object是否是同一個對象 } } @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); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。