2

我有一個GridView充滿了圖像,當你點擊其中一個圖像時,它開始了一個detais活動。
這一切工作正常,但現在我想做一個主 - 佈局佈局。所以,我創建了「佈局,土地」文件夾,而不是隻有一個gridview,它是這樣的:如何在屏幕上顯示的兩個片段之間傳遞數據?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/background" 
tools:context=".MainActivityFragment"> 

<GridView 
    android:id="@+id/main_grid" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:numColumns="auto_fit" /> 

<fragment 
    android:id="@+id/fragment" 
    android:name="com.example.lucas.popularmovies.DetailActivityFragment" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    tools:layout="@layout/fragment_detail" /> 

</LinearLayout> 


在此之前,我經過詳細信息的數據作爲額外的意圖和檢索它片段。
但是,當我在同一個屏幕上顯示所有內容時,如何在不啓動新活動的情況下單擊圖像時以更新詳細信息的方式傳遞數據?謝謝。

+2

的[與其它片段進行通信(http://developer.android.com/training/basics/fragments/communicating.html)文檔是我發現主從片段的最簡單的例子。 –

回答

1

簡單的片段。

DetailActivityFragment fragment= (DetailActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment); 
    if (fragment != null) { 
     fragment.updateImage(url); 
    } 
-1

我發佈了另一個SO鏈接的完整答案@Passing data between fragments contained in an activity。只需找到我的用戶名,這是該帖子的唯一答案。讓我們知道你的進步。

+1

如果這是重複的,你應該按照這樣的方式進行投票,而不是發佈僅指向其他帖子的僅鏈接答案。 –

+0

@MikeM。我下次再考慮一下。我不願意投任何職位爲重複職位,然後可能會被其他人投票結案。我記得有一次,我實際上不同意別人在另一篇文章中的看法。 –

0

既然你已經在佈局一個靜態的片段,我想你不會刪除,我會說使用一個簡單的邏輯是這樣的:

在你的活動中,創建將更新您的靜態fragment等的方法:

public void updateImage(String imageUrl) { 
    DetailActivityFragment fragment= (DetailActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment); 
    if (fragment != null) { 
     fragment.updateImage(imageUrl); 
    } 
} 

無論何時單擊圖像,都會調用此方法。

並在你的片段中加載你的圖片在你的ImageView(你應該有一個)。

0

按方格使用EventBus。

創建總線對象和活動進行註冊

Bus bus = new Bus(); 
bus.register(this); 

與參數作爲模型創建活動public方法。

@Subscribe 
public void getDataFromGrid(MainGridItem item) { 
    // TODO: React to the event somehow! 
} 

GridViewonItemClick發佈項目 -

bus.post(mainGridItems.get(position)); 
0

你可以找到使用

Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentLoadingSpace) 
if(fragment instanceof Fragment1){ 
    ((Fragment1) fragment).updateSelectedObjec(Objects object); 
} 
相關問題