我已经好好打了几天,试图找出如何更好地处理片段交互,以便事件只触发一次。基本上我有一个主活动,它包含一个主要的片段容器。该主容器容纳另外三个容器碎片(柱子)。然后,这三个片段中的每一个都包含各种其他子片段,如下所示。
当我单击其中一个子片段时,主要活动获取片段管理器并用单击的片段替换单个主片段容器;基本上它正在拍摄片段并使其全屏显示。
我遇到的问题是,在子片段全屏后,片段中的任何额外点击都会创建后续片段。因此,如果我单击全屏片段4次,我最终得到5个片段视图;来自主要活动&的初始点击4个重复。
TL / DR - 问题:如何在片段全屏后阻止片段自行替换?
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(3列中的第1列)
<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 :(得分:0)
也许您可以将片段实例保存在数组中,如下所示:
Fragment[] myFragments = {
new Fragment1(),
new Fragment2(),
...
};
然后你可以通过调用changeFragment(myFragments[i])
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();
}
}
希望它有效。
最好的问候。