gpt4 book ai didi

android - RecyclerView 仅显示前两项

转载 作者:行者123 更新时间:2023-11-29 14:59:51 24 4
gpt4 key购买 nike

我正在尝试解析一个远程 JSON 文件,一切正常,但它只显示前两项,没有其他内容。

我检查了 XML 文件,布局的高度是 wrap_content

这是我尝试使用 RecyclerView 的 fragment 代码。

        channel_list = view.findViewById(R.id.channel_list);
channel_list.setHasFixedSize(true);
channel_list.setLayoutManager(new LinearLayoutManager(getActivity()));
channel_list.setNestedScrollingEnabled(false);

listItems = new ArrayList<>();

channel_adapter = new Channel_Adapter(listItems,getActivity());
channel_list.setAdapter(channel_adapter);

channel_progress = view.findViewById(R.id.channel_progress);

StringRequest stringRequest = new StringRequest(Request.Method.GET,
channel_data,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
channel_progress.setVisibility(View.GONE);
try {
JSONObject json = new JSONObject(response);
JSONArray array = json.getJSONArray("channels");

for(int i = 0; i < array.length(); i++){
JSONObject o = array.getJSONObject(i);
Channel_Item item = new Channel_Item(
o.optString("name"),
o.optString("network"),
o.optString("description"),
o.optString("programs"),
o.optString("logo")
);

listItems.add(item);
}

Toast.makeText(getActivity(), "Size" + listItems.size(), Toast.LENGTH_SHORT).show();

channel_adapter.notifyDataSetChanged();

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
channel_progress.setVisibility(View.GONE);
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);

这是相同的 XML 文件。

<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/channel_progress"/>

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/channel_list">

</android.support.v7.widget.RecyclerView>

</FrameLayout>

</ScrollView>

这是我的整个日志

03-24 18:09:14.247 4845-4845/a.tvreminder I/InstantRun: starting instant run server: is main process
03-24 18:09:14.355 4845-4845/a.tvreminder W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-24 18:09:14.791 4845-4871/a.tvreminder D/NetworkSecurityConfig: No Network Security Config specified, using platform default

[ 03-24 18:09:14.942 4845: 4869 D/ ]
HostConnection::get() New Host Connection established 0xa94665c0, tid 4869
03-24 18:09:14.944 4845-4869/a.tvreminder I/OpenGLRenderer: Initialized EGL, version 1.4
03-24 18:09:14.944 4845-4869/a.tvreminder D/OpenGLRenderer: Swap behavior 1
03-24 18:09:14.944 4845-4869/a.tvreminder W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-24 18:09:14.945 4845-4869/a.tvreminder D/OpenGLRenderer: Swap behavior 0
03-24 18:09:15.037 4845-4845/a.tvreminder W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

这是我的适配器。

public class Channel_Adapter extends 
RecyclerView.Adapter<Channel_Adapter.ViewHolder> {

View v;

private List<Channel_Item> listItems;
private Context context;

public Channel_Adapter(List<Channel_Item> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}

@Override
public Channel_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.channel_card, parent, false);
return new ViewHolder(v);

}

@Override
public void onBindViewHolder(Channel_Adapter.ViewHolder holder, int position) {
final Channel_Item item = listItems.get(position);

holder.channel_name.setText(item.getName());
holder.channel_network.setText(item.getNetwork());
holder.channel_description.setText(item.getDescription());

Glide.with(context)
.load(item.getLogo())
//.thumbnail(0.9f)
//.placeholder(R.drawable.ic_image)
.into(holder.channel_logo);


holder.card_body.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

item.getPrograms();

}
});

}

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

public class ViewHolder extends RecyclerView.ViewHolder{

public TextView channel_name,channel_network,channel_description;
public ImageView channel_logo;
public LinearLayout card_body;

public ViewHolder(View itemView) {
super(itemView);

channel_network = (TextView) itemView.findViewById(R.id.channel_network);
channel_description = (TextView) itemView.findViewById(R.id.channel_description);
channel_name = (TextView) itemView.findViewById(R.id.channel_name);
channel_logo = (ImageView) itemView.findViewById(R.id.channel_logo);
card_body = (LinearLayout) itemView.findViewById(R.id.channel_card_body);

}
}

public void updateChannelList(List<Channel_Item> channelList){
this.listItems.addAll(channelList);
notifyDataSetChanged();
}

}

最佳答案

我复制了您的代码并运行了它,我找到了解决方案,但我不完全知道它是如何工作的以及为什么会工作。但我认为您的 recyclerView 已经工作正常并且正在加载所有项目。

第 1 步:- 删除 XML 中的 ScrollView 并仅保留 FrameLayout 及其内容(即进度条和回收 View )

第 2 步:- 设置 channel_list.setNestedScrollingEnabled(true);

我希望这能完成工作。

关于android - RecyclerView 仅显示前两项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49464995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com