RecyclerView,它应该显示cardViews并没有显示任何东西

时间:2017-09-17 13:26:46

标签: java android android-recyclerview recycler-adapter android-viewholder

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

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

1 个答案:

答案 0 :(得分:0)

到目前为止,我还不知道问题的原因,但是我将适配器和视图持有者都移动到Recipes Fragment作为内部类,这解决了这个问题。