1

我知道這個問題有幾個問題,因爲我已經搜索了一個小時,但這兩個都沒有解決我的問題,也沒有解決方案太舊。在同一個可滾動佈局/片段中添加多個GridView

我正在開發一個活動應用程序,它顯示出去的人的出席人數。它需要有兩個GridView:YES和NO,取決於出席情況,並顯示客人的照片。

我已經制作了「YES」GridView,並且嘗試向我的佈局中添加一個新的TextView和GridView,並在Java中爲我的「NO」GridView 獲取佈局,但它只顯示第一個。

我在做什麼錯?我爲每個選項卡使用Fragments。

這就是我所訴v.s.我想:

This is my app, just showing one GridView: enter image description here

這是我的片段佈局:

[fragment_one.xml] 

<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:orientation="vertical" 
tools:context="com.example.example.MainActivity.OneFragment"> 

<TextView android:id="@+id/title_yes" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="14dp" 
android:text="Yes" 
android:textAllCaps="true" 
android:textSize="14sp" 
android:fontFamily="sans-serif-regular" /> 

<GridView android:id="@+id/gridview_yes" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/title_yes" 
    android:numColumns="4" 
    android:verticalSpacing="2dp" 
    android:horizontalSpacing="2dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" /> 

<TextView android:id="@+id/title_no" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="14dp" 
    android:text="No" 
    android:textAllCaps="true" 
    android:textSize="14sp" 
    android:fontFamily="sans-serif-regular" /> 

<GridView android:id="@+id/gridview_no" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/title_no" 
    android:numColumns="4" 
    android:verticalSpacing="2dp" 
    android:horizontalSpacing="2dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" /> 

</LinearLayout> 

這是我的片段的Java:

public class OneFragment extends Fragment { 

    public OneFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_one, container, false); 
     GridView gridViewYes = (GridView) view.findViewById(R.id.gridview_yes); 
     GridView gridViewNo = (GridView) view.findViewById(R.id.gridview_no); 
     gridViewYes.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity() 
     gridViewNo.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity() 

    gridViewYes.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(getActivity(), "Esta es la imagen " + position + ".", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    return view; 
    } 

} 
+0

使用RecyclerView與佈局管理器這樣https://github.com/TonicArtos/SuperSLiM,是真正有用的 –

回答

1

的GridView可能不是在這裏你最好的選擇。該小部件旨在用於在滾動容器中顯示可能長的事物列表時使用。當您擁有一組不需要自己的滾動行爲的可管理項目時,TableView更好。

此外,您可能不希望match_parent的layout_height用於這些事情。使用wrap_content只能讓它們像內容所示那樣高。

如果因爲不確定整個高度而需要整個事物滾動,請將LinearLayout放置在ScrollView中,讓它確定如果表和文本集合是否可滾動(如果它們太高)分配的空間。

+0

感謝您的建議道格!我解決了我的問題,改變GridView的layout_height,如果我將它作爲GridView而不改變爲TableLayout,會對我的應用產生什麼影響?你爲什麼說這樣比較好? –

+0

堆疊可自行滾動的多個組件(如GridView)可能會導致UI無法使用,如果它們變高。也許它現在適用於特定的數據集,但如果您獲得更多數據並且所有數據都比可用空間高,它可能在未來運行良好。 –

+0

道格最後兩件事。 1.即使我使用ScrollView將我的LinearLayout與兩個GridView嵌套,我是否還需要TableLayout,否則如果GridView變得太大,仍然會出現問題。 2.你怎麼看待使用不同的佈局,如GridLayout或多個LinearLayout? –

1

你的問題是你已經設置GridView是高度match_parent。

android:layout_height="match_parent" 

您應該更改如下:

android:layout_height="wrap_content" 
+0

簡單但有用,地獄,非常感謝你先生,這是我的問題! –

+0

很高興提供幫助。接受它作爲答案,以幫助未來有同樣問題的人。 –

相關問題