繁体   English   中英

错误:如何将Cardview插入回收站片段布局

[英]Error: How to insert cardview into recycler fragment layout

大家好,我目前正在研究如何解析JSON并在Android中使用RecyclerView和CardView显示。 我不明白为什么卡片视图未显示在回收者视图布局中。 有人遇到过这个问题吗? 谢谢

news_layout.xml(片段布局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"


    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/recyclerview"
        android:clickable="true"
        android:focusable="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

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


</RelativeLayout>

recycle.news.xml(cardview布局)

<android.support.v7.widget.CardView
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="https://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"

    android2:layout_marginTop="5dp"
    android2:layout_marginLeft="5dp"
    android2:layout_marginRight="5dp"
    android2:layout_gravity="center|top"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardCornerRadius="20dp"
    android2:layout_width="match_parent"
    android2:layout_height="wrap_content">

<FrameLayout
    android2:layout_width="match_parent"
    android2:layout_height="400dp"
    app:cardElevation="0dp"
    android2:background="@drawable/cardviewstring">



    <LinearLayout
        android2:orientation="vertical"
        android2:layout_width="380dp"
        android2:layout_height="match_parent"


        android2:weightSum="1"
        android2:layout_marginRight="20dp">

        <LinearLayout
            android2:orientation="vertical"
            android2:layout_width="match_parent"
            android2:layout_weight="1"
            android2:layout_height="250dp">

            <ImageView
                android2:layout_width="match_parent"
                android2:layout_height="match_parent"
                app:srcCompat="@mipmap/ic_launcher"
                android2:id="@+id/imageView4" />

        </LinearLayout>

        <LinearLayout
            android2:orientation="vertical"
            android2:layout_width="match_parent"
            android2:layout_height="wrap_content"
            android2:paddingTop="25dp">

            <ScrollView
                android2:layout_width="match_parent"
                android2:layout_height="84dp"
                android2:background="@drawable/screen_background_dark_transparent"
                android2:layout_marginLeft="3dp">

                <LinearLayout
                    android2:layout_width="match_parent"
                    android2:layout_height="wrap_content"
                    android2:orientation="vertical" >

                    <TextView
                        android2:text="TextView"
                        android2:layout_width="match_parent"
                        android2:layout_height="wrap_content"
                        android2:id="@+id/textView4" />

                    <TextView
                        android2:text="TextView"
                        android2:layout_width="match_parent"
                        android2:layout_height="35dp"
                        android2:id="@+id/textView3" />
                </LinearLayout>
            </ScrollView>

        </LinearLayout>

        <LinearLayout
            android2:orientation="vertical"
            android2:layout_marginTop="10dp"
            android2:layout_width="match_parent"
            android2:layout_height="42dp"
            android:layout_alignParentBottom="true">

            <LinearLayout
                android2:orientation="horizontal"
                android2:layout_width="match_parent"
                android2:layout_height="match_parent">

                <ImageView
                    android2:layout_width="wrap_content"
                    android2:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_share"
                    android2:id="@+id/imageView3"
                    android2:layout_weight="1" />

                <ImageView
                    android2:layout_width="wrap_content"
                    android2:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_like"
                    android2:id="@+id/imageView2"
                    android2:layout_weight="1" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

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

NewsFragment.java

package com.androidbelieve.drawerwithswipetabs;

import android.app.LauncherActivity;
import android.graphics.Movie;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Ratan on 7/29/2015.
 */


public class NewsFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private GridLayoutManager gridLayoutManager;
    private newsAdapter nAdapter;
    private List<listnews> data_list;

    private JsonArrayRequest jsonArrayRequest;
    private RequestQueue requestQueue;




    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.news_layout,container,false);
        mRecyclerView =(RecyclerView)view.findViewById(R.id.recyclerview);
        mRecyclerView.setHasFixedSize(true);

        data_list = new ArrayList<>();
        load_data_from_server(0);

        mRecyclerView.setHasFixedSize(true);


        gridLayoutManager = new GridLayoutManager(getActivity(),2);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        nAdapter = new newsAdapter(getActivity(),data_list);
        mRecyclerView.setAdapter(nAdapter);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1)
                {
                    load_data_from_server(data_list.get(data_list.size()-1).getNews_id());
                }

            }
        });



        return view;



    private void load_data_from_server(final int id)
    {
        AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
            @Override
            protected Void doInBackground(Integer... params) {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://192.168.107.1/ibmcoe_la/selected.php?id="+params[0])
                        .build();

                try{
                    Response response = client.newCall(request).execute();

                    JSONArray array = new JSONArray(response.body().string());

                    for(int i=0; i<array.length(); i++)
                    {
                        JSONObject object = array.getJSONObject(i);

                        listnews data = new listnews(object.getInt("news_id"),object.getString("path_image"),object.getString("news_image")
                                ,object.getString("news_title"),object.getString("news_description")
                                );


                        data_list.add(data);
                    }



                }catch (IOException e){
                    e.printStackTrace();
                }catch (JSONException e){
                    System.out.println("End of content");


                }


                return null;
            }

            protected void onPostExecute(Void avoid){
                nAdapter.notifyDataSetChanged();
            }
        };

        task.execute(id);
    }

    @Override
    public String toString()
    {
        return "NewsFragment";
    }






}

