我已经学会了如何在单击按钮时将片段活动从一个更改为另一个(所有都是片段活动),但现在我遇到了同一片段上多个按钮的问题。只有第一个按钮 id 有效。我有多个按钮,每个按钮都有不同的片段活动。需要帮忙
package com.test.fragmentation;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class List extends Fragment {
public List() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
Button ID = (Button) rootView.findViewById(R.id.btnHello);
ID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment NAME = new HelloFragment();
fragmentTransaction.replace(R.id.fragment_container, NAME);
fragmentTransaction.commit();
}
});
return rootView;
}
}
将片段名称更改为要移动的片段名称
Button ID = (Button) rootView.findViewById(R.id.btnHello);
ID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment NAME = new HelloFragment();
fragmentTransaction.replace(R.id.fragment_container, NAME);
fragmentTransaction.commit();
}
});
单击该按钮更改片段名称:-
ABC NAME = new ABC ();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)