如何在滑动后访问下一个片段中的按钮?

时间:2016-01-21 15:28:32

标签: java android android-fragments

正如您在此照片中看到的,如果我点击标题1中的2按钮,则会显示Fragment中的FrameLayout。但是,如果我滑动到标题2,则两个按钮不起作用。

enter image description here

以下是代码:

package com.flashcards;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BaseFlashCardsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BaseFlashCardsFragment extends Fragment implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_POSITION = "position";

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

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param position Parameter 1.
     * @return A new instance of fragment BaseFlashCardsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BaseFlashCardsFragment newInstance(int position) {
        BaseFlashCardsFragment fragment = new BaseFlashCardsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            position = getArguments().getInt(ARG_POSITION, 0);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = null;
        int layoutId;
        switch (position) {
            case 0: layoutId = R.layout.limits_flashcardsfragment1; break;
            case 1: layoutId = R.layout.limits_flashcardsfragment2; break;
            default: layoutId = R.layout.limits_flashcardsfragment3; break;
        }

        rootView = inflater.inflate(layoutId, container, false);
        rootView.findViewById(R.id.buttonterm).setOnClickListener(this);
        rootView.findViewById(R.id.buttondefinition).setOnClickListener(this);
        switchPage(new limits_fragmentpageA());
        switchPage(new limits_fragmentpageB());

        return rootView;
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();

        if (viewId == R.id.buttonterm)
            switchPage(new limits_fragmentpageA());
        else if (viewId == R.id.buttondefinition)
            switchPage(new limits_fragmentpageAA());
        else if (viewId == R.id.buttonterm)
            switchPage(new limits_fragmentpageB());
        else if (viewId == R.id.buttondefinition)
            switchPage(new limits_fragmentpageBB());

    }

    private void switchPage (Fragment fragment){
        android.support.v4.app.FragmentManager manager = getFragmentManager();
        manager.beginTransaction()
                .replace(R.id.activity_main_fragmentcontainer, fragment)
                .commit();
    }
}

您也可以download the project获得清晰的观点。

2 个答案:

答案 0 :(得分:0)

我可以看到你需要在你的活动中拥有所有元素(按钮,viewpager,title等),而不是片段,因为它在所有片段之间共享,所以你不需要在每个片段中重新创建它们。

要更改标题(和较低提示),您需要将OnPageChangeListener添加到viewpager适配器。

要在术语/定义之间切换片段,您需要直接在这些按钮的OnClickListener中传递事件,例如" (BaseCardFragment)adapter.instantiateItem(viewpager,viewpager.getCurrentItem).flipToTerm() or flipToDefinition()"

答案 1 :(得分:0)

我认为问题在于,如果你处于片段状态,那么你应该getChildFragmentManager()代替getFragmentManager() 可能你应该像这样重写switchPage()

private void switchPage (Fragment fragment){ android.support.v4.app.FragmentManager manager = getChildFragmentManager(); manager.beginTransaction() .replace(R.id.activity_main_fragmentcontainer, fragment) .commit(); }