简体   繁体   English

Android Java 使用 cardview 来显示 gridview 的项目

[英]Android Java Use a cardview to display the items of a gridview

Hi everyone I'm trying to show with a GridView some items that show the contents of an array of strings.大家好,我正在尝试用GridView显示一些显示字符串数组内容的项目。 This is the CardView I want to set as item:这是我想设置为项目的CardView

在此处输入图片说明

So when I click on a button, the layout of the choice of the type of film becomes visible and I set an adapter:所以当我点击一个按钮时,电影类型选择的布局变得可见,我设置了一个适配器:

cTBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postaCardVIew.setVisibility(View.GONE);
                cT.setVisibility(View.VISIBLE);

                GridView simpleGridView = findViewById(R.id.simpleGridView);

                String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza","Fantasy",
                        "Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
                };

                final ArrayAdapter<String> adapter = new ArrayAdapter<String> (posta.this,android.R.layout.simple_list_item_1, genrelist);
                simpleGridView.setAdapter(adapter);
}

But this is what I am getting:但这就是我得到的:

在此处输入图片说明

I want to inflate this layout though:我想夸大这个布局:

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

    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="150dp"
        android:layout_height="150dp"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:cardCornerRadius="30dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <TextView
            android:id="@+id/type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type"
            android:layout_centerInParent="true"
            android:textSize="20dp"
            android:textStyle="bold"
            />
    
        </RelativeLayout>
    
    
</androidx.cardview.widget.CardView>

To get such a thing:要得到这样的东西:

在此处输入图片说明

Can anyone help me figure out how I could inflate the desired layout?谁能帮我弄清楚如何膨胀所需的布局?

In order to use a custom item layout, you need to create a custom adapter that extends from the BaseAdapter or ArrayAdapter为了使用自定义项目布局,您需要创建一个从BaseAdapterArrayAdapter扩展的自定义适配器

Then inflate and build your item layout in the getView() method:然后在getView()方法中膨胀并构建您的项目布局:


public class CustomAdapter extends BaseAdapter {

    String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza", "Fantasy",
            "Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
    };


    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            holder = new ViewHolder();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title = convertView.findViewById(R.id.type);
        holder.title.setText(genrelist[position]);
        return convertView;
    }

    static class ViewHolder {
        TextView title;
    }



    public int getCount() {
        return genrelist.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
}

And use it as the GridView adapter:并将其用作GridView适配器:

cTBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        postaCardVIew.setVisibility(View.GONE);
        cT.setVisibility(View.VISIBLE);

        GridView simpleGridView = findViewById(R.id.simpleGridView);

        simpleGridView.setAdapter(new CustomAdapter());
    }
});

I had to manipulate the margin and cared width and wrap the CardView with a FrameLayout in order to force the margin among cards:我必须操纵边距和关心的宽度,并用FrameLayout包裹CardView以强制卡片之间的边距:

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="4dp"
    android:layout_marginVertical="4dp">

    <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="120dp"
        android:layout_height="120dp"
        app:cardCornerRadius="30dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Type: 1"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </androidx.cardview.widget.CardView>
</FrameLayout>

Result:结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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