麻烦ViewPager显示片段 [英] Trouble with ViewPager displaying Fragments

查看:150
本文介绍了麻烦ViewPager显示片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个片段包含三个选项卡使用ViewPager显示。起初我用来实例使用FragmentMgr活动的片段,这工作得很好。当我转换使用ViewPager这个导航,这个片段将不再显示。

I am trying to get a fragment with three tabs displayed using ViewPager. Initially I used to instantiate the fragment from the Activity using FragmentMgr, this worked fine. When I converted this navigation using ViewPager, this Fragment is no longer displayed.

MainActivity.java

MainActivity.java

当我启动片段这样,它就会显示出来。例如:

When I initiate Fragment like this, it gets displayed. eg:

LaunchListFragment fragment = new LaunchListFragment();
fragment.newInstance(LIST_TYPE);
getSupportFragmentManager().beginTransaction()
                .add(R.id.item_detail_container,   
 fragment).commit();

我尝试了上述code这一变化充分利用ViewPager如下:

I tried this change above code to make use of ViewPager as follows:

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new LaunchPageAdapter(getSupportFragmentManager(),LIST_TYPE));
mViewPager.setCurrentItem(0);

在这里呼吁LaunchPageAdapter LaunchListFragment。

where LaunchPageAdapter calls LaunchListFragment.

LaunchPageAdapter.java

LaunchPageAdapter.java

public class LaunchPageAdapter extends FragmentPagerAdapter {
private static String select="";

    public LaunchPageAdapter(FragmentManager fm, String type) {
        super(fm);
        select=type;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return LaunchListFragment.newInstance(select);
            case 1:
                return AddTeamFragment.newInstance();

        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

下面是LaunchListFragment.java

Here is LaunchListFragment.java

public class LaunchListFragment extends ListFragment implements ActionBar.TabListener {
public static  String LIST_TYPE = "invalid";
GenericListData g_data[] = null;


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

    }
   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View result = inflater.inflate(R.layout.fragment_launch_list,
                container, false);

         setActionTabs();
         setList();
        return (result);
    }


   public static Fragment newInstance(String type) {
    Fragment frag = new LaunchListFragment();
    Bundle args = new Bundle();
    LIST_TYPE=type;
    args.putString(LIST_TYPE, type);
    frag.setArguments(args);
    return (frag);
}

这是MainActivity使用main.xml中的布局

This is main.xml layout used by MainActivity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools=  "match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/item_list"
    android:name="com.teammanager.ItemListFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@android:layout/list_content" />

<FrameLayout
    android:id="@+id/item_detail_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</FrameLayout>

由LaunchListFragment使用fragment_launch_list.xml

fragment_launch_list.xml used by LaunchListFragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" >
</ListView>

<TextView
    android:id="@+id/itemName"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical"
    android:textColor="#000000"
    android:textSize="22dp"
    android:textStyle="bold" />

当我调试这个,我可以看到LaunchListFragment是越来越实例化,以及的onCreate观的onCreate被调用,它只是将不会被显示出来。 (

When I debug this, I can see LaunchListFragment is getting instantiated, onCreate and onCreate View is called, it just won't get displayed. :(

能否有人在这里让我知道如果我在我的ViewPager这里执行缺少什么?

Can anyone here let me know if I am missing anything here in my ViewPager implementation?

更新

当我打电话LaunchListFragment不使用Viewpager,我首先设置内容视图,并通过R.id.item_detail_container这是我想要显示的片段的FrameLayout的ID。请参考前面的main.xml

When I called LaunchListFragment without using Viewpager, I set the content view first and passed R.id.item_detail_container which is the id of the FrameLayout where I want the fragment to be displayed. Please refer to main.xml earlier

 setContentView(R.layout.main.xml);
 LaunchListFragment fragment = new LaunchListFragment();
 fragment.newInstance(LIST_TYPE);
  getSupportFragmentManager().beginTransaction()
                .add(R.id.item_detail_container,    
  fragment).commit();

当我改变了这种使用ViewPager,我不知道在那里我指挥片段显示。在我的片段OnView由我膨胀fragment_launch_list并返回查看。但鉴于寻呼机怎么会知道在哪里设置的片段视图。难道是ID寻呼机里面呢?

When I changed this to use ViewPager, I am not sure where I'm directing the Fragment to be displayed. In my fragment onView I'm inflating fragment_launch_list and returning the View. But how will the view pager know where to set the fragment view. Is it inside the id pager?

我是一个绝对的初学者,我道歉,如果这一切都是幼稚的问题。

I'm an absolute beginner, I apologize if all these are naive questions.

干杯。

推荐答案

这是对您有用,

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> fragments=null;
    private FragmentManager fragmentManager=null;
    public ViewPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragments) {
        super(fragmentManager);
        this.fragments=fragments;
        this.fragmentManager=fragmentManager;
    }
    @Override
    public Fragment getItem(int position) {
        fragmentManager.beginTransaction().commitAllowingStateLoss();
        return fragments.get(position);
    }
    @Override
    public int getCount() {
        return fragments.size();
    }
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object)
    {
        super.setPrimaryItem(container,0,fragments.get(0));
    }
    @Override
    public void notifyDataSetChanged()
    {
        super.notifyDataSetChanged();
    }
    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        fragmentManager.executePendingTransactions();
        fragmentManager.saveFragmentInstanceState(fragments.get(position));
    }
    public void replaceItem(int position,Fragment fragment)
    {
        fragments.set(position, fragment);
        this.notifyDataSetChanged();
    }
}

MainActivity.java

MainActivity.java

public class MainActivity extends FragmentActivity implements OnPageChangeListener,TabListener
{
    private android.support.v4.view.ViewPager mViewPager=null;
    private ViewPagerAdapter mPagerAdapter=null;
    private ActionBar action=null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        initilizeViewPager();
        action=getActionBar();
                action.setDisplayShowHomeEnabled(false);
                action.setDisplayShowTitleEnabled(false);
                action.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }
         public void initilizeViewPager()
     {
                 mViewPager=(android.support.v4.view.ViewPager)findViewById(R.id.viewPager);
         List<Fragment> fragments = new Vector<Fragment>();
                fragments.add(LaunchListFragment.newInstance(select));
                fragments.add(AddTeamFragment.newInstance());
                fragments.add(thirdFragment.newInstance());
                viewPagerAdapter=new ViewPagerAdapter();
mViewPager.setAdapter(viewPagerAdapter(getSupportFragmentManager(),fragments));
                new setAdapterTask().execute();
        mViewPager.setOnPageChangeListener(this);
     }
     private class setAdapterTask extends AsyncTask<Void,Void,Void>
     {
        protected Void doInBackground(Void... params) 
        {
            return null;
        }
        @Override
        protected void onPostExecute(Void result)
        {
            mViewPager.setAdapter(mPagerAdapter);
            Tab first=action.newTab().setTag("first").setText("First").setTabListener(MainActivity.this);
            Tab second=action.newTab().setTag("second").setText("Second").setTabListener(MainActivity.this);
Tab third=action.newTab().setTag("third").setText("Third").setTabListener(MainActivity.this);
            action.addTab(first);
            action.addTab(second);
                    action.addTab(third);
            }
        }
     }
     public void onPageScrollStateChanged(int arg0) 
     {}
     public void onPageScrolled(int arg0, float arg1, int arg2)
     {}
     public void onPageSelected(int position) 
     {
         getActionBar().setSelectedNavigationItem(position);
     }
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
     public void onTabSelected(Tab tab, FragmentTransaction ft) {
         mViewPager.setCurrentItem(tab.getPosition());
     }
     public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
}

main_layout.xml

main_layout.xml

<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="fill_parent"
       android:layout_below="@android:id/tabs"
       android:layout_height="fill_parent"/>
</RelativeLayout>

这篇关于麻烦ViewPager显示片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