ViewPager中t常見tab例子2


效果圖示例:java







===============================android





一、res/layout中activity_main.xml佈局數組


代碼ide



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"佈局

    xmlns:tools="http://schemas.android.com/tools"this

    android:layout_width="match_parent"spa

    android:layout_height="match_parent"xml

    android:orientation="vertical" >事件


    <LinearLayout get

        android:id="@+id/linearLayout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#000"

        android:gravity="center"

        android:orientation="horizontal">

        

    <TextView

        android:id="@+id/text_info"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="wrap_content"

        android:textSize="25sp"

        android:gravity="center"

        android:textColor="#fff"

        android:text="消息" />

    

    <TextView

        android:id="@+id/text_friend"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="wrap_content"

        android:textSize="25sp"

        android:gravity="center"

         android:textColor="#fff"

        android:text="好友" />

    

    <TextView

        android:id="@+id/text_group"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="wrap_content"

        android:textSize="25sp"

        android:gravity="center"

         android:textColor="#fff"

        android:text="羣組" />

    

    <TextView

        android:id="@+id/text_dynamic"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:layout_height="wrap_content"

        android:textSize="25sp"

        android:gravity="center"

         android:textColor="#fff"

        android:text="動態" />

    

    </LinearLayout>

    

    <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        />


</LinearLayout>




========================



二、源文件2個一個Fragment 一個MainActivity



MainActivity類


代碼


public class MainActivity extends FragmentActivity {


private ViewPager viewPager;

private LinearLayout linearLayout;

private MyFragmentPagerAdapter adapter;

private List<Fragment> list;

private TextView[] arrText;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        initTabs();//初始化tab方法

        initViewPager();//初始化viewPager方法

    }

    

    private void initTabs() {

this.linearLayout = (LinearLayout) this.findViewById(R.id.linearLayout);

arrText = new TextView[linearLayout.getChildCount()];//有多少個TextView就new多少個

for(int i = 0;i<arrText.length;i++){

//在這給tab中的每一個TextView設置須要的屬性

arrText[i] = (TextView) linearLayout.getChildAt(i);//把TextView放到數組裏方便在其餘地方操做

//arrText[i].setEnabled(true);

arrText[i].setBackgroundColor(Color.GRAY);//所有初始化爲灰色

arrText[i].setTag(i);//設置標記 方便在其餘地方操做相應的TextView

//arrText點擊事件監聽

arrText[i].setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//在這裏用到上面設置的 標誌

viewPager.setCurrentItem((Integer) v.getTag());

}

});

}

//arrText[0].setEnabled(false);

//設置默認的TextView

arrText[0].setBackgroundColor(Color.CYAN);//青色

}


private void initViewPager() {

this.viewPager = (ViewPager) this.findViewById(R.id.viewPager);

list = new ArrayList<Fragment>();

//碎片的建立

Tabs_Fragment fragment1 = new Tabs_Fragment();

Bundle bundle1 = new Bundle();

bundle1.putInt("tabIndex", 1);

fragment1.setArguments(bundle1);

Tabs_Fragment fragment2 = new Tabs_Fragment();

Bundle bundle2 = new Bundle();

bundle2.putInt("tabIndex", 2);

fragment2.setArguments(bundle2);

Tabs_Fragment fragment3 = new Tabs_Fragment();

Bundle bundle3 = new Bundle();

bundle3.putInt("tabIndex", 3);

fragment3.setArguments(bundle3);

Tabs_Fragment fragment4 = new Tabs_Fragment();

Bundle bundle4 = new Bundle();

bundle4.putInt("tabIndex", 4);

fragment4.setArguments(bundle4);

list.add(fragment1);

list.add(fragment2);

list.add(fragment3);

list.add(fragment4);

adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), list);

viewPager.setAdapter(adapter);

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override

public void onPageSelected(int position) {

for(int i = 0;i<arrText.length;i++){

//arrText[i].setEnabled(true);

//所有改成灰色

arrText[i].setBackgroundColor(Color.GRAY);

}

//arrText[position].setEnabled(false);

//選中的改成青色

arrText[position].setBackgroundColor(Color.CYAN);

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override

public void onPageScrollStateChanged(int arg0) {

}

});

}


class MyFragmentPagerAdapter extends FragmentPagerAdapter{

private List<Fragment> list;

public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> list) {

super(fm);

  this.list = list;

}

@Override

public Fragment getItem(int position) {

return this.list.get(position);

}

@Override

public int getCount() {

return this.list.size();

}

    }

}




--------------------------------



Tabs_Fragment.java類


代碼



public class Tabs_Fragment extends Fragment {


private int tabIndex;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

tabIndex = getArguments().getInt("tabIndex");

}

@Override

public View onCreateView(LayoutInflater inflater,

@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

TextView text = new TextView(getActivity());

switch(tabIndex){

case 1:

text.setText("這是消息界面fragment");

text.setTextSize(30);

break;

case 2:

text.setText("這是好友界面fragment");

text.setTextSize(30);

break;

case 3:

text.setText("這是羣組界面fragment");

text.setTextSize(30);

break;

case 4:

text.setText("這是動態界面fragment");

text.setTextSize(30);

break;

}

return text;

}

}

相關文章
相關標籤/搜索