如何在我的活动中获取片段的元素并更改其值?
你有这个片段......
public class EventDetail extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.event_detail, container, false);
return v;
}
}
... fragment_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgEventCover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:src="@drawable/imagemapoio2" />
<TextView
android:id="@+id/txtEventDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtEventInfo"
android:layout_centerVertical="true"
android:text="LXFACTORY | 2km"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/txtEventInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtEventDetail"
android:layout_centerHorizontal="true"
android:text="Playground present Drum&Bass in the Factory"
android:textColor="@color/dk" />
<ImageView
android:id="@+id/imgTiming"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_below="@+id/txtEventTime"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:src="@drawable/yellowtiming" />
<TextView
android:id="@+id/txtEventTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtEventDetail"
android:layout_below="@+id/txtEventInfo"
android:layout_marginTop="5dp"
android:text="22.00H - 04.30H"
android:textColor="@color/dk"
android:textSize="10dp" />
</RelativeLayout>
主要活动......(更改值的方法)
public void initializeEvent() {
Fragment fr = new EventDetail();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
View v = fr.getView();
//get fragment elements.
fragmentTransaction.replace(R.id.fragment_menu, fr);
fragmentTransaction.commit();
}
如何从此片段中获取textviews和imageview以设置其图像和文本?
答案 0 :(得分:1)
这样的事情?
((EventDetail)getActivity()).function(data);
答案 1 :(得分:0)
您不应直接通过Activity或Fragment方法设置任何值。
从活动到片段,
setArguments(bundle)
,在片段中,您可以稍后通过getArguments()
从片段到活动,
Fragment
传递到Activity