gpt4 book ai didi

java - 为什么这个 RecyclerView + CardView 示例不起作用?

转载 作者:行者123 更新时间:2023-12-01 09:30:39 26 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,其中包含主要 Activity 的食物列表以及每种食物的复选框。

我按照本教程完成了整个文本和复选框的操作:http://android-pratap.blogspot.co.il/2015/01/recyclerview-with-checkbox-example.html

但是每当我运行应用程序时,它都不会在主屏幕上显示 RecyclerView...(应用程序将运行,但 RecyclerView 不存在)

如果有人可以解释我如何更改我的代码,这样就不需要卡片,那就太棒了。这是我的代码:

MainAcitivity.java:(只是将RecyclerView与FoodAdapter结合起来)

package com.gregskl.foodreminder;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private RecyclerView recycler;
private RecyclerView.Adapter adapter;

private List<Food> foods;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

foods = new ArrayList<>();
foods.add(new Food("Grapes", true));
foods.add(new Food("Oranges", true));

recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));
adapter = new FoodAdapater(foods);
recycler.setAdapter(adapter);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

FoodAdapter.java:

package com.gregskl.foodreminder;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
* Created by Gregory on 11-Sep-16.
*/
public class FoodAdapater extends RecyclerView.Adapter<FoodAdapater.ViewHolder> {

private List<Food> foods;

public FoodAdapater(List<Food> foods) {
this.foods = foods;
}

@Override
public FoodAdapater.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, null);
return new ViewHolder(itemLayoutView);
}

@Override
public void onBindViewHolder(FoodAdapater.ViewHolder holder, int position) {
ViewHolder h = (ViewHolder) holder;
h.text.setText(foods.get(position).getText());
h.checkbox.setChecked(foods.get(position).isAvailable());
h.checkbox.setTag(foods.get(position));
h.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(), "Hey", Toast.LENGTH_LONG).show();
}
});
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView text;
public CheckBox checkbox;

public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
}
}
}

Food.java:(食物的数据模型)

package com.gregskl.foodreminder;

/**
* Created by Gregory on 11-Sep-16.
*/
public class Food {

private String text;
private boolean available;

public Food(String text, boolean available) {
this.text = text;
this.available = available;
}

public String getText() {
return text;
}

public boolean isAvailable() {
return available;
}

public void setText(String text) {
this.text = text;
}

public void setAvailable(boolean available) {
this.available = available;
}
}

content_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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.gregskl.foodreminder.MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:scrollbars="vertical" />

</RelativeLayout>

列表.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"

card_view:cardUseCompatPadding="true" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>

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

最佳答案

尝试在 RecyclerView 中将高度从 android:layout_height="0dp" 设置为 android:layout_height="match_parent"

0dp高度用于LinearLayout,你的是RelativeLayout

关于java - 为什么这个 RecyclerView + CardView 示例不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39440847/

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