2014-02-21 129 views
0

如何獲取我的活動中片段的元素並更改其值?android從片段獲取imageview

喜有此片段...

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&amp;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和imageviews來設置他們的圖像和文本?

+0

它是片段的佈局,您應該從片段內設置它。你想從活動中完成的特殊原因? – Sharj

+0

我在我的活動中有一個列表,當我點擊一個項目時,它會顯示一些關於該項目的數據片段。所以這些數據應該是動態的。 –

回答

1

這樣的事情?

((EventDetail)getActivity()).function(data); 
+0

我認爲這適用於片段活動 –

+0

試試這個然後http://stackoverflow.com/questions/18486952/get-id-of-textview-in-fragment-from-fragmentactivity-in-viewpager – morty346

0

你不應該直接通過Activity或Fragment方法設置任何值。

從活動到碎片,

  • 你應該在其中您可以通過setArguments(bundle)發送 捆添加適當的值,並在片段可以在以後通過

從片段retrive這些值活動,

  • 您應該創建一個接口並使用其方法來傳遞從FragmentActivity的值