0

我有一个recyclerView必须显示卡的列表,但它不! 有一个食谱数组列表传递给适配器以显示它们作为cardviews,调试中的所有内容似乎都没问题,但它不会在屏幕上显示任何内容。 ViewHolder:RecyclerView应该显示cardViews不显示任何东西

import android.support.v7.widget.RecyclerView; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.example.nd.ameer.bake.R; 

public class RecipeViewHolder extends RecyclerView.ViewHolder { 

    public TextView titleTextView; 
    public ImageView coverImageView; 

    public RecipeViewHolder(View v) { 
     super(v); 
     titleTextView = (TextView) v.findViewById(R.id.titleTextView); 
     coverImageView = (ImageView) v.findViewById(R.id.recipeImageView); 
    } 
} 

适配器:

import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.bumptech.glide.Glide; 
import com.example.nd.ameer.bake.R; 
import com.example.nd.ameer.bake.models.Recipe; 
import com.example.nd.ameer.bake.views.holders.RecipeViewHolder; 

import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

public class RecipeAdapter extends 

RecyclerView.Adapter<RecipeViewHolder> { 


    ArrayList<Recipe> recipes = new ArrayList<>(); 
    Context context; 

    public RecipeAdapter(ArrayList<Recipe> recipes, Context context) { 
     this.recipes = recipes; 
     this.context = context; 
    } 

    @Override 
    public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(context).inflate(R.layout.recipe_item, parent, false); 
     return new RecipeViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(RecipeViewHolder holder, int position) { 

     holder.coverImageView.setImageResource(R.color.colorPrimaryLight); 
     holder.titleTextView.setText(recipes.get(position).getName()); 
    } 

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

片段onCreateView:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    rootView = inflater.inflate(R.layout.fragment_recipe, container, false); 

    createRecipes(); 

    recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recipe); 
    recyclerView.setHasFixedSize(true); 
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    if (recipes.size() > 0 & recyclerView != null) { 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setAdapter(new RecipeAdapter(recipes, getContext())); 
    } 
    return rootView; 
} 

方法createRecipes();创建一个配方列表,然后它传递给适配器。

as you can see, the recipes size is 4, so it's not the problem

+0

你确定'recipes'列表的大小不是0吗? –

+0

是的它总是4,相同的列表 –

+0

你可以发布你的createRecipes()代码和logcat日志 –

回答

0

我不知道问题的原因到现在,不过我感动适配器和视图持有人的食谱片段作为内部类,这也解决了这一问题。