【问题标题】:Differentiate two instances of the same Fragment区分同一 Fragment 的两个实例
【发布时间】:2017-07-16 21:35:43
【问题描述】:

我有这个包含片段的活动的布局文件:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sguancicosimo.com.planmytrip.NewTrip1"
android:orientation="vertical">




<EditText
    android:id="@+id/Where"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/Where"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="85dp"
    android:drawableLeft="@drawable/ic_location_city_black_24dp"/>

    <ImageView
        android:id="@+id/imageButtonDone"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/Where"
        android:src="@drawable/ic_done_black_24dp"
        android:layout_marginTop="96dp"
        android:layout_marginLeft="25dp"
        />

    <TextView
        android:id="@+id/departureDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/departureDate"
        android:layout_marginTop="35dp"
        android:layout_marginLeft="15dp"
        android:layout_below="@+id/Where"
        android:layout_alignParentStart="true"
        android:textSize="20sp"
        android:textStyle="bold"
        android:clickable="true"
        android:onClick="showCalFrag"/>

    <TextView
        android:id="@+id/returnDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/returnDate"
        android:layout_marginTop="35dp"
        android:layout_marginLeft="15dp"
        android:layout_below="@+id/departureDate"
        android:layout_alignParentStart="true"
        android:textSize="20sp"
        android:textStyle="bold"
        android:clickable="true"
        android:onClick="showCalFrag"/> 

</RelativeLayout>

片段是在运行时单击两个TextView(showCalFrag方法)时创建的。片段包含在一个CalendarView中,我想保留用户选择的日期,在Activity中发送它们,但我不知道如何区分两个 CalendarView,因为它们是同一 Fragment 的两个实例,而且我在让用户更改已选择的日期时遇到问题。

【问题讨论】:

标签: android android-fragments


【解决方案1】:

你可以创建一个enum 喜欢:

public enum FragmentType{

FROM_DATE,TO_DATE

}

并在片段中创建一个具有此枚举类型的新变量:

private FragmentType fragmentType;

public void setFragmentType(FragmentType type){ this.fragmentType = type;}

当调用片段时:

YourFragment fragment = new YourFragment();
fragment.setFragmentType(FragmentType.FROM_DATE); // for the first textView 
fragment.setFragmentType(FragmentType.TO_DATE); // for the second textView

然后你就可以区分这两个实例了

【讨论】:

  • 如何从活动类中检索 FragmentType?
  • 为 FragmentType 创建一个 getter 方法,例如:public FragmentType getFragmentType(){ return fragmentType;} 并从片段的实例中调用它来获取类型
猜你喜欢
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多