2014-07-10 3 views
0

사용자가 특정 드롭 다운 메뉴 스타일 버튼을 누르면 레이아웃이 다른 레이아웃 위에 나타나야하는 응용 프로그램을 작성하고 있습니다. 나는 이것을하기 위해 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> 

감사합니다.

답변

2

true 일 때 .commit() 거래를 잊어 버렸습니다.

또한 새로 생성 된 QuizFragment의 인스턴스 (기존 인스턴스가 아닌)를 제거하려고합니다. FragmentManager.findFragmentById()을 사용하여 기존 단편에 대한 참조를 얻은 다음 해당 참조로 remove()을 호출하십시오.

Fragment quizFragment = fm.findFragmentById(R.id.quizFragment); 
if (quizFragment != null) { 
    ft.remove(quizFragment); 
    ft.commit(); 
} 

또한, 단지 if (!hasRotated)보다는 if (hasRotated == false) 사용하는 것이 바람직합니다.

+0

시도해 보았습니다. 같은 문제. –

+0

@DtrollMC 편집을 참조하십시오 – kcoppock

+0

그래서 당신이 말한대로, fm.findFragmentById (R.id.quizFragment); 그래서 지금 어떻게 실제로 참조를 얻습니까? 귀하의 지시에 따라 혼란 스럽습니다. –

2

매번 새로운 조각을 추가하는 중입니다. 예 : 함수 외부에 아래 문을 작성하십시오.

QuizFragment qf = new QuizFragment(); 

은 또한 당신은 (hasRotated = TRUE) 두 번째 경우에

ft.commit(); 

를 놓쳤다.

코드에서 기본적으로 수행하는 작업은 새 단편을 작성한 다음 첫 번째 사례 (hasRotated = false)에서 추가하고 두 번째 사례 (hasRotated = true)에서 새로운 단편을 작성하고이 새로운 조각 (이전에 만든 조각을 제거하는 대신보기에 추가되지 않은 조각).

관련 문제