如何从另一个片段的 onTouch() 中替换一个片段?

How can I replace a Fragment from within onTouch() of another Fragment?

我需要在用户触摸另一个 FragmentTextView 时显示一个 Fragment。这是我拥有的:

public class FindPeopleHelpFragment extends Fragment {

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find_people_help, container, false);

        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.find_people_help);

        TextView welcome_home = (TextView) view.findViewById(R.id.find_people_help);
        welcome_home.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent evt) {
                if (evt.getAction() == MotionEvent.ACTION_DOWN) {
                    EditProfileFragment editProfileFragment = new EditProfileFragment();
                    FragmentManager manager = getActivity().getSupportFragmentManager();
                    manager.beginTransaction().replace(R.id.fragment_container, editProfileFragment, editProfileFragment.getTag()).commit();
                }
                return true;
            }
        });

        // Inflate the layout for this fragment
        return view;
    }
}

问题是 onTouch() return 是 boolean,但是当替换 Fragment 时,我需要 return 是 View .否则我会得到以下内容。

 10-05 15:00:51.057 25152-25152/wegrok.chiaramail.com E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: wegrok.chiaramail.com, PID: 25152
                                                                       java.lang.IllegalArgumentException: No view found for id 0x7f0f00b1 (wegrok.chiaramail.com:id/fragment_container) for fragment FindPeopleHelpFragment{c052157 #1 id=0x7f0f00b1}

有人知道怎么处理吗?

您需要从您的 Activity 进行这些 Fragment 交易。托管 FragmentActivity 应该有一个 public 函数来处理片段切换,这样当你的 Fragment1TextView 被点击时, [= Activity 的 34=] 方法将被调用以进行 Fragment 切换。

所以您可以考虑拥有这样的 Activity

public class YourActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        // The Fragment 1 will be loaded by default
        switchToFragment1();
    }

    public void switchToFragment1() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new Fragment1()).commit();
    }

    public void switchToFragment2() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new Fragment2()).commit();
    }
}

现在从您的 Fragment1,您需要在 TextView 中实现一个 onClick 侦听器,以便在单击 TextView 时,Fragment1 切换到 Fragment2

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ((YourActivity) getActivity()).switchToFragment2();
    }
});) 

希望对您有所帮助。