RecyclerView不要调用OnCreateViewHolder,OnBindViewHolder,GetItemCount()

时间:2018-07-30 09:53:39

标签: android

在此代码中,我在 RecyclerView类的每一行上使用了断点,但是调用了该类的唯一构造函数,不再调用任何函数(OnCreateViewHolder,OnBindViewHolder,GetItemCount()等) enter image description here。 LogCat的图像也已上传...

MainActivity

package com.example.admin.recyclerview;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    String[] titles;
    String[] description;
    int[] images = {R.drawable.facebook, R.drawable.instagram, R.drawable.twitter, R.drawable.google};
    ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        titles = getResources().getStringArray(R.array.titles);
        description = getResources().getStringArray(R.array.description);
        int i = 0;
        for(String name : titles){
            DataProvider dataProvider = new DataProvider(images[i], titles[i], description[i]);
            arrayList.add(dataProvider);
            i++;
        }

        adapter = new RecyclerAdapter(arrayList);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setAdapter(adapter);
    }
}

RecyclerView类

package com.example.admin.recyclerview;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    private ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();

    public RecyclerAdapter(ArrayList<DataProvider> arrayList){
        this.arrayList = arrayList;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int i) {
        DataProvider dataProvider = arrayList.get(i);
        recyclerViewHolder.imageView.setImageResource(dataProvider.getImage_res());
        recyclerViewHolder.title.setText(dataProvider.getTitle());
        recyclerViewHolder.description.setText(dataProvider.getDescription());
    }

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

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView title;
        TextView description;

        public RecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
            description = itemView.findViewById(R.id.description);
        }
    }
}

数据提供者

package com.example.admin.recyclerview;

public class DataProvider {
    private int image_res;
    private String title;
    private String description;


    public DataProvider(int image_res, String title, String description) {
        this.image_res = image_res;
        this.title = title;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImage_res() {
        return image_res;
    }

    public void setImage_res(int image_res) {
        this.image_res = image_res;
    }
}

2 个答案:

答案 0 :(得分:0)

您没有将LayoutManager添加到RV。

recyclerView.setLayoutManager(layoutManager)

如果您的RV无法正常工作

答案 1 :(得分:0)

您错过添加LayoutManager。添加recyclerView.setLayoutManager(layoutManager);

    adapter = new RecyclerAdapter(arrayList);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager); // add this line
    recyclerView.setAdapter(adapter);