我有一個頁面是兩個Fragment
點擊第一頁之後裡面有一個按鈕可以連到一個內頁
這時如果我再去點擊上面的第一頁或第二頁都無法再加入到view裡
請問我該如何做呢?
以下附上Main跟第一頁的Code
<mainActivity>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) this.findViewById(R.id.btn_fragment1);
Button button2 = (Button) this.findViewById(R.id.btn_fragment2);
Button back = (Button) this.findViewById(R.id.button);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onButtonClick(view.getId());
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onButtonClick(view.getId());
}
});
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
private void onButtonClick(int id) {
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.layout_container);
if (fragment == null) {
if (id == R.id.btn_fragment1)
fragment = new Sample1Fragment();
else
fragment = new Sample2Fragment();
replaceFragment(fragment);
} else if (id == R.id.btn_fragment1
&& fragment instanceof Sample2Fragment) {
replaceFragment(new Sample1Fragment());
} else if (id == R.id.btn_fragment2
&& fragment instanceof Sample1Fragment) {
replaceFragment(new Sample2Fragment());
}
}
public void replaceFragment(Fragment fragment) {
this.getFragmentManager()
.beginTransaction()
.replace(R.id.layout_container, fragment).addToBackStack(null)
.commit();
}
public void onBackPressed()
{
if (getFragmentManager().getBackStackEntryCount() > 0){
getFragmentManager().popBackStack();
}
else super.onBackPressed();
}
<SampletFragment1>
public class Sample1Fragment extends Fragment {
private String mTimeString;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_sample1, container,
false);
TextView txtDateTime = (TextView) view.findViewById(R.id.txtDateTime);
txtDateTime.setText("這裡是第一頁");
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClick(view.getId());
}
});
return view;
}
public void replaceFragment(Fragment fragment) {
this.getFragmentManager()
.beginTransaction()
.replace(R.id.layout_container, fragment).addToBackStack(null)
.commit();
}
private void onButtonClick(int id) {
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.layout_container);
if (fragment == null) {
if (id == R.id.btn_fragment1)
fragment = new Sample1Fragment();
else
fragment = new Sample2Fragment();
replaceFragment(fragment);
} else if (id == R.id.btn_fragment1
&& fragment instanceof Sample2Fragment ) {
replaceFragment(new Sample1Fragment());
} else if (id == R.id.btn_fragment2
&& fragment instanceof Sample1Fragment ) {
replaceFragment(new Sample2Fragment());
}else if (id == R.id.btn_fragment1 && fragment instanceof Sample3Fragment){
replaceFragment(new Sample1Fragment());
}
replaceFragment(new Sample3Fragment());
}
}
巴扎黑2017-04-10 16:18:58
其實自己已經找到解決辦法
修改MainActivity下面的nclick就可以了~
附上代碼
private void onButtonClick(int id) {
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.layout_container);
//?????Layout???
if (fragment == null) {
if (id == R.id.btn_fragment1) {
fragment = new Sample1Fragment();
} else {
fragment = new Sample2Fragment();
}
replaceFragment(fragment);
} else if ((id == R.id.btn_fragment1) &&
fragment instanceof Sample2Fragment) {
replaceFragment(new Sample1Fragment());
} else if ((id == R.id.btn_fragment2) &&
fragment instanceof Sample1Fragment) {
replaceFragment(new Sample2Fragment());
} else if ((id == R.id.btn_fragment1) &&
fragment instanceof Sample3Fragment) {
replaceFragment(new Sample1Fragment());
} else if ((id == R.id.btn_fragment2) &&
fragment instanceof Sample3Fragment) {
replaceFragment(new Sample2Fragment());
}
}