RecyclerView onCreateViewHolder未被调用

时间:2016-09-20 06:21:08

标签: java android methods android-recyclerview android-volley

我想在RecyclerView中添加数据。完成编译后,RecyclerView未在虚拟手机内显示。当我再次检查时,onCreateViewHolder未被调用。希望你们可以帮忙我解决了这个问题。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.mycustomvolley.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="100dp"
       android:id="@+id/recyclerview"
       >

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

row_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:background="#BBDEFB"
    >

    <com.android.volley.toolbox.NetworkImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_margin="8dp"
        android:id="@+id/networkImage"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textName"
        android:text="@string/title"
        android:layout_toRightOf="@+id/networkImage"
        android:padding="8dp"
        android:textColor="@color/colorText"
        android:textSize="18dp"
        android:textStyle="bold"/>

</RelativeLayout>

MainActivity.java

package com.example.user.mycustomvolley;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<ArtInformation> arrayList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);


        BackgroundTask backgroundTask = new BackgroundTask(MainActivity.this);
        arrayList = backgroundTask.getArrayList();
        adapter = new RecyclerAdapter(arrayList,this);
        recyclerView.setAdapter(adapter);

    }
}

BackgroundTask.java

package com.example.user.mycustomvolley;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

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

import java.util.ArrayList;

/**
 * Created by User on 9/17/2016.
 */
public class BackgroundTask {
    Context context;
    ArrayList<ArtInformation> arrayList = new ArrayList<>();
    private static final String json_url = "http://192.168.1.7/volley/imagetext/getData.php";

    public BackgroundTask(Context context){
        this.context = context;
    }

    public ArrayList<ArtInformation> getArrayList(){

        //make json request.
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,json_url,(String) null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                int count = 0;
                while (count < response.length()) {
                    try{
                        JSONObject jsonObject = response.getJSONObject(count);
                        String artTemp = jsonObject.getString("art");
                        String imageTemp = jsonObject.getString("image");

                        ArtInformation artInformation = new ArtInformation(artTemp,imageTemp);
                        arrayList.add(artInformation);
                        count++;

                    }catch (JSONException ex){
                        ex.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,"Error:[JSON Process",Toast.LENGTH_LONG).show();
                //progressDialog.hide();
                error.printStackTrace();
            }
        });
        MySingleton.getmInstances(context).addToRequestQueue(jsonArrayRequest);
        return arrayList;
    }


}

RecyclerAdapter.java

package com.example.user.mycustomvolley;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

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

/**
 * Created by User on 9/17/2016.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    ArrayList<ArtInformation> arrayList = new ArrayList<>();
    Context context;
    private ImageLoader imageLoader;

    public RecyclerAdapter(ArrayList<ArtInformation> list, Context context){
        super();
        this.arrayList = list;
        this.context = context;

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("","call onCreateViewHolder");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_list,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);

        return myViewHolder;
    }

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

        ArtInformation artInformation = arrayList.get(position);
       // Log.d("Check:","art:"+artInformation.getArt_name()+" / path:"+artInformation.getImage_path());
        imageLoader = MySingleton.getmInstances(context).getImageLoader();
        imageLoader.get(artInformation.getImage_path(), ImageLoader.getImageListener(holder.image, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

        holder.image.setImageUrl(artInformation.getImage_path(),imageLoader);
        holder.artName.setText(artInformation.getArt_name());

    }

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

    public static class MyViewHolder extends  RecyclerView.ViewHolder{
        TextView artName;
        NetworkImageView image;


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

            artName = (TextView) itemView.findViewById(R.id.textName);
            image = (NetworkImageView) itemView.findViewById(R.id.networkImage);


        }

    }

}

1 个答案:

答案 0 :(得分:1)

如果getItemCount方法返回0,即arrayList.size() == 0,则RecyclerView永远不会尝试实例化视图。