RecyclerView未设置适配器

时间:2019-07-06 13:03:04

标签: java android android-recyclerview recycler-adapter

我在一个片段中有三个回收站视图。我已经为其中两个设置了适配器。但是,一个回收者视图会使卡片视图夸大,而另一个则不会。

我已经调试了代码,没有显示的回收站视图适配器没有被调用。

这是片段中的onViewCreated()方法

public void onViewCreated(View view, @NonNull Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerViewVendors = view.findViewById(R.id.rcyview_vendors);
        recyclerViewEvents = view.findViewById(R.id.rcyview_home_events);
        String json;
        try{

            InputStream is = getActivity().getAssets().open("vendorServices.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            if(is.read(buffer) == -1){
                throw new EOFException();
            }
            is.close();
            json = new String(buffer, StandardCharsets.UTF_8);

            JSONObject object = new JSONObject(json);
            JSONArray j_Array = object.getJSONArray("vendorServices");

            for(int i=0;i<j_Array.length();i++){
                JSONObject jo_inside = j_Array.getJSONObject(i);
                myDatasetVendors.add(new VendorService(
                        jo_inside.getString("name"),
                        jo_inside.getString("img")
                ));
            }


            is = getActivity().getAssets().open("activities.json");
            size = is.available();
            buffer = new byte[size];
            if (is.read(buffer) == -1) {
                throw new EOFException();
            }

            is.close();
            json = new String(buffer, StandardCharsets.UTF_8);

            JSONObject obj = new JSONObject(json);
            JSONArray m_jArray = obj.getJSONArray("Activities");

            for (int i = 0; i < m_jArray.length(); i++) {
                JSONObject jo_inside = m_jArray.getJSONObject(i);
                myDatasetEvents.add(jo_inside.getString("title"));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }



        layoutManagerVendors = new LinearLayoutManager(this.getContext(), LinearLayoutManager.HORIZONTAL, false);
        recyclerViewVendors.setLayoutManager(layoutManagerVendors);

        mAdapterVendors = new AdapterVendorServices(myDatasetVendors, this.getContext());
        recyclerViewVendors.setAdapter(mAdapterVendors);

        layoutManagerEvents = new LinearLayoutManager(this.getContext(), LinearLayoutManager.HORIZONTAL, false);
        recyclerViewEvents.setLayoutManager(layoutManagerEvents);

        mAdapterEvents = new AdapterHomeEvents(myDatasetEvents, this.getContext());
        recyclerViewEvents.setAdapter(mAdapterEvents);
    }


这是我的适配器,不会被调用

public class AdapterHomeEvents extends RecyclerView.Adapter<AdapterHomeEvents.MyViewHolder> {
    ArrayList<String> mDataset;
    Context context;

    public AdapterHomeEvents(ArrayList<String> mDataset, Context context) {
        this.mDataset = mDataset;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_home_events, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.textView.setText(mDataset.get(position));
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        MyViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.txt_crdViewHome_events);
        }
    }
}

我希望它可以用卡片视图填充两个回收视图,但是它只能填充一个回收器视图,而不会填充另一个视图。

1 个答案:

答案 0 :(得分:5)

您忘记在适配器中设置尺寸:-

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