RecyclerView仅显示前两项

时间:2018-03-24 13:01:39

标签: android android-recyclerview recycler-adapter

我正在尝试解析一个远程JSON文件,一切正常,但它只显示前两个项目而没有别的。

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

以下是我在片段中的代码,我尝试使用RecyclerView

        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>

这是我的整个logcat

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();
}

}

3 个答案:

答案 0 :(得分:3)

我复制了你的代码然后运行它,我发现了一个解决方案,但我并不确切知道它是如何工作的。但我认为您的recyclerView已经正常工作并且正在加载所有项目。

步骤1: - 删除XML中的ScrollView并仅保留FrameLayout及其内容(即progressbar和recyclerview)

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

我希望能做到这一点。

答案 1 :(得分:1)

在channel_adapter中添加公共方法以更新列表

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

然后清除listItems以刷新列表

try {
                        JSONObject json = new JSONObject(response);
                        JSONArray array = json.getJSONArray("channels");

                        listItems.clear();
                        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.updateChannelList(listItems);


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

答案 2 :(得分:1)

尝试将ScrollView,FrameLayout,RecyclerView的layout_height修改为match_parent