我想根据按钮单击更改片段。 (我已经做了两个片段类称为fragmentLeft和fragmentMiddle)在按钮上更改片段单击
这是我MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
Fragment fr;
public void selectFrag(View view) {
if(view == findViewById(R.id.bigbutton_left)) {
fr = new fragmentLeft();
}else {
fr = new fragmentMiddle();
}
FragmentManager fm = getFragmentManager();
if (fm != null) {
// Perform the FragmentTransaction to load in the list tab content.
// Using FragmentTransaction#replace will destroy any Fragments
// currently inside R.id.fragment_content and add the new Fragment
// in its place.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fr);
ft.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是fragmentLeft.java
public class fragmentLeft extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragmentleft, container, false);
}
}
同样是fragmenttwo的除外将布局ID传递给infater.inflate()
我制作了两个布局文件两个,并使用了正确的名称。 这是我的片断的定义从我activity_main.xml中
但我无法得到的按钮做任何事情。没有交易发生,我坚持第一个片段。
有点谷歌搜索后,我才知道,它不建议您在布局中使用的片段,但一些其他类型的布局,但我看到这个示例代码http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ ,似乎工作就好了。
是什么问题?
::编辑:: 有一些真正奇怪的事情发生。 如果我使用的主要活动此布局的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.mainpackage.FragmentOne"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
片段发生变化,表现为他们预计。 但是,如果我用这个布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="@+id/bigbutton_middle"
android:layout_alignBottom="@+id/bigbutton_middle"
android:layout_marginLeft="7dp"
android:layout_toRightOf="@+id/bigbutton_middle"
android:background="@drawable/mainbutton_right" />
<Button
android:id="@+id/bigbutton_left"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="@+id/bigbutton_middle"
android:layout_alignBottom="@+id/bigbutton_middle"
android:layout_marginRight="7dp"
android:layout_toLeftOf="@+id/bigbutton_middle"
android:background="@drawable/mainbutton_left" />
<Button
android:id="@+id/bigbutton_middle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="187dp"
android:background="@drawable/mainbutton_middle" />
<fragment
android:id="@+id/fragment_place"
android:name="com.mainpackage.FragmentOne"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/bigbutton_right" />
</RelativeLayout>
片段似乎没有改变,即。按钮似乎已经死了。
可能是什么问题?
我使用Eclipse朱诺
那么,你发布的代码没有任何引用Button的'OnClickListeners'。我假设你有一些,是吗? – Wamasa
我不,我期待selectFrag为我完成这项工作。 –