GridView只显示一行

时间:2017-09-01 04:35:50

标签: android android-layout android-gridview

我在Android中显示GridView的列表项时遇到了问题。它只显示一行有两列。我知道这是布局的问题,但我无法弄清楚,我做错了什么?

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

想要的图片

enter image description here

图片我正在

enter image description here

这是一个布局问题,我知道但是我无法解决这个问题。如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

请尝试以下代码,

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()); 


} 


}

在RecyclerView refer here

中创建两列

我希望这对你有所帮助。

答案 1 :(得分:1)

使用RecycleViewGridLayoutManager代替GridView非常有用。

因为GridView已滚动,您已将ScrollView作为父视图。

RecycleView来克服这种情况,您可以使用recycleViewscrollview内使用NestedScrollView

您有很多解决方案可以解决这个问题。

<强> 1。使用自定义GridViewCustom GridView

<强> 2。使用RecycleView。{/ 1}}

第3。使用NestedScrollView Link for expandable view

在我看来,ExpandlabeView是最好的选择。

使用此示例: -

recycleView