gpt4 book ai didi

android - 运行应用程序时我的 cardView 布局未显示

转载 作者:行者123 更新时间:2023-11-30 00:44:35 25 4
gpt4 key购买 nike

你好,所以我创建了一个简单的应用程序,它使用 cardview 和 recycler View 来显示图像。

现在,当我运行该应用程序时,只会显示一个带有应用程序名称的空白应用程序

这是代码

主要 Activity 文件

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
List<listItem> item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = (RecyclerView) findViewById(R.id.recycler_Container);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myAdapter adapter = new myAdapter(dataFile.getListData());
recyclerView.setAdapter(adapter);



}}

适配器文件

public class myAdapter extends RecyclerView.Adapter<myAdapter.pageHolder> {

List<listItem> listData;
LayoutInflater inflater;

public myAdapter(List<listItem> listData) {
this.listData = listData;
}

public class pageHolder extends RecyclerView.ViewHolder{

ImageView image;
TextView text;
View container;

public pageHolder(View itemView) {
super(itemView);

image = (ImageView) itemView.findViewById(R.id.thumbnail);
text = (TextView) itemView.findViewById(R.id.card_text);
container = itemView.findViewById(R.id.layout_container);

}}

@Override
public myAdapter.pageHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
return new pageHolder(view);
}

@Override
public void onBindViewHolder(myAdapter.pageHolder holder, int position) {

listItem item =listData.get(position);
holder.text.setText(item.getTitle());
holder.image.setImageResource(item.getIconId());

}

@Override
public int getItemCount() {
return listData.size();
}}

列表项文件

public class listItem {

String title;
int iconId;

public listItem(int iconId, String title) {
this.iconId = iconId;
this.title = title;
}

public int getIconId() {
return iconId;
}

public void setIconId(int iconId) {
this.iconId = iconId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}}

数据文件

public class dataFile {


public static int icons[]={R.drawable.album9,R.drawable.album1,R.drawable.album3,R.drawable.album4,R.drawable.album5,R.drawable.album6,
R.drawable.album7,R.drawable.album8,R.drawable.album10,R.drawable.album11,R.drawable.album12,R.drawable.album13,R.drawable.album14,R.drawable.album15,
R.drawable.album16};

public static String title[] = {"Album"};


public static List<listItem> getListData(){

List<listItem> data = new ArrayList<>();
for(int i =0;i<icons.length;i++){
new listItem(icons[i],"ALBUM"+i);
}
return data;
}}

主 XML 文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adi.customcardview.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="16dp"
android:id="@+id/recycler_Container"/></RelativeLayout>

enter image description here

卡片布局 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout_container"
app:cardCornerRadius="5dp"
app:cardBackgroundColor="#efdada"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/thumbnail"
android:padding="12dp"
android:src="@drawable/album9"/>
<TextView
android:text="ALBUM NAME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:id="@+id/card_text"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/thumbnail"/>
</RelativeLayout></android.support.v7.widget.CardView></LinearLayout>

最佳答案

据我了解,在 getListData 方法中,您没有将 listItem 元素添加到数据列表中。于是尝试改方法如下:

public static List<listItem> getListData(){

List<listItem> data = new ArrayList<>();
for(int i =0;i<icons.length;i++){
data.add(new listItem(icons[i],"ALBUM"+i));
}
return data;
}

关于android - 运行应用程序时我的 cardView 布局未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42001019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com