2016-09-11 2 views
0

주 활동에 음식 목록이 있고 각 음식에 확인란이있는 앱을 만들려고합니다. 이 RecyclerView + CardView 예제가 작동하지 않는 이유는 무엇입니까?

나는 전체 텍스트와 체크 박스 일을 수행하기 위해이 튜토리얼을 따라 : http://android-pratap.blogspot.co.il/2015/01/recyclerview-with-checkbox-example.html

을하지만 응용 프로그램을 실행할 때마다 그냥 메인 화면에 RecyclerView을 표시하지 않습니다 (응용 프로그램은 실행됩니다 하지만 RecyclerView는 거기에 없습니다.)

누군가 내 코드를 어떻게 바꿀 수 있는지 설명 할 수 있으면 멋진 카드를 필요로하지 않을 것입니다. 여기에 내 코드입니다 :

MainAcitivity.java :

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 (그냥 FoodAdapter와 RecyclerView 결합) :

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> 

list.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> 

답변

1

RecyclerViewandroid:layout_height="match_parent"android:layout_height="0dp"에서 높이를 설정하십시오.

0dp 높이가 LinearLayout에 사용 된 경우 사용자의 크기는 RelativeLayout입니다.

+0

와우 ... 나는 그걸 알아 채지 못했다고는 생각하지 않는다. CardView없이 이것을하는 것은 어떨까? list.xml에서 CardView를 제거하고 RelativeLayout을 수평 LinearLayout으로 변경할 수 있습니까? –

+0

그래,'list.xml'에서'CardView'를 제거 할 수 있습니다. 그냥 위젯 일 뿐이야. 당신은 당신이 좋아하는 것을 무엇이든 할 수 있습니다. 이것이 당신이 배우는 방법입니다. 건배! –

관련 문제