片段中的TabLayout问题(android / java)

时间:2015-10-29 19:03:43

标签: java android tabs android-viewpager android-tablayout

我试图在我的应用程序中添加tabLayout。当我第一次打开片段(其中有我的tablayout)它工作正常,但如果我关闭片段并尝试重新打开它,选项卡指示器不会跟随我的滑动并且所有viewPager变为空白...我该如何解决这个问题?

我的片段:

package com.gabrielemarcozzi.mycal;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PercentageFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment PercentageFragment.
 */
// TODO: Rename and change types and number of parameters
public static PercentageFragment newInstance(String param1, String param2) {
    PercentageFragment fragment = new PercentageFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public PercentageFragment() {
    // Required empty public constructor
}

private FragmentActivity context;

@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) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_percentage, container, false);

    Toolbar mToolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
    mToolbar.setTitle(R.string.percentage);

    ViewPager percentagePager = (ViewPager)view.findViewById(R.id.percentage_pager);
    percentagePager.setAdapter(new PercentagePagerAdapter(context.getSupportFragmentManager()));

    TabLayout percentageTab = (TabLayout)view.findViewById(R.id.percentage_tabs);
    percentageTab.setupWithViewPager(percentagePager);

    return view;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        MainActivity main = (MainActivity) activity;
        mListener = main;
        context = (FragmentActivity) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}

class PercentagePagerAdapter extends FragmentPagerAdapter {

    String[] pergentageTabsArray;

    public PercentagePagerAdapter(FragmentManager fm) {
        super(fm);
        pergentageTabsArray = getResources().getStringArray(R.array.percentage_tabs);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        TabsPercentageFragment TabsPercFrag = TabsPercentageFragment.getIstance(position);
        return TabsPercFrag;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return pergentageTabsArray[position];
    }

    @Override
    public int getCount() {
        return 3;
    }
}

public static class TabsPercentageFragment extends android.support.v4.app.Fragment {

    public static TabsPercentageFragment getIstance(int position) {
        TabsPercentageFragment increaseFrag = new TabsPercentageFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        increaseFrag.setArguments(args);
        return increaseFrag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view;
        Bundle bundle = getArguments();
        switch(bundle.getInt("position")) {
            case 1:
                view = inflater.inflate(R.layout.fragment_discount, container, false);
                break;
            case 2:
                view = inflater.inflate(R.layout.fragment_xofy, container, false);
                break;
            default:
                view =inflater.inflate(R.layout.fragment_increase, container, false);
                break;
        }

        return view;
    }
}

}

我的片段xml文件:

<LinearLayout
    android:id="@+id/percentage_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gabrielemarcozzi.mycal.PercentageFragment">

    <android.support.design.widget.TabLayout
        android:id="@+id/percentage_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabMode="fixed"
        app:tabIndicatorColor="@color/colorUp"
        app:tabTextColor="@android:color/white"
        app:tabSelectedTextColor="@color/colorUp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/percentage_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <!-- TODO: Update blank fragment layout -->

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试在以下位置使用getChildFragmentManager()而不是getSupportFragmentManager():

percentagePager.setAdapter(new PercentagePagerAdapter(context.getSupportFragmentManager()));

并更改您的导入: import android.app.Fragment;

import android.support.v4.app.Fragment;