Alex Ardavin
Alex Ardavin

Reputation: 345

Add multiple GridView in the same scrollable layout/fragment

I know there are several questions for this problem already as I have searched for an hour, but neither of those has solved my problem nor the solution is too old.

I'm working on an event app and it displays the attendance of people going to it. It need to has two GridView: YES and NO, depending on the attendance, and it shows the picture of the guests.

I have already made the "YES" GridView and I tried adding a new TextView and GridView to my layout + getting the layout of it in Java for my "NO" GridView but it only shows the first one.

What am I doing wrong? I'm using Fragments for each of my tabs.

This is what I have v.s. what I want:

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

This is my fragment's layout:

[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>

This is my fragment's 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;
    }

}

Upvotes: 1

Views: 2315

Answers (2)

Lazy Ninja
Lazy Ninja

Reputation: 22527

Your problem is you have set GridView yes height to match_parent.

android:layout_height="match_parent"

You should change as follows:

android:layout_height="wrap_content"

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317467

GridView is probably not your best option here. That widget is intended to be used when you have a potentially long list of things to display in a scrolling container. TableView is better when you have a manageable set of items that doesn't need its own scrolling behavior.

Also, you probably don't want a layout_height of match_parent for these things. Use wrap_content to only let them be as tall as their content suggests.

If you need the whole thing to scroll because you aren't certain of the entire height, place the LinearLayout in a ScrollView and let that determine if the collection of tables and text should be scrollable if they're too tall for the space allotted.

Upvotes: 1

Related Questions