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

我不知道問題的原因到現在,不過我感動適配器和視圖持有人的食譜片段作爲內部類,這也解決了這一問題。