RecyclerView在加载时不显示任何项目视图

时间:2018-09-17 11:42:00

标签: android android-recyclerview retrofit2 recycler-adapter recyclerview-layout

我正在检索JSON数据并通过Retrofit对其进行解析。我想在RecyclerView上显示它,但不能使其显示视图。我在哪里做错了?这里给出了MainActivity,Model和ListFlowerAdapter

public class MainActivity extends AppCompatActivity {

    private ListFlowerAdapter mListFlowerAdapter;
    private RecyclerView mRecyclerView;
    private RetrofitClient mRetrofitClient;
    LinearLayoutManager linearLayoutManager;

    //private List<FlowerModel> myFlowers;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        configViews();

        mRetrofitClient = new RetrofitClient();
        Call<List<FlowerModel>> listCall = mRetrofitClient.getFlowerService().getAllFlowers();
        listCall.enqueue(new Callback<List<FlowerModel>>() {
            @Override
            public void onResponse(Call<List<FlowerModel>> call, Response<List<FlowerModel>> response) {
                if (response.isSuccessful()) {
                    List<FlowerModel> flowerList = response.body();

                    for (int i = 0; i < flowerList.size(); i++) {
                        FlowerModel flowerModel = flowerList.get(i);
                        mListFlowerAdapter.addFlower(flowerModel);
                    }
                } else {
                    int sc = response.code();
                    switch (sc) {
                    }
                }
            }

            @Override
            public void onFailure(Call<List<FlowerModel>> call, Throwable t) {
            }
        });
    }

    private void configViews() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);

        mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mListFlowerAdapter = new ListFlowerAdapter();
        mRecyclerView.setAdapter(mListFlowerAdapter);

        //mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
        //mListFlowerAdapter = new ListFlowerAdapter();
        // mRecyclerView.setAdapter(mListFlowerAdapter);
    }
}
  

ListFlowerAdapter类

public class ListFlowerAdapter extends RecyclerView.Adapter<ListFlowerAdapter.MyViewHolder> {

    private static final String TAG = ListFlowerAdapter.class.getSimpleName();
    private Context context;
    private List<FlowerModel> myFlowers;

    public ListFlowerAdapter() {
        myFlowers = new ArrayList<>();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, null, false);
        return new MyViewHolder(row);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        FlowerModel currFlower = myFlowers.get(position);
        holder.mName.setText(currFlower.getName());
        holder.mPrice.setText(Double.toString(currFlower.getPrice()));
        Picasso.with(holder.itemView.getContext()).load("http://services.hanselandpetal.com/photos/" + currFlower.getPhoto()).into(holder.mPhoto);
    }

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

    public void addFlower(FlowerModel flowerModel) {
        myFlowers.add(flowerModel);
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView mPhoto;
        private TextView mName, mPrice;

        public MyViewHolder(View itemView) {
            super(itemView);
            mPhoto = itemView.findViewById(R.id.flower_photo);
            mName = itemView.findViewById(R.id.flower_name);
            mPrice = itemView.findViewById(R.id.flower_price);
        }
    }
}
  

FlowerModel类

 public class FlowerModel implements Serializable {
    @Expose
    private String category;
    @Expose
    private String price;

    @Expose
    private String instructions;

    @Expose
    private String photo;

    @Expose
    private String name;

    @Expose
    private int productId;

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getInstructions() {
        return instructions;
    }

    public void setInstructions(String instructions) {
        this.instructions = instructions;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }
}
  

XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/flower_photo"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="@mipmap/ic_launcher"

            android:scaleType="centerCrop" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/flower_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:text="Large Text" />

            <TextView
                android:id="@+id/flower_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:text="Medium Text" />
        </LinearLayout>

<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Stop Downloading" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:visibility="visible" />

能帮我解决这个问题吗?非常感谢。

3 个答案:

答案 0 :(得分:1)

不要递归通知适配器,

  

您的代码:

for (int i = 0; i < flowerList.size(); i++) {
                    FlowerModel flowerModel = flowerList.get(i);
                    mListFlowerAdapter.addFlower(flowerModel); // Called from here
                }
     

适配器的代码

public void addFlower(FlowerModel flowerModel) {
    myFlowers.add(flowerModel);
    notifyDataSetChanged(); // Notifying everytime
}

相反,您应该将所有数据添加到适配器,并且仅通知一次:(更新的代码)

for (int i = 0; i < flowerList.size(); i++) {
                    FlowerModel flowerModel = flowerList.get(i);
                    mListFlowerAdapter.addFlower(flowerModel);
                }
mListFlowerAdapter.notifyDataSetChanged(); // Notify like this

适配器的代码

public void addFlower(FlowerModel flowerModel) {
    myFlowers.add(flowerModel);
    //notifyDataSetChanged(); Remove from here
}

答案 1 :(得分:1)

尝试删除此行:

mRecyclerView.setHasFixedSize(true);

并将其添加到XML文件中:

android:fillViewport="true"

没有这一行,您的RecyclerView内容的初始高度为0(空列表),并且在固定大小的情况下,即使您向适配器添加了项,它也无法增长。

答案 2 :(得分:1)

将活动上下文传递给适配器,并使用该上下文在列表视图中填充项目。

 private Context mContext;     

 public ListFlowerAdapter(Context context) {
        myFlowers = new ArrayList<>();
        this.mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View row = LayoutInflater.from(mContext).inflate(R.layout.row_item, parent, false);
        return new MyViewHolder(row);
    }

从活动发送活动上下文以像这样初始化适配器

 mListFlowerAdapter = new ListFlowerAdapter(this);

并用于添加新项目

public void addFlowers(List<FlowerModel> flowerModels) {
    for (int i = 0; i < flowerModels.size(); i++) {
            FlowerModel flowerModel = flowerModels.get(i);
            myFlowers.add(flowerModel);
         }
    notifyDataSetChanged();  
}

成功响应,只需发送列表以更新适配器

List<FlowerModel> flowerList = response.body();
mListFlowerAdapter.addFlowers(flowerList);

希望这会有所帮助。