如何在Recyclerview适配器中使用Recyclerview

时间:2016-01-05 05:32:32

标签: android android-recyclerview

如何在recyclerview适配器中设置recyclerview

Fragment_A
在Fragment_A中设置recyclerview Adapter_A

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    View view = inflater.inflate(R.layout.fragment_a, container, false);
    List<Model_A> model = new ArrayList<Model_A>();

    Adapter_A adapter  = new Adapter_A(model,getActivity().getApplicationContext());
    LinearLayoutManager llm = new LinearLayoutManager(getActivity().getApplication());

    RecyclerView r = (RecyclerView) view.findViewById(R.id.fragment_a_recyclerview);
    r.setHasFixedSize(true);
    r.setLayoutManager(llm);
    r.setAdapter(adapter);

    model.add(new Model_A("A"));
    model.add(new Model_A("B"));
    model.add(new Model_A("C"));
    model.add(new Model_A("D"));
    model.add(new Model_A("E"));
    model.add(new Model_A("F"));
    model.add(new Model_A("G"));
    model.add(new Model_A("E"));
    model.add(new Model_A("F"));
    model.add(new Model_A("G"));

    return view;

}





Adapter_A
在Adapter_A中设置recyclerview Adapter_A_Card

public class Adapter_A extends RecyclerView.Adapter<Adapter_A.ModelViewHolder>{
    List<Model_A> model;
    Context context;


    public Adapter_A(List<Model_A> model,Context context){
        this.model = model;
        this.context = context;
    }
    @Override
    public ModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_a, parent, false);
        return new ModelViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(ModelViewHolder holder, int position) {
        Model_A m = model.get(position);
        TextView header = (TextView) holder.itemview.findViewById(R.id.cardview_a_header);
        header.setText(m.header);



        new Load(holder.itemView,context,position).Calls();
    }

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

    class ModelViewHolder extends RecyclerView.ViewHolder{
        protected View itemview;
        public ModelViewHolder(View itemView) {
            super(itemView);
            this.itemview = itemView;
        }
    }


    class Load{
        View view;
        Context context;
        int id;

        public Load(View view,Context context,int id){
            this.view = view;
            this.context =context;
            this.id = id;
        }
        public void Calls(){
            id=id+1;
            String url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=__HIDDENKEY_&q=love&page_limit=50&page=" + id;


            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            List<Model_A_Card> model_a_card = new ArrayList<Model_A_Card>();
                            Adapter_A_Card adapter = new Adapter_A_Card(model_a_card,context);
                            LinearLayoutManager llm = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);

                            RecyclerView r = (RecyclerView) view.findViewById(R.id.cardview_a_recyclerview);
                            r.setHasFixedSize(true);
                            r.setLayoutManager(llm);

                            r.setAdapter(adapter);

                            try {
                                JSONArray movies = response.getJSONArray("movies");

                                for (int i = 0; i < movies.length(); i++) {

                                    JSONObject obj = movies.getJSONObject(i);

                                    Model_A_Card m = new Model_A_Card();

                                    m.title = obj.getString("title");

                                    JSONObject objImage = obj.getJSONObject("posters");
                                    m.image = objImage.getString("original");
                                    model_a_card.add(m);
                                }
                            }catch (JSONException e){

                            }

                            adapter.notifyDataSetChanged();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });


            NetworkVolley.getInstance(context).addToRequestQueue(jsonObjectRequest);
        }
    }

}

使用此代码时没有任何错误
当我使用Adapter_A滚动recyclerview时, 始终使用Adapter_A_Card刷新recyclelerview

我在 onCreateViewHolder 内尝试设置,成功,不刷新,
但位置不存在 请帮帮我

    public class Adapter_A extends RecyclerView.Adapter<Adapter_A.ModelViewHolder>{

    @Override
    public ModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_a, parent, false);
        int position = 0; // NOT POSITION
        new Load(itemView,context,position).Calls();
        return new ModelViewHolder(itemView);
    }

}

1 个答案:

答案 0 :(得分:0)

不要将onCreateViewHolder用于recyclerview的绑定视图

删除您编写的以下代码的第2和第3个原始文件:

@Override
    public ModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_a, parent, false);
        int position = 0; // NOT POSITION -> should not be here
        new Load(itemView,context,position).Calls(); //-> should not be here
        return new ModelViewHolder(itemView);
    }

new Load(itemView,context,position).Calls();

写下代码onBindViewHolder(ModelViewHolder holder, int position)