我是android的新手。我想在android中设计一个包含片段内Tab视图的页面。
片段:
为此,我有一个名为fragment_home.xml的片段。
标签的布局资源文件:
三个布局资源文件名为
today.xml
this-week.xml
this_month.xml
和资源文件的java类是
Today.java
ThisWeek.java
Thismonth.java
我怀疑的是如何在片段中实现制表视图。我已经提到了一些SO文件,但对我没什么帮助。
Home.java(片段):
public class Home extends Fragment{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
private OnFragmentInteractionListener mListener;
public Home() {
}
public static Home newInstance(String param1, String param2) {
Home fragment = new Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
Pager adapter = new Pager(getChildFragmentManager());
adapter.addFragment(new Today(), "Today");
adapter.addFragment(new Reminders(), "Reminders");
adapter.addFragment(new Settings(), "Settings");
viewPager.setAdapter(adapter);
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
我想在这里实施一些但我不知道的,怎么样? 请帮我搞定。
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Home">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
PagerAdapter
public class Pager extends FragmentStatePagerAdapter {
int tabCount;
public Pager(FragmentManager fragmentManager, int tabCount) {
super(fragmentManager);
this.tabCount= tabCount;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Today todayObj = new Today();
return todayObj;
case 1:
ThisWeek thisWeekObj = new ThisWeek();
return thisWeekObj;
case 2:
ThisMonth thisMonthObj = new ThisMonth();
return thisMonthObj;
default:
return null;
}
}
@Override
public int getCount() {
return tabCount;
}
public void addFragment(Today today, String today1) {
}
public void addFragment(Reminders reminders, String reminders1) {
}
public void addFragment(Settings settings, String settings1) {
}
}
答案 0 :(得分:1)
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class Home extends Fragment{
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new FragmentOne(), "Fragment name one"); //Need to call first tab fragment
adapter.addFragment(new FragmentTwo(), "Fragment name two"); // Need second tab fragment and so on...
viewPager.setAdapter(adapter);
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
FragmentOne.class
public class FragmentOne extends Fragment {
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
}
fragment_one.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="@color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment One Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
FragmentTwo.class
public class FragmentTwo extends Fragment {
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
}
}
fragment_two.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="@color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
使用此代码,这是使用view pager
的tablayout的完整示例