1

我有一個應用程序,看起來是這樣的:嵌套SupportMapFragment內Recyclerview

app

這是一個recyclerview和recyclerview內的片段。

的recyclerview XML是:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="match_parent" 
    android:id="@+id/main_recyclerview_holder"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/main_recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 

    </RelativeLayout> 

稱爲backgroundRlMaps的recyclerview的每行內,它位於該片段佈局XML顯示如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/backgroundRlMaps" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="BACK"/> 

     <other views...> 

</RelativeLayout> 

你看到的視圖,首先用一個重疊用一個按鈕進行簡單視圖,並且一旦用戶點擊了按鈕,視圖顯示以顯示顯示地圖的backgroundRlMaps視圖。例如,回收者視圖中的每個項目將是不同的國家(中國,印度,巴基斯坦等),每次單擊地圖按鈕時,該片段將顯示適用的地圖。

我已經粘貼了以下邏輯爲mapbutton:

((ViewHolder) holder).mapButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     //bringing the fragment container called backgroundRlMaps to the front 
     ((ViewHolder) holder).backgroundRlMaps.bringToFront(); 
     //Dealing with maps here 
     //A new supportMapFragment is formed every time 
     SupportMapFragment mapFragment = SupportMapFragment.newInstance(); 
     mapFragment.getMapAsync(RecyclerViewAdapter.this); 
     //Adding the map to the relativeLayout fragment container 
     f.getChildFragmentManager().beginTransaction().add(((ViewHolder) holder).backgroundRlMaps.getId(), mapFragment, String.valueOf(position)).commit(); 
     f.getChildFragmentManager().executePendingTransactions(); 
     } 
    } 

然而,每次我點擊地圖按鈕,只有最上面的片段backgroundRlMaps示出了地圖和我可以看到,頂部片段刷新其每次點擊其他國家的地圖按鈕時查看。 另外兩個,如在屏幕上,繼續顯示沒有。

有沒有辦法讓這個工作正常?

我懷疑由於backgroundRlMaps在所有片段中都是重複的,所以當我要求childFragmentManager添加到SupportMapFragment時,它會選擇帶有該佈局引用的第一個片段以添加supportMapFragment。

回答

-1

問題是您的RecyclerView中的每個項目都是Fragment。 無法將Fragment設置爲ListViewRecyclerView項目。

查看更多信息here

+0

我不知道,將工作作爲從recyclerview基本視圖只想改變到ListView,但碎片還是要在列表視圖等的每一行我會回到同樣的問題。 – Simon

+0

@Simon我的答案中的主要事情是不可能將Fragment作爲ListView項目! – Oleksandr