我知道这个问题已被提出,我知道我需要做什么;我只需要一点点推动就能让我超越终点线。这就是我所拥有的。
带有两个标签的Android应用程序,我需要根据选择的标签切换片段视图。到目前为止,这是我的代码。
主要活动
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// This is called when a tab is selected.
Log.d(app, "Tab: " + tab.getPosition());
switch (tab.getPosition()) {
case 0:
Test1Fragment test1Fragment = new Test1Fragment();
fragmentTransaction.add(R.id.fragment_container, test1Fragment);
break;
case 1:
//Intent i = new Intent(this, Test1.class);
//startActivity(i);
Test2Fragment test2Fragment = new Test2Fragment();
fragmentTransaction.add(R.id.fragment_container, test2Fragment);
break;
case 2:
break;
default:
}
}
// Implemented from ActionBar.TabListener
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// This is called when a previously selected tab is unselected.
}
// Implemented from ActionBar.TabListener
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// This is called when a previously selected tab is selected again.
}
main_activity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container">
我只是不知道如何给碎片充气。单击选项卡1时,此代码将显示test1Fragment,但单击选项卡2时,test1Fragment仍会显示。我知道我需要拆除和更换。我只是不知道该怎么做。请帮忙
答案 0 :(得分:0)
我首先看到的是,你缺少一个完成交易的提交()。
遵循本教程: https://www.youtube.com/watch?v=Gimo5cZler8 而它的后续教程,实现viewpager也很有趣。
它帮助我在我的应用程序中成功实现它。 https://www.youtube.com/watch?v=iJ178Z8U7FM
这是你感兴趣的部分:
package com.example.multiplefrag;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.fragmentexample.R;
public class MenuFragment extends Fragment {
Fragment frag;
FragmentTransaction fragTransaction;
public MenuFragment() {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_multi, container, false);
frag = new KoalaFragment();
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container,frag);
fragTransaction.commit();
Button btnKoala = (Button) view.findViewById(R.id.buttonKoala);
Button btnPenguin = (Button) view.findViewById(R.id.buttonPenguin);
Button btnOther = (Button) view.findViewById(R.id.buttonOther);
btnKoala.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
frag = new KoalaFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container,frag);
fragTransaction.commit();
}
});
btnPenguin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
frag = new PenguinFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container,frag);
fragTransaction.commit();
}
});
btnOther.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
frag = new OtherFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container,frag);
fragTransaction.commit();
}
});
return view;
}
}