2014-07-22 3 views
0

저는 단편 상호 작용을보다 잘 처리하여 이벤트가 한 번만 발생하도록하는 방법을 찾아 내려고 며칠 동안 머리를 쓰고있었습니다. 필자는 본질적으로 하나의 메인 프래그먼트 컨테이너를 보유하는 주요 활동을 가지고 있습니다. 그 주 용기는 세 개의 다른 용기 파편 (기둥)을 수용한다. 이 세 조각에는 각각 다음과 같이 여러 다른 자식 조각이 있습니다. 중복 된 조각을 방지하려면 어떻게해야합니까?

Default layout

내가 자식 조각 중 하나를 클릭

는 주요 활동은 단편 관리자를 가져오고 단편 클릭으로 하나의 주요 조각 용기를 대체; 본질적으로 조각을 가져 와서 전체 화면으로 만듭니다. 제가

봉착

Child Fragment clicked fullscreen

문제는 하위 단편이 전체 화면 후 단편 내의 추가 클릭 후속 단편을 생성한다는 것이다. 따라서 전체 화면 조각을 4 번 클릭하면 5 개의 조각보기로 끝납니다. 주요 활동 & 4 개의 중복에서 처음 클릭하십시오.

TL/DR - 질문 : 전체 화면이 된 후 프래그먼트가 대체되지 않게하려면 어떻게해야합니까? (제 3 열)

MFDTailboard.java

public class MFDTailboard extends Activity implements Calendar.OnFragmentInteractionListener, 
    .... 
    public void onCalendarFragmentInteraction(Uri uri) { 
     getFragmentManager().beginTransaction() 
      .replace(R.id.MFDTailboardContainer, Calendar.newInstance("Calendar Fragment", "CalFrag")) 
      .addToBackStack("Fullscreen Calendar") 
      .commit(); 
    } 

Calendar.java

public class Calendar extends Fragment implements TextView.OnClickListener { 
.... 
     @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_calendar, container, false); 

     nextScheduledDate = (TextView) view.findViewById(R.id.NextScheduledDate); 
     nextScheduledDate.setOnClickListener(this); 
     // Inflate the layout for this fragment 
     return view; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnFragmentInteractionListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    public void onClick(View v) { 
      mListener.onCalendarFragmentInteraction(null); 
    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p/> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
      // TODO: Update argument type and name 
      public void onCalendarFragmentInteraction(Uri uri); 
    } 

레이아웃 activity_mfdtailboard.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/MFDTailboardContainer" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

</RelativeLayout> 

레이아웃 fragment_calendar_callback_container

,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/FragmentContainer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:background="@android:color/darker_gray" 
android:orientation="vertical" 
android:weightSum="2" 
tools:context=".fragments.CalendarCallback"> 

</LinearLayout> 

레이아웃 fragment_calendar.xml (전체 화면/아동 조각)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/calendar_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="1dp" 
    android:layout_weight="1" 
    android:background="@android:color/white" 
    android:orientation="vertical" 
    android:gravity="center_vertical"> 

    <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/calendar_header" 
      android:id="@+id/calendar_header" 
      android:layout_weight="0" 
      android:gravity="center_horizontal" 
      android:background="@android:color/background_dark" 
      android:textColor="@android:color/primary_text_dark" 
      android:textSize="18sp" /> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 

      <TextView 
        android:id="@+id/NextScheduledLabel" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="@string/next_scheduled_label" 
        android:textSize="24sp" /> 

      <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="right" 
        android:orientation="vertical"> 

        <TextView 
          android:id="@+id/RemainingLabel" 
          android:layout_width="wrap_content" 
          android:layout_height="match_parent" 
          android:text="@string/remaining_label" 
          android:textSize="18sp" /> 

        <TextView 
          android:id="@+id/RemainingCount" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:gravity="center" 
          android:text="@string/remaining_count" 
          android:textSize="30sp" /> 
      </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="1"> 

      <TextView 
        android:id="@+id/NextScheduledDate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:text="@string/next_scheduled_day" 
        android:textSize="102sp" /> 

      <TextView 
        android:id="@+id/NextScheduledMonth" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:text="@string/next_scheduled_month" 
        android:textSize="36sp" 
        android:layout_marginTop="-20dp" /> 
    </LinearLayout> 

    <View 
      android:id="@+id/HorizontalLine" 
      android:layout_width="wrap_content" 
      android:layout_height="1dp" 
      android:background="@android:color/black" /> 

    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:baselineAligned="false" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 

      <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:orientation="vertical"> 

        <TextView 
          android:id="@+id/CycleFirstDate" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/cycle_first_date" 
          android:textSize="30dp" /> 

        <TextView 
          android:id="@+id/CycleFirstDay" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/cycle_first_day" 
          android:textSize="18dp" /> 

      </LinearLayout> 

      <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:orientation="vertical"> 

        <TextView 
          android:id="@+id/CycleSecondDate" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/cycle_second_date" 
          android:textSize="30dp" /> 

        <TextView 
          android:id="@+id/CycleSecondDay" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/cycle_second_day" 
          android:textSize="18dp" /> 

      </LinearLayout> 

      <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:orientation="vertical"> 

        <TextView 
          android:id="@+id/CycleThirdDate" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/cycle_third_date" 
          android:textSize="30dp" /> 

        <TextView 
          android:id="@+id/CycleThirdDay" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/cycle_third_day" 
          android:textSize="18dp" /> 

      </LinearLayout> 

    </LinearLayout> 

당신의 도움에 미리 감사합니다!

답변

0

아마 당신은 다음과 같이 배열 내부에 조각 인스턴스를 유지할 수 :

Fragment[] myFragments = { 
    new Fragment1(), 
    new Fragment2(), 
    ... 
}; 

그런 다음 당신이 그 (것)들을 교환 할 수

public void changeFragment(Fragment fragment){ 
    if(null != fragment && fragment != actualFragment){ 
     FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction(); 
     ft.replace(R.id.container, fragment); 
     ft.addToBackStack(null); 
     ft.commit(); 
    } 
} 

changeFragment(myFragments[i])이 작동 희망 호출하여.

감사합니다.

+0

하위 조각은 null이 아니며 레이아웃을 기반으로 한 작은 컨테이너에 저장됩니다. 조각 트랜잭션은 매번 실행됩니다. onFragmentInteraction은 주요 액티비티에 바인딩되어 있으므로 조각을 클릭 할 때마다 조각 만들기 코드가 실행되고, 지금은 별도의 메서드가 아닌 배열을 통해 실행됩니다. 내 파편을 배열에 집어 넣는 것이 어떻게 그들의 생성을 onFragmentInteraction의 단일 캐치로 제한 하는지를 설명 할 수 있습니까? – Rescue9

+0

인스턴스 배열을 사용하면 동일한 조각의 다른 인스턴스로 표시되는 실제 조각을 덮어 쓰려고하지 않아도됩니다. 또한, 조각을 인스턴스화하는 곳을 확인하는 것이 좋습니다. '.replace (R.id.MFDTailboardContainer, Calendar.newInstance ("일정 조각", "CalFrag"))'이 줄은 모든 추가 인스턴스를 만드는 것입니다. 당신은 달력을 클릭합니다. 희망이 당신에게 어떤 도움이됩니다. – FeleMed

관련 문제