我的ViewPage在另一个片段中无法正常工作

时间:2017-05-14 09:32:13

标签: android android-fragments

我有奇怪的问题 我使用框架布局,在里面我使用像下面的一些片段和底部栏处理片段

现在在其中一个片段我使用ViewPager和TabLayout像下面

我的问题是何时将片段从首先变为第三 第一次正确改变但是 第二次它无法正常工作,有些事情发生在下面

这是第三个片段加载(Just Load ViewPager)的代码

public class Social 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 TabLayout tabLayout;
private BestMonth b,b1;
private ViewPager viewPager;

private OnFragmentInteractionListener mListener;

public Social() {
    // Required empty public constructor
    b= new BestMonth();

    b1= new BestMonth();
}


/**
 * 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 Social.
 */
// TODO: Rename and change types and number of parameters
public static Social newInstance(String param1, String param2) {
    Social fragment = new Social();
    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) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_social, container, false);

    viewPager = (ViewPager)view.findViewById(R.id.viewpager);
    setupViewPager(viewPager);


    tabLayout = (TabLayout)view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    setupTabIcons();

    return view;
}

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


private void setupTabIcons() {

    TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
    tabOne.setText("برترین های ماه");
    tabLayout.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
    tabTwo.setText("محمبوب ترین ها");

    tabLayout.getTabAt(1).setCustomView(tabTwo);

}



private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());

    adapter.addFragment(b, "بهترین ها");
    adapter.addFragment(b1, "برترین ها");
    viewPager.setAdapter(adapter);
}

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);
    }
}

@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
    void onFragmentInteraction(Uri uri);
}

}

此片段加载ViewPager内部第三片段的内容

public class BestMonth 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 FloatingActionButton floatingActionButton;


private List<SocialInsClass> movieList = new ArrayList<>();
private RecyclerView recyclerView;
private PostAdapter mAdapter;
private OnFragmentInteractionListener mListener;


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


// TODO: Rename and change types and number of parameters
public static BestMonth newInstance(String param1, String param2) {
    BestMonth fragment = new BestMonth();
    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) {

    View view = inflater.inflate(R.layout.fragment_best_month, container, false);

    view = Init(view);

    return view;
}


private void prepareMovieData() {
    movieList.clear();
    SocialInsClass movie = new SocialInsClass("آرش", "https://ig-s-b-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-19/s150x150/17267780_1886609681623949_4740572605386326016_a.jpg", "گل من", "http://nargil.ir/plant/images/pic/978/Armeria%20Maritima712.jpg");
    movieList.add(movie);

    movie = new SocialInsClass("راضیه", "https://ig-s-c-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-19/14134653_740842746053754_866503745_a.jpg", " قابل شما رو ندار", "http://nargil.ir/plant/images/pic/978/Armeria%20Maritima308.jpg");
    movieList.add(movie);


    movie = new SocialInsClass("یگانه", "https://ig-s-a-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-19/s150x150/17663339_736255433209640_3627796035541139456_a.jpg", "گل دون منزل من", "http://nargil.ir/plant/images/pic/978/Armeria%20Maritima953.jpg");
    movieList.add(movie);

    mAdapter.notifyDataSetChanged();
}

private View Init(View view) {


    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_views);
    floatingActionButton= (FloatingActionButton) view.findViewById(R.id.post);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

          startActivity(new Intent(getActivity(), SendPost.class));

        }
    });


    mAdapter = new PostAdapter(movieList, getActivity());

    prepareMovieData();


    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);




    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 onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

非常感谢您的指南

:)

1) First Fragment 2) Third Fragment I am Using ViePager In It(Load Correctly) 3)When Choose in second time and does not work corectly

0 个答案:

没有答案