Android Java 使用cardview来显示gridview的项目
Posted
技术标签:
【中文标题】Android Java 使用cardview来显示gridview的项目【英文标题】:Android Java Use a cardview to display the items of a gridview 【发布时间】:2021-09-24 15:52:52 【问题描述】:大家好,我想用GridView
显示一些显示字符串数组内容的项目。这是我要设置为项目的CardView
:
所以当我点击一个按钮时,选择电影类型的布局变得可见,我设置了一个适配器:
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);
但这就是我得到的:
我想放大这个布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="30dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
>
<RelativeLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/type"
android:layout_
android:layout_
android:text="Type"
android:layout_centerInParent="true"
android:textSize="20dp"
android:textStyle="bold"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
要得到这样的东西:
任何人都可以帮我弄清楚如何扩展所需的布局吗?
【问题讨论】:
【参考方案1】:为了使用自定义项布局,您需要创建一个从BaseAdapter
或ArrayAdapter
扩展的自定义适配器
然后在 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;
并将其用作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());
);
我不得不操纵边距和关心的宽度,并用 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_
android:layout_
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/type"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:text="Type: 1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
结果:
【讨论】:
感谢您的回复,稍后我会尝试您的代码,如果它解决了我的问题,请告诉您以上是关于Android Java 使用cardview来显示gridview的项目的主要内容,如果未能解决你的问题,请参考以下文章
如何在recyclerview中为cardview android设置带有int数组的onclicklistener