วันศุกร์ที่ 29 สิงหาคม พ.ศ. 2557

การใช้งาน ViewPagerIndicator


1.สร้าง MainActivity with Fragment

\\MainActivity//

import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainmenuActivity extends FragmentActivity {
  
    FragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;
    int Number = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout."ชื่อ layout ของเรา");
      
        mAdapter = new FragmentAdapter(getSupportFragmentManager());
        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
                 เปลี่ยนเป็น สไตต่างๆเช่น
                  Title
                  //mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);

                   Tab
        //mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
                    เลือกเอาซักอย่าง

        mIndicator.setViewPager(mPager);
     
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu. "ดูชื่อใน res->menu แล้วก็อบมาใส" , menu);
        return true;
    }
  
  
}

เออเร่อชั่งมั้น กดแก้เองเด๋วมันก็หาย
หน้า xml ของ MainActivity


\\Your Fragment Holder in the XML//
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


//Title    <com.viewpagerindicator.TitlePageIndicator
         //Tab    <com.viewpagerindicator.TabPageIndicator  เลือกเอา
        android:id="@+id/indicator"
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="#000000"
        android:textColor="#2FB3E3"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        /> 
</LinearLayout>

จากนั้นก็ไปที่ res->values->style.xml  ถ้าใช้ Tab ใส่ลงไปใน AppTheme ด้วย 
ไม่งั้นแม่งสไตหายกุงงตั้งนานไอสัส
<item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item>

2. สร้าง FragmentAdapter.java 

\\Fragment Adapter//

import com.viewpagerindicator.IconPagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
    public FragmentAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }
    @Override
    public int getIconResId(int index) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public Fragment getItem(int position)
    {
        // TODO Auto-generated method stub
        Fragment fragment = new BitFragment();
        switch(position){
        case 0:
            fragment = new BitFragment();
            break;
        case 1:
            fragment = new ByteFragment();
            break;
        case 2:
            fragment = new KiloByteFragment();
            break;
        case 3:
            fragment = new MegaByteFragment();
            break;
        }
        return fragment;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub

                 มีกี่ tab เมิงกะใส่ตรงนี้นะ
        return 4;
    }
  
    @Override
    public CharSequence getPageTitle(int position){
        String title = "";
        switch(position){
        case 0:
            title = "Bit";
            break;
        case 1:
            title = "Byte";
            break;
        case 2:
            title = "KiloByte";
            break;
        case 3:
            title = "MegaByte";
            break;
        }
        return title;
    }
}
 3.สร้าง Fragment (Activity ) class+xml หน้าต่างๆที่จะเรียกใช้

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ชื่อไรก็ว่าไปFragment extends Fragment {
  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootview = inflater.inflate(R.layout.ชื่อเลเอ้าเปลี่ยนตามด้วย, null);
        
         //Calendar
         CaldroidFragment caldroidFragment;
        caldroidFragment = new CaldroidFragment();
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        caldroidFragment.setArguments(args);
        android.support.v4.app.FragmentTransaction t = getFragmentManager().beginTransaction();
        t.replace(R.id.calendar1, caldroidFragment);
        t.commit();


        //  About Clear Button
        Button btnMultipleClear = (Button) view.findViewById(R.id.btnMultipleClear);
        btnMultipleClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Clear", Toast.LENGTH_SHORT).show();
            }
        });
      
      
        return rootview ;
      
    }
}
    xml กำหนดเองเลยอยากใส่ไรใส่ได้เลย



เดบิต By Mizzeeboy กุรักเมิงฝุดๆขอบใจมากๆ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น