在片段中实现滑动选项卡
Implementing sliding tabs in a fragment
我正在查看有关如何使用 TabLayout 的指南。这就是他们显示附加 PagerAdapter class 的方式。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
但是,他们的 TabLayout 在 Activity 中,而我的在 Fragment 中。我无法根据我的用例调整他们的代码,因为我不完全理解活动和片段之间的实际区别。现在,当我尝试在选项卡之间滑动时,没有任何反应。这是我的代码:
public class CandidateListFragment extends Fragment {
public static final String TAG = "candidates";
@InjectView(R.id.tabLayout) TabLayout tabLayout;
public CandidateListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_candidatelist, container, false);
initInstances(v);
return v;
}
private void initInstances(View v){
//TODO: Change to support below API level 21
getActivity().findViewById(R.id.toolbar_actionbar).setElevation(R.dimen.toolbar_candidatelist_elevation);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
viewPager.setAdapter(new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
getActivity()));
ButterKnife.inject(this, v);
tabLayout.setupWithViewPager(viewPager);
}
@Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
我明白了!!!
我将片段的 xml 布局文件设置为 RelativeLayout 而不是 LinearLayout,因此我的 ViewPager 不在屏幕上,这意味着我无法滑动。这是让我犯下的简单错误
我正在查看有关如何使用 TabLayout 的指南。这就是他们显示附加 PagerAdapter class 的方式。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
但是,他们的 TabLayout 在 Activity 中,而我的在 Fragment 中。我无法根据我的用例调整他们的代码,因为我不完全理解活动和片段之间的实际区别。现在,当我尝试在选项卡之间滑动时,没有任何反应。这是我的代码:
public class CandidateListFragment extends Fragment {
public static final String TAG = "candidates";
@InjectView(R.id.tabLayout) TabLayout tabLayout;
public CandidateListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_candidatelist, container, false);
initInstances(v);
return v;
}
private void initInstances(View v){
//TODO: Change to support below API level 21
getActivity().findViewById(R.id.toolbar_actionbar).setElevation(R.dimen.toolbar_candidatelist_elevation);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
viewPager.setAdapter(new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
getActivity()));
ButterKnife.inject(this, v);
tabLayout.setupWithViewPager(viewPager);
}
@Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
我明白了!!! 我将片段的 xml 布局文件设置为 RelativeLayout 而不是 LinearLayout,因此我的 ViewPager 不在屏幕上,这意味着我无法滑动。这是让我犯下的简单错误