newsAdapter.java

package com.androidbelieve.drawerwithswipetabs;

import android.content.Context;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LENOVO on 20/2/2017.
 */


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



    private Context context;
    private List<listnews> my_data;

    public newsAdapter(Context context, List<listnews> my_data)
    {
        this.context = context;
        this.my_data = my_data;
    }

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


            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_news,parent,false);

//            ViewHolder viewHolder = new ViewHolder(view);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {



        holder.description.setText(my_data.get(position).getNews_description());
        Glide.with(context).load(my_data.get(position).getPath_image()).into(holder.dataimage);

    }

    @Override
    public int getItemCount()
    {
        if(my_data !=null)
        {
            return my_data.size();
        }
        return 0;
    }

    public static  class ViewHolder extends RecyclerView.ViewHolder {

        public TextView description;
        public TextView imagetitle;
        public ImageView dataimage;

        public ViewHolder(View itemView) {
            super(itemView);
            description = (TextView) itemView.findViewById(R.id.textView3);
            imagetitle = (TextView) itemView.findViewById(R.id.textView4);
            dataimage = (ImageView) itemView.findViewById(R.id.imageView4);

        }
    }



}

listnews.java

package com.androidbelieve.drawerwithswipetabs;

import java.io.Serializable;

/**
 * Created by LENOVO on 21/2/2017.
 */

public class listnews implements Serializable {


    public int news_id;
    public String path_image, news_image, news_title, news_description;

    public listnews(int news_id, String path_image, String news_image, String news_title, String news_description) {
        this.news_id = news_id;
        this.path_image = path_image;
        this.news_image = news_image;
        this.news_title = news_title;
        this.news_description = news_description;
    }

    public int getNews_id() {
        return news_id;
    }

    public void setNews_id(int news_id) {
        this.news_id = news_id;
    }

    public String getPath_image() {
        return path_image;
    }

    public void setPath_image(String path_image) {
        this.path_image = path_image;
    }

    public String getNews_image() {
        return news_image;
    }

    public void setNews_image(String news_image) {
        this.news_image = news_image;
    }

    public String getNews_title() {
        return news_title;
    }

    public void setNews_title(String news_title) {
        this.news_title = news_title;
    }

    public String getNews_description() {
        return news_description;
    }

    public void setNews_description(String news_description) {
        this.news_description = news_description;
    }


}

数据库结构

php数据库结构

**localhost Server Response  (get JSON)**

http://192.168.107.1/ibmcoe_la/selected.php?id

{
  "server_response": [
    {
      "news_id": "25",
      "path_image": "http://localhost/ibmcoe_la/images/lonesky.jpg",
      "news_title": "lonesky.jpg",
      "news_description": "testttttt",
      "news_timepost": "17.03.01 15:34:53"
    },
    {
      "news_id": "23",
      "path_image": "http://localhost/ibmcoe_la/images/ump-ios-01.jpg",
      "news_title": "ump-ios-01.jpg",
      "news_description": "5455454",
      "news_timepost": "17.02.22 15:24:07"
    },
    {
      "news_id": "22",
      "path_image": "http://localhost/ibmcoe_la/images/fccc9dc81940317b7881c2be7f5c94c4.jpg",
      "news_title": "fccc9dc81940317b7881c2be7f5c94c4.jpg",
      "news_description": "21",
      "news_timepost": "17.02.22 15:23:10"
    },
    {
      "news_id": "19",
      "path_image": "http://localhost/ibmcoe_la/images/logo-rasmi-ump-(logo-sahaja).png",
      "news_title": "logo-rasmi-ump-(logo-sahaja).png",
      "news_description": "qqqq",
      "news_timepost": "17.02.22 15:10:04"
    },
    {
      "news_id": "5",
      "path_image": "http://localhost/ibmcoe_la/images/Koala.jpg",
      "news_title": "Koala.jpg",
      "news_description": "e",
      "news_timepost": "17.02.22 10:40:32"
    },
    {
      "news_id": "4",
      "path_image": "http://localhost/ibmcoe_la/images/Tulips.jpg",
      "news_title": "Tulips.jpg",
      "news_description": "c",
      "news_timepost": "17.02.22 10:22:54"
    },
    {
      "news_id": "2",
      "path_image": "http://localhost/ibmcoe_la/images/Chrysanthemum.jpg",
      "news_title": "Chrysanthemum.jpg",
      "news_description": "d",
      "news_timepost": "17.02.22 10:16:23"
    },
    {
      "news_id": "18",
      "path_image": "http://localhost/ibmcoe_la/images/fccc9dc81940317b7881c2be7f5c94c4.jpg",
      "news_title": "fccc9dc81940317b7881c2be7f5c94c4.jpg",
      "news_description": "q",
      "news_timepost": "17.02.22 03:08:28"
    },
    {
      "news_id": "17",
      "path_image": "http://localhost/ibmcoe_la/images/galaxy-pictures-6.jpg",
      "news_title": "galaxy-pictures-6.jpg",
      "news_description": "1",
      "news_timepost": "17.02.22 02:39:56"
    },
    {
      "news_id": "16",
      "path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
      "news_title": "PicIBM.png",
      "news_description": "2",
      "news_timepost": "17.02.22 02:37:56"
    },
    {
      "news_id": "15",
      "path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
      "news_title": "PicIBM.png",
      "news_description": "2",
      "news_timepost": "17.02.22 02:37:45"
    },
    {
      "news_id": "14",
      "path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
      "news_title": "PicIBM.png",
      "news_description": "2",
      "news_timepost": "17.02.22 02:33:38"
    },
    {
      "news_id": "13",
      "path_image": "http://localhost/ibmcoe_la/images/testin123.jpg",
      "news_title": "testin123.jpg",
      "news_description": "1",
      "news_timepost": "17.02.22 02:30:08"
    },
    {
      "news_id": "1",
      "path_image": "http://localhost/ibmcoe_la/images/Penguins.jpg",
      "news_title": "Penguins.jpg",
      "news_description": "testing",
      "news_timepost": "17.02.21 04:49:32"
    }
  ]
}

我认为您可以尝试这样:

JSONObject jsonObject= new JSONObject(response.body().string());
JSONArray array = jsonObject.getJSONArray("server_response");
for(int i=0; i<array.length(); i++)
    {
     JSONObject object = array.getJSONObject(i);
     listnews data = new listnews(object.getInt("news_id"),object.getString("path_image"),object.getString("news_image")
                            ,object.getString("news_title"),object.getString("news_description")
                            );


              data_list.add(data);
     }

尝试一下,尝试在CardView上方插入线性布局,并在cardView上放一些标高,看它是否会出现:

<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="https://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orienatation="vertical"
 android2:layout_height="wrap_content"
>
<android.support.v7.widget.CardView

    android:id="@+id/card_view"
    android:layout_width="match_parent"

    android2:layout_marginTop="5dp"
    android2:layout_marginLeft="5dp"
    android2:layout_marginRight="5dp"
    android2:layout_gravity="center|top"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardCornerRadius="20dp"
     app:elevation="5dp"
    android2:layout_width="match_parent"
    android2:layout_height="wrap_content">

<FrameLayout
    android2:layout_width="match_parent"
    android2:layout_height="400dp"
    app:cardElevation="0dp"
    android2:background="@drawable/cardviewstring">



    <LinearLayout
        android2:orientation="vertical"
        android2:layout_width="380dp"
        android2:layout_height="match_parent"


        android2:weightSum="1"
        android2:layout_marginRight="20dp">

        <LinearLayout
            android2:orientation="vertical"
            android2:layout_width="match_parent"
            android2:layout_weight="1"
            android2:layout_height="250dp">

            <ImageView
                android2:layout_width="match_parent"
                android2:layout_height="match_parent"
                app:srcCompat="@mipmap/ic_launcher"
                android2:id="@+id/imageView4" />

        </LinearLayout>

        <LinearLayout
            android2:orientation="vertical"
            android2:layout_width="match_parent"
            android2:layout_height="wrap_content"
            android2:paddingTop="25dp">

            <ScrollView
                android2:layout_width="match_parent"
                android2:layout_height="84dp"
                android2:background="@drawable/screen_background_dark_transparent"
                android2:layout_marginLeft="3dp">

                <LinearLayout
                    android2:layout_width="match_parent"
                    android2:layout_height="wrap_content"
                    android2:orientation="vertical" >

                    <TextView
                        android2:text="TextView"
                        android2:layout_width="match_parent"
                        android2:layout_height="wrap_content"
                        android2:id="@+id/textView4" />

                    <TextView
                        android2:text="TextView"
                        android2:layout_width="match_parent"
                        android2:layout_height="35dp"
                        android2:id="@+id/textView3" />
                </LinearLayout>
            </ScrollView>

        </LinearLayout>

        <LinearLayout
            android2:orientation="vertical"
            android2:layout_marginTop="10dp"
            android2:layout_width="match_parent"
            android2:layout_height="42dp"
            android:layout_alignParentBottom="true">

            <LinearLayout
                android2:orientation="horizontal"
                android2:layout_width="match_parent"
                android2:layout_height="match_parent">

                <ImageView
                    android2:layout_width="wrap_content"
                    android2:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_share"
                    android2:id="@+id/imageView3"
                    android2:layout_weight="1" />

                <ImageView
                    android2:layout_width="wrap_content"
                    android2:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_like"
                    android2:id="@+id/imageView2"
                    android2:layout_weight="1" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

</android.support.v7.widget.CardView>
<LinearLayout/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM