我知道,关于此主题的论坛帖子已经有很多,但没有一个能解决我的问题
我的代码如下所示:
private SectionsPagerAdapter myAdapt;
private ViewPager myPager;
....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
if (mSectionsPagerAdapter == null) {
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
}
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
....
但是我得到的Cannot resolve method getChildFragmentManager ()
似乎有很多问题。
我已经尝试了很多东西:
android.support.v7.app.getChildFragmentManager()
,这导致Cannot make static reference from non-static context
错误。整个问题出现了,因为每当我返回活动时,Fragments就会变空。如果您有其他想法,请随时回答或评论,然后解决有关getChildFragmentManager的整个问题
我不知道该怎么做......
ps:这是整个类的实现:
import android.support.v4.app.FragmentManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class HomeScreen extends AppCompatActivity {
int index = 0;
int semIndex = 0;
public static final String GROUP_EXTRA = "Group";
public static final String HOME_SCREEN_LOG = "HomeScreen Activity";
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
if (mSectionsPagerAdapter == null) {
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
}
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
final Bundle b;
if ((b =this.getIntent().getExtras()) != null) {
AsyncTask at = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
String groupName = b.getString(HomeScreen.GROUP_EXTRA),
desc = b.getString(NewEditGroupActivity.DESC_EXTRA),
time = b.getString(NewEditGroupActivity.TIME_EXTRA),
date = b.getString(NewEditGroupActivity.DATE_EXTRA);
int numOfPart = b.getInt(NewEditGroupActivity.NUM_EXTRA);
boolean regular = b.getBoolean(NewEditGroupActivity.IS_NEW);
ExpandListChild elc = new ExpandListChild(desc,String.valueOf(numOfPart),date,time,regular);
ExpandListGroup elg = new ExpandListGroup(groupName);
MyLgFragment lf = (MyLgFragment) mSectionsPagerAdapter.getItem(0);
AllLgFragment af = (AllLgFragment) mSectionsPagerAdapter.getItem(1);
af.add(elc,elg);
lf.add(elc,elg);
return null;
}
};
}
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
inflater = getLayoutInflater();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
如果您需要更多信息,请与我们联系。
答案 0 :(得分:24)
getChildFragmentManager()
是Fragment
的方法。由于您是AppCompatActivity
的子类,因此无法访问它。如果您使用支持库中的Fragment
,则可以使用getSupportFragmentManager()
答案 1 :(得分:2)
这可能不是最优雅的解决方案,但在我的情况下,我需要viewpager在自己的片段中运行,同时让工具栏和tablayout驻留在MainAcitivy中。 唯一的缺点是您必须在不使用Tab / Viewpager的其他片段中手动隐藏TabLayout。
但是这样你仍然可以从片段中调用getChildFragmentManager。
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_rides_host, container, false);
viewPager = (ViewPager) v.findViewById(R.id.fm_feed_host_viewpager);
setupViewPager(viewPager);
((MainActivity)getActivity()).mTabLayout.setupWithViewPager(viewPager);
return v;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(this.getChildFragmentManager());
adapter.addFragment(new YourFragment(), "Title");
adapter.addFragment(...);
viewPager.setAdapter(adapter);
}
答案 2 :(得分:2)
答案 3 :(得分:2)
您应该喜欢:
adapter = new ViewPagerAdapter(this.getChildFragmentManager());
答案 4 :(得分:0)
使用Kotlin,与我使用的Frgament一起使用:
//using this.childFragmentManager
//not as this.childFragmentManager()
val sectionsPagerAdapter = CameraTabLayoutAdapter(this.childFragmentManager)