ScrollView不适用于Android Studio中的GridLayout

时间:2019-03-02 22:10:19

标签: android scrollview android-gridlayout

嗨,我想创建一个响应式可滚动网格。在搜索时,我遇到了一些代码。我调整了此代码以允许滚动网格。该代码使用cardViews创建2列网格和4行。 cardViews缩小以允许更多卡进入视口而不是滚动。愿意接受任何想法和替代方法来完成这项工作。谢谢。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:scrollbars="vertical"
    android:fillViewport="true">

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

        <RelativeLayout
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <TextView
                android:id="@+id/textGrid"
                android:text="Games"
                android:textSize="34sp"
                android:textColor="@android:color/black"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>

        <android.support.v7.widget.GridLayout
            android:id="@+id/gridView"
            android:layout_weight="8"
            app:columnCount="2"
            app:rowCount="2"
            android:padding="14dp"
            app:alignmentMode="alignMargins"
            app:columnOrderPreserved="false"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            >

            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/sampleimage" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Grid One
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

        </android.support.v7.widget.GridLayout>

    </LinearLayout>

</ScrollView>

2 个答案:

答案 0 :(得分:0)

我的一个应用中有一个类似的模式。票证已添加到3格宽的网格中,您可以滚动网格。这是我使用的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/your_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical">
        <ScrollView
            android:id="@+id/your_scrollview"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="2dip"
            android:layout_gravity="center_horizontal">
            <LinearLayout
                android:id="@+id/your_grid"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="visible">
                <GridView
                    android:id="@+id/gridViewTickets"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:numColumns="3"
                    android:padding="8dp"
                    android:horizontalSpacing="8dp"
                    android:verticalSpacing="8dp"
                    android:minHeight="250dip"
                    android:gravity="center_horizontal"
                    android:layout_gravity="center_horizontal">
                </GridView>
            </LinearLayout>
        </ScrollView
</LinearLayout>

答案 1 :(得分:0)

经过大量研究,我发现了RecyclerView。这有点令人困惑,但是效果很好。我必须在MainActivity.java中使用两(2)个布局(activity_main.xml和row_data.xml)。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">

    <GridView
        android:id="@+id/gridview"
        android:numColumns="2"
        android:verticalSpacing="1dp"
        android:background="#e5e5e5"
        android:horizontalSpacing="1dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </GridView>

</RelativeLayout>

row_data.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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.support.v7.widget.CardView
        android:id="@+id/gridviewdata"
        android:layout_margin="12dp"
        app:cardCornerRadius="12dp"
        app:cardElevation="6dp"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        >

        <LinearLayout
            android:orientation="vertical"
            android:padding="16dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/images"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/apple" />

            <TextView
                android:id="@+id/fruits"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="Apple something"
                android:textStyle="normal|italic"
                android:textSize="25dp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

MainActivity.java

package com.example.gridrecycler;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    GridView gridView;

    String[] fruitNames = {"Apple","Orange","strawberry","Melon","Kiwi","Banana"};
    int[] fruitImages = {R.drawable.apple,R.drawable.oranges,R.drawable.strawberry,R.drawable.watermelon,R.drawable.kiwi,R.drawable.banana};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //finding listview
        gridView = findViewById(R.id.gridview);

        CustomAdapter customAdapter = new CustomAdapter();
        gridView.setAdapter(customAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(getApplicationContext(),fruitNames[i],Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getApplicationContext(),GridItemActivity.class);
                intent.putExtra("name",fruitNames[i]);
                intent.putExtra("image",fruitImages[i]);
                startActivity(intent);
            }
        });


    }

    private class CustomAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return fruitImages.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
            //getting view in row_data
            TextView name = view1.findViewById(R.id.fruits);
            ImageView image = view1.findViewById(R.id.images);

            name.setText(fruitNames[i]);
            image.setImageResource(fruitImages[i]);
            return view1;
        }
    }
}

我看着diff tuts提出了这个建议。现在,我怀疑它是否符合最佳实践。谢谢大家的建议。