Recyclerview没有调用onCreate或onBind或构造函数

时间:2015-02-25 11:39:45

标签: android android-recyclerview

RecyclerView不会调用onCreate或onBind或构造函数。没有断点或日志。可能是什么原因?可能是什么原因?有人怎么解决这个问题?这是我的代码。适配器:

public class MainContentRecyclerAdapter extends RecyclerView.Adapter<MainContentRecyclerAdapter.MainContentViewHolder> {

private Context context;
private LayoutInflater inflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
List<Item> items = new ArrayList<Item>();

public MainContentRecyclerAdapter(Context context) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    volleySingleton = VolleySingleton.getSingletonInstance();
    imageLoader = volleySingleton.getImageLoader();
}

public void setData(List<Item> items) {
    this.items = items;
    notifyItemRangeChanged(0,items.size());
}

@Override
public MainContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.e("Sony","onCreateViewHolder");
    View view = inflater.inflate(R.layout.cart_item, parent, false);
    MainContentViewHolder holder = new MainContentViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final MainContentViewHolder holder, int position) {
    Log.e("Sony","onBindViewHolder");
    Item currentItem = items.get(position);
    if (currentItem.imageURL != null) {
        imageLoader.get(currentItem.imageURL, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                Log.e("Sony","onResponseImage");
                holder.itemImage.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Sony","onErrorImage");
                holder.itemImage.setImageResource(R.drawable.default_product);
            }
        });
    }

}

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

class MainContentViewHolder extends RecyclerView.ViewHolder {

    ImageView itemImage;


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

        itemImage = (ImageView) itemView.findViewById(R.id.imgItemPic);   
    }
}
}

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardItemHolderRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/imgItemPic"
    android:layout_width="@dimen/itemImageDimen"
    android:layout_height="@dimen/itemImageDimen"
    android:layout_centerVertical="true"
    android:contentDescription="@string/productImage"
    android:src="@drawable/default_product"
    android:elevation="2dp"
    android:focusableInTouchMode="false"
    android:layout_marginTop="@dimen/loginWidgetTopMargin" />

</RelativeLayout>

活动onCreate

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.appBar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    //Calls volleyLibrary to fetch images using google image search
    getData("Sachin");

    mainListView = (RecyclerView) findViewById(R.id.recyclerList);
    mainListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    mainListView.setHasFixedSize(true);
    recyclerViewListAdapter = new MainContentRecyclerAdapter(this);
    mainListView.setAdapter(recyclerViewListAdapter);
    mainListView.setLayoutManager(new LinearLayoutManager(this));
}

1 个答案:

答案 0 :(得分:1)

我可以在您的代码中看到的唯一问题

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

items.size(); always return 0在设置适配器后解析此调用getData。

   recyclerViewListAdapter = new MainContentRecyclerAdapter(this);     
     mainListView.setAdapter(recyclerViewListAdapter);
     getData("Sachin");

同时更新您的

setData

public void setData(List<Item> items) {
    this.items = items;
    mRecyclerAdapter.notifyDataSetChanged();
}

OR

public void setData(List<Item> items) {
        this.items = items;        
    }

在您调用setData

之后立即从主活动中调用
mRecyclerAdapter.notifyDataSetChanged();