簡體   English   中英

片段中的TabLayout問題(android / java)

[英]Issue with TabLayout in fragment (android/java)

我試圖在我的應用程序中添加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>

嘗試在以下位置使用getChildFragmentManager()而不是getSupportFragmentManager():

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

並從以下位置更改導入: import android.app.Fragment;

import android.support.v4.app.Fragment;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM