简体   繁体   中英

GridView is Showing only one row

I am having issue in showing the list items of the GridView in Android. It is showing only one row with two column. I know this is the issue of the layout but I am not able to figure out, where I am doing wrong?

  <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/hire_relative_contact_align_bottom"
            android:layout_below="@+id/book_homestay_image_linear"
            android:scrollbars="vertical">

            <LinearLayout
                android:id="@+id/linear_book_homestay_main_scrollable"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/book_homestay_image_linear"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="Basic Detail"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />


                    <com.bluejamesbond.text.DocumentView

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp"
                        app:documentView_textColor="#333"
                        app:documentView_textFormat="plain"
                        app:documentView_textSize="12sp"
                        ext:documentView_textAlignment="justified" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_marginRight="4dp"
                        android:layout_weight="1">

                        <ImageView
                            android:id="@+id/homestay_image_basic_detail"
                            android:layout_width="120dp"
                            android:layout_height="match_parent"
                            android:layout_alignParentRight="true"
                            android:scaleType="fitXY" />


                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="4dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:text="Host"
                            android:textAllCaps="false"
                            android:textColor="#555"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/homestay_textHost"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_marginBottom="3dp"
                            android:layout_weight="1"
                            android:text=""
                            android:textAllCaps="true"
                            android:textColor="#555"
                            android:textSize="10sp"
                            android:textStyle="normal" />


                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:text="Address"
                            android:textAllCaps="false"
                            android:textColor="#555"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/homestay_textAddress"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_marginBottom="3dp"
                            android:layout_weight="1"
                            android:text=""
                            android:textAllCaps="true"
                            android:textColor="#555"
                            android:textSize="10sp"

                            android:textStyle="normal" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:text="Phone"
                            android:textAllCaps="false"
                            android:textColor="#555"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/homestay_textPhone"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_marginBottom="3dp"
                            android:layout_weight="1"
                            android:text=""
                            android:textAllCaps="true"
                            android:textColor="#555"
                            android:textSize="10sp"
                            android:textStyle="normal" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:text="Email"
                            android:textAllCaps="false"
                            android:textColor="#555"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/homestay_textEmail"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:text=""
                            android:textAllCaps="true"
                            android:textColor="#555"
                            android:textSize="10sp"
                            android:textStyle="normal" />


                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="Description"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />


                    <com.bluejamesbond.text.DocumentView
                        android:id="@+id/homestay_textDescriptionjustify"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp"
                        app:documentView_textColor="#333"
                        app:documentView_textFormat="plain"
                        app:documentView_textSize="12sp"
                        ext:documentView_textAlignment="justified" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="Facilities"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />


                    <GridView
                        android:id="@+id/book_homestay_gridView_facilities"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="30dp"


                        android:horizontalSpacing="5dp"
                        android:numColumns="2"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="5dp" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="Meals"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />

                    <GridView
                        android:id="@+id/book_homestay_gridViewMeal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="30dp"
                        android:horizontalSpacing="5dp"
                        android:numColumns="2"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="5dp" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="Area Facilities"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />

                    <GridView
                        android:id="@+id/book_homestay_gridViewAreaFacilities"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="30dp"
                        android:horizontalSpacing="5dp"
                        android:numColumns="2"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="5dp" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="Rooms"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />

                    <GridView
                        android:id="@+id/book_homestay_gridViewRooms"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="30dp"
                        android:horizontalSpacing="5dp"
                        android:numColumns="2"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="5dp" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="130dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2"
                        android:gravity="center"
                        android:text="House Rules"
                        android:textAllCaps="true"
                        android:textColor="#FFFFFF"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="200dp"
                        android:layout_height="1dp"
                        android:layout_gravity="center"
                        android:background="#5EB6C2" />

                    <GridView
                        android:id="@+id/book_homestay_gridViewHouseRules"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="30dp"
                        android:horizontalSpacing="5dp"
                        android:numColumns="2"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="5dp" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="10dp"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/textViewRateAndReview"
                        android:layout_width="150dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="5dp"
                        android:background="@drawable/rounded_edittext"
                        android:gravity="center"
                        android:text="@string/nameHere"
                        android:textAllCaps="true"
                        android:textColor="#4FA8B5"
                        android:textStyle="bold" />


                </LinearLayout>

            </LinearLayout>
        </ScrollView>

items_xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageViewTickMark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/textViewFacilities"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="center"
        android:layout_gravity="center"/>

</LinearLayout>

image I want to get

在此处输入图片说明

image I am getting

在此处输入图片说明

This is a layout issue, I know that but I'm not able to solve this issue. How can this issue be solved?

Try this following code,

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter .ViewHolder> { 


Context mContext; 
ArrayList<BookHomeStayFacilitiesPOJO> class_book_homestay_facilities = new ArrayList<>(); 
String TAG = "HomeTab_adapter"; 


public RecyclerViewAdapter(Context mContext, ArrayList<BookHomeStayFacilitiesPOJO> class_book_homestay_facilities) { 

this.mContext = mContext; 
this.class_book_homestay_facilities = class_book_homestay_facilities; 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 
public TextView category; 
public ImageView imageViewCheckbox; 


public ViewHolder(View v) { 
super(v); 
category = (TextView) v.findViewById(R.id.textViewFacilities); 
imageViewCheckbox = (ImageView) v.findViewById(R.id.imageViewTickMark); 
} 
} 


@Override 
public int getItemCount() { 

System.out.println("SIze)" + class_book_homestay_facilities.size()); 
return class_book_homestay_facilities.size(); 
} 


@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_homestay_grid_item_facilities, parent, false); 
RecyclerView.ViewHolder viewHolder = new ViewHolder(view); 
return viewHolder; 
} 

@Override 
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { 


class_book_homestay_facilities.get(position).getCategory(); 
class_book_homestay_facilities.get(position).getImage(); 
for (int i = 0; i < class_book_homestay_facilities.size(); i++) { 
System.out.println("System" + class_book_homestay_facilities.get(i).getCategory()); 
} 
// holder.Category.setText(class_book_homestay_facilities.get(position).getCategory()); 


} 


}

Create Two column in RecyclerView refer here

I hope this may help you.

It is useful to be used RecycleView with GridLayoutManager instead of GridView .

Because GridView have scrolling and you already put ScrollView as parent View.

RecycleView is came to overcome this situations , where you can use recycleView inside a scrollview using NestedScrollView .

You have plenty of solutions to solve this problem.

1. Use custom GridView . Custom GridView

2. Use RecycleView With NestedScrollView .

3. Use ExpandlabeView . Link for expandable view

In My opinion recycleView is best options.

Use this Example:-

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:textColor="#ff0000"
            android:textSize="18sp"
            android:layout_marginBottom="10dp"
            android:text="All History" />


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/size_30">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:text="Recent History" />

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/textView4">

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

            </android.support.v4.widget.NestedScrollView>
        </RelativeLayout>

    </LinearLayout>


</ScrollView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM