无法从viewpager中找到片段

时间:2017-10-11 07:11:45

标签: android android-fragments android-viewpager

在我的活动中有一个片段,其中有一个viewpager,其中有5个片段,在main活动中我需要从viewpager获取其中一个片段

活动

public class MainActivity extends AppCompatActivity {


    public void setFragment(){

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
    mainFragment= new MainFragment();
    FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container, mainFragment,"mainFragment");

    fragmentTransaction.commitAllowingStateLoss();

        }
    }, 250);
  }

    public void findFragment() {

    mainFragment= (MainFragment) getSupportFragmentManager().findFragmentByTag("mainFragment");

    viewPager = mainFragment.viewPager;
    FragmentOne fragment = (FragmentOne) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem());


  }

}

MainFragment

public class MainFragment extends Fragment{


private void initViewPagerAndTabs() {

    viewPager =  view.findViewById(R.id.viewPager);
    pagerAdapter = new PagerAdapter(getChildFragmentManager());

    pagerAdapter.addFragment(FragmentOne.newInstance(), "one");
    pagerAdapter.addFragment(FragmentTwo.newInstance(), "two");
    pagerAdapter.addFragment(FragmentThree.newInstance(), "three");
    pagerAdapter.addFragment(FragmentFour.newInstance(), "four");
    pagerAdapter.addFragment(FragmentFive.newInstance(), "five");

    viewPager.setAdapter(pagerAdapter);
    TabLayout tabLayout = view.findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);
    viewPager.setCurrentItem(1);
    viewPager.setOffscreenPageLimit(3);
  }

    private static class PagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentTitleList = new ArrayList<>();

    private PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        fragmentList.add(fragment);
        fragmentTitleList.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTitleList.get(position);
    }

  }


}

如果我使用&#34; getFragmentManager()&#34;而不是&#34; getChildFragmentManager()&#34;然后它返回片段但是当我使用&#34; getChildFragmentManager()&#34;在viewPager中,它在活动

中返回null

1 个答案:

答案 0 :(得分:0)

尝试使用1个片段运行代码,它可以帮助您编写代码:

  

主要活动

public class OpenQuran extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

TabLayout tabLayoutqaue;
Pager mAdapter;
MediaPlayer mp;

//This is our viewPagerss
ViewPager viewPager;
View mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_open_quran);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Read Quran");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //Initializing the tablayout
    tabLayoutqaue = (TabLayout) findViewById(R.id.tabLayoutqaue);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);

    //Creating our pager adapter
    mAdapter = new Pager(getSupportFragmentManager());

    // 1 Fragment
    mAdapter .addFrag(new Quran(), "Quran");


    viewPager.setAdapter(mAdapter);

    tabLayoutqaue.setupWithViewPager(viewPager);


    tabLayoutqaue.setOnTabSelectedListener(this);

}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
  

Fargment Class:

public class parahlist extends Fragment {

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

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    return inflater.inflate(R.layout.activity_parahlist, container, false);}


public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

}
  

查看寻呼机类:

public class Pager extends FragmentPagerAdapter {

//integer to count number of tabs
int tabCount;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

//Constructor to the class
public Pager(FragmentManager fm) {
    super(fm);
    //Initializing tab count
    this.tabCount= tabCount;
}

//Overriding method getItem
@Override
public Fragment getItem(int position) {

    return mFragmentList.get(position);

}
@Override
public int getCount() {
    return mFragmentList.size();
}

public void addFrag(Fragment Fragment, String title) {
    mFragmentList.add(Fragment);
    mFragmentTitleList.add(title);
}



@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
}

}

现在,MainActivity,2nd Fragment和3rd one的代码是View Pager类。您无需更改或编辑View Pager类。只需创建Java类并按原样粘贴该代码。 在主活动中,行:mAdapter .addFrag(new Quran(), "Quran");添加说明和片段。您可以在“新”之后添加自己的片段。 之后,您必须将Tablayout小部件放在Xml中,如下所示:

  <android.support.design.widget.TabLayout
        android:id="@+id/tabLayoutqaue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorgreen"
        android:minHeight="?attr/actionBarSize"
        app:tabIndicatorColor="@color/colorback"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

和View Pager一样:

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

您可能会遇到有关tablayout的问题: 只需将此行放在依赖项下的gradle构建中:

 compile 'com.android.support:design:25.3.1' 

根据您的版本。例如你有25.0.1 然后行将是:

compile 'com.android.support:design:25.0.1

我还在这里告诉你更多。如果你有问题!