在Android sdk中使用滑动视图调用尽可能多的片段(布局)

时间:2014-02-18 15:55:56

标签: android android-fragments menu swipeview

我有以下课程:

 public class MyFragment extends Fragment{

    int mCurrentPage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Getting the arguments to the Bundle object */
        Bundle data = getArguments();

        /** Getting integer data of the key current_page from the bundle */
        mCurrentPage = data.getInt("current_page", 0);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     

        View v = inflater.inflate(R.layout.myfragment_layout, container,false);             
        TextView tv = (TextView ) v.findViewById(R.id.tv);
        tv.setText("Você está no item " + mCurrentPage +" do menu"+ "\n\n" + "deslize para os lados para poder escolher a opção");  
        return v;

在oncreateview方法只调用布局的情况下,如何在每个paágina或滑动视图选项卡中将其称为布局(片段),主要用作菜单。

梨作为Bandle方法参与以下课程:

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 5;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment.setArguments(data);
        return myFragment;
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {     
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {        
        return "Pag" + ( position + 1 );
    }

主要看起来像这样:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Obtendo uma referência ao ViewPager definido o arquivo de layout */        
        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Obtendo gerente fragmento */
        FragmentManager fm = getSupportFragmentManager();

        /** instanciando FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** definindo o pagerAdapter ao objeto pager */
        pager.setAdapter(pagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

和/或xml main_activity:

<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" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_tab_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />
    </android.support.v4.view.ViewPager>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="Menu Principal"
        android:textColor="#1E90FF"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="25sp" />

</RelativeLayout>

和myfragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"    
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center"    
    >    
</TextView>   

请给我发送代码,这些代码可以考虑我的目标来绘制几个布局!

0 个答案:

没有答案