我在一个负责显示JSON数据的片段中有一个RecyclerView,一切正常,但它只显示前两个JSON项。我尝试通过祝酒来检查列表的大小,但它似乎工作得很好,因为它显示了4。
此外,我检查了所有XML文件,以查看已设置为wrap_content
的布局属性。
这是我在onCreateView中的代码
program_list = view.findViewById(R.id.program_list);
program_list.setHasFixedSize(true);
program_list.setLayoutManager(new LinearLayoutManager(getActivity()));
program_list.setNestedScrollingEnabled(false);
listItems = new ArrayList<>();
program_adapter = new Program_Adapter(listItems,getActivity());
program_list.setAdapter(program_adapter);
program_progress = view.findViewById(R.id.program_progress);
StringRequest stringRequest = new StringRequest(Request.Method.GET,
program_data,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
program_progress.setVisibility(View.GONE);
try {
JSONObject json = new JSONObject(response);
JSONArray array = json.getJSONArray("programs");
listItems.clear();
for(int i = 0; i < array.length(); i++){
JSONObject o = array.getJSONObject(i);
Program_Item item = new Program_Item(
o.optString("name"),
o.optString("channel"),
o.optString("description"),
o.optLong("timings"),
o.optString("logo")
);
listItems.add(item);
}
Toast.makeText(getActivity(), "Size " + listItems.size(), Toast.LENGTH_SHORT).show();
program_adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
program_progress.setVisibility(View.GONE);
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
这是我的适配器
public class Program_Adapter extends RecyclerView.Adapter<Program_Adapter.ViewHolder> {
View v;
private List<Program_Item> listItems;
private Context context;
public Program_Adapter(List<Program_Item> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public Program_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.program_card, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(Program_Adapter.ViewHolder holder, int position) {
final Program_Item item = listItems.get(position);
holder.program_name.setText(item.getName());
holder.program_channel.setText(item.getNetwork());
holder.program_description.setText(item.getDescription());
Glide.with(context)
.load(item.getLogo())
//.thumbnail(0.9f)
//.placeholder(R.drawable.ic_image)
.into(holder.program_logo);
holder.card_body.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.getTimings();
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView program_name,program_channel,program_description;
public ImageView program_logo;
public LinearLayout card_body;
public ViewHolder(View itemView) {
super(itemView);
program_channel = (TextView) itemView.findViewById(R.id.program_channel);
program_description = (TextView) itemView.findViewById(R.id.program_description);
program_name = (TextView) itemView.findViewById(R.id.program_name);
program_logo = (ImageView) itemView.findViewById(R.id.program_logo);
card_body = (LinearLayout) itemView.findViewById(R.id.program_card_body);
}
}
}
答案 0 :(得分:0)
我可以看到一个错误,你没有设置回收者视图的方向。
LinearLayoutManager linear = new new LinearLayoutManager(getActivity());
linear.setOrientation(LinearLayoutManager.VERTICAL);
program_list.setLayoutManager(linear);
如果它只是一个回收者视图,则不需要 setNestedScrollingEnabled(false)。
希望这会有所帮助!!