我正在编写一个应用程序,当用户按下某个下拉菜单样式按钮时,该应用程序需要在另一个布局上显示布局。我正在使用Fragments和FragmentTransactions来执行此操作。单击按钮时会出现片段(应该这样)但是当我再次单击该按钮时,片段不会消失。
以下是所有相关代码:
MainActivity.java:
package com.example.tipquiz;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.R.anim;
public class MainActivity extends Activity implements OnRatingBarChangeListener {
// Testing Stuff to show the rating value, will need to use later for maths
RatingBar rb;
TextView tv;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;
RelativeLayout dropDownLayout;
Boolean hasRotated = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownLayout = (RelativeLayout) findViewById(R.id.dropDownLayout);
dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void dropDown(View view){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
QuizFragment qf = new QuizFragment();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
if(hasRotated == false){
dropDownButton.setRotation(90);
dropDownLayout.setVisibility(View.VISIBLE);
ft.add(R.id.quizFragment, qf);
ft.show(qf);
ft.commit();
//qf.tv.setVisibility(View.VISIBLE);
hasRotated = true;
}else if(hasRotated == true){
dropDownButton.setRotation(0);
dropDownLayout.setVisibility(View.GONE);
//qf.tv.setVisibility(View.GONE);
hasRotated = false;
ft.hide(qf);
ft.remove(qf);
}
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
// final int numStars = ratingBar.getNumStars();
tv.setText(rating + "/5.0");
}
// http://www.compiletimeerror.com/2013/08/android-rating-bar-example.html#.U7SZ5fldXm4
// http://custom-android-dn.blogspot.ca/2013/01/how-to-use-and-custom-ratingbar-in.html
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbcde3"
android:orientation="vertical" >
<GridLayout
android:id="@+id/gridLayout1"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:background="#e3e3e3"
android:columnCount="2"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="108dp"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_gravity="right|top"
android:layout_row="0"
android:background="#3fa9f5"
android:fontFamily="helvetica"
android:text="@string/settings_button"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</GridLayout>
<Space
android:layout_width="match_parent"
android:layout_height="12sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/gridLayout1"
android:layout_marginTop="24dp"
android:text="@string/rys"
android:textColor="#888888"
android:textSize="19sp" />
<RatingBar
android:id="@+id/ratingBar1"
style="@style/circleRatingBar"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:numStars="5"
android:rating="3.0"
android:stepSize="1.0" />
<ImageView
android:id="@+id/dropDownButton"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignBottom="@+id/ratingBar1"
android:layout_toRightOf="@+id/ratingBar1"
android:onClick="dropDown"
android:src="@drawable/ddb" />
<RelativeLayout
android:id="@+id/dropDownLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView2"
android:visibility="gone" >
<TextView
android:id="@+id/testTV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Testing dropdown" />
</RelativeLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ratingBar1"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp"
android:text="@string/tipTitle"
android:textColor="#888888"
android:textSize="19sp" />
<FrameLayout
android:id="@+id/quizFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/dropDownButton" />
</RelativeLayout>
提前致谢。
答案 0 :(得分:2)
.commit()
为hasRotated
时,您忘了true
您的交易。
此外,您正在尝试删除新创建的QuizFragment
实例 - 而不是现有实例。
使用FragmentManager.findFragmentById()
获取对现有片段的引用,然后使用该引用调用remove()
。
Fragment quizFragment = fm.findFragmentById(R.id.quizFragment);
if (quizFragment != null) {
ft.remove(quizFragment);
ft.commit();
}
此外,最好只使用if (!hasRotated)
而不是if (hasRotated == false)
。
答案 1 :(得分:2)
你每次都要添加一个新片段,即 只需在函数外写下面的语句。
QuizFragment qf = new QuizFragment();
你错过了
ft.commit();
在第二种情况下(hasRotated = true)。
你的代码基本上做的是,创建一个新的片段,然后在第一种情况下添加它(hasRotated = false),在第二种情况下(hasRotated = true),它创建一个新片段并尝试删除这个新片段(尚未添加到视图中)而不是尝试删除先前创建的片段。