每当我在onCreate方法中使用SectionsStateManager来获取要显示的片段时,工具栏仍然是可见的(连同其菜单项一样),但完全不可单击。下面是我的MainActivity的onCreate方法,片段的(MainFeed)onCreateView方法和SectionsStatePagerAdapter的代码。
更新:还包括 activity_main
MainActivity的onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
Log.d(TAG, "onCreate: Started.");
RedditClient redditClient = new RedditClient(UserAgent.of("android",
"com.digitalnode.feedr", "v0.0.1", "TheFiveHundred"));
TokenStore store = new RedditTokenStore(
PreferenceManager.getDefaultSharedPreferences(MainActivity.this));
RefreshTokenHandler refreshTokenHandler = new RefreshTokenHandler(store, redditClient);
AuthenticationManager manager = AuthenticationManager.get();
manager.init(redditClient, refreshTokenHandler);
main_auth_manager = manager;
mSectionsStatePagerAdapter = new SectionsStatePagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
//setup the pager
setupViewPager(mViewPager);
}
我的片段的onCreateView方法
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_feed_layout, container, false);
Log.d(TAG, "onCreateView: started.");
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
redditClient = new RedditClient(UserAgent.of("android",
"com.digitalnode.feedr", "v0.0.1", "TheFiveHundred"));
TokenStore store = new RedditTokenStore(
PreferenceManager.getDefaultSharedPreferences(getContext()));
RefreshTokenHandler refreshTokenHandler = new RefreshTokenHandler(store, redditClient);
AuthenticationManager manager = AuthenticationManager.get();
manager.init(redditClient, refreshTokenHandler);
Entry[] arr = new Entry[entryList.size()];
for(int i = 0; i < arr.length; i++)
{
arr[i] = entryList.get(i);
}
// specify an adapter (see also next example)
Log.d("posts", "" + entryList);
mAdapter = new EntryAdapter(arr);
mRecyclerView.setAdapter(mAdapter);
//if(redditIsLoggedIn())
//new SetUpReddit().execute();
return view;
}
SectionsStatePagerAdapter
public class SectionsStatePagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsStatePagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
activity_main布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
layout = "@layout/toolbar"
android:id="@+id/tool_bar"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>