How to setup recyclerview in recyclerview adapter
Fragment_A
Setup recyclerview Adapter_A in Fragment_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
Setup recyclerview Adapter_A_Card in Adapter_A
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);
}
}
}
when i scroll recyclerview with Adapter_A, always refresh recyclerview with Adapter_A_Card
i try setup inside , it success, not refresh, 内进行设置,它成功,但不刷新,
But Position is not there
Please help me
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);
}
}
Do not use onCreateViewHolder
for binding views for recyclerview
remove 2nd and 3rd raw of the following code that you wrote:
@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();
write that code onBindViewHolder(ModelViewHolder holder, int position)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.