RecyclerView没有出现在片段中 [英] RecyclerView doesn't appear in Fragment

查看:45
本文介绍了RecyclerView没有出现在片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在片段中添加了一个recyclerView,但片段中未显示任何内容 我针对同一问题发现了许多问题,但实际上我避免了这些错误 我从适配器中的每个方法发送Toast消息,并在Toast中正常显示数据 这是我的代码

i add a recyclerView to Fragment but it doesn't show any thing in side the fragment i found many questions for the same problem but actually i avoid those errors i Toast messages From Every method in Adapter and it shows data normal in Toast here is my Code

StoresList类

StoresList Class

public class StoresList {
String storeName,storeImage,storeId;

public StoresList(String storeId,String storeName, String storeImage) {
    this.storeName = storeName;
    this.storeImage = storeImage;
    this.storeId=storeId;
}

public String getStoreId() {
    return storeId;
}

public String getStoreName() {
    return storeName;
}

public String getStoreImage() {
    return storeImage;
}
}

StoreFragment类

StoreFragment class

  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.widget.GridLayoutManager;
  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.Request;
  import com.android.volley.RequestQueue;
  import com.android.volley.Response; 
  import com.android.volley.VolleyError;
  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.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;


  public class StoresFragment extends Fragment {
   RecyclerView storeRecyclerView;
   RecyclerView.Adapter adapter;
   List<StoresList> storesList;
   View  v;
public StoresFragment() {
    // Required empty public constructor

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

     v= inflater.inflate(R.layout.fragment_stores, container, false);
    storeRecyclerView=(RecyclerView)v.findViewById(R.id.stores_recycler_view);
    storeRecyclerView.setHasFixedSize(true);
    storeRecyclerView.setLayoutManager(new GridLayoutManager(v.getContext(),2));
    storesList=new ArrayList<>();

    loadStoresData();
    return v;
}
public void loadStoresData(){

    StringRequest storesStringRequest=new StringRequest(Request.Method.POST,
             Settings.STORES_PAGE, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            try {
                JSONArray storeArray=new JSONArray(s);

                for(int i=0;i<storeArray.length();i++){
                    JSONObject store=storeArray.getJSONObject(i);

                  storesList.add(new StoresList(store.getString("store_id"),store.getString("store_name"),Settings.SERVER_URl+store.getString("store_img")));

                   // Toast.makeText(v.getContext(), storesList.get(i).getStoreName(), Toast.LENGTH_SHORT).show();
                }
                adapter=new StoreListAdapter(v.getContext(),storesList);
               storeRecyclerView.setAdapter(adapter);


            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(v.getContext(), "incorrect json format", Toast.LENGTH_SHORT).show();
            }




        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {


        }
    }){
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("sub_cat_id","1");


            return params;
        }};
    RequestQueue storesRequestQueue= Volley.newRequestQueue(v.getContext());
    storesRequestQueue.add(storesStringRequest);

}



}

StoreListAdapter类

StoreListAdapter class

     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.ImageView;
     import android.widget.TextView;
     import android.widget.Toast;

     import com.squareup.picasso.Picasso;

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


   public class StoreListAdapter extends 
   RecyclerView.Adapter<StoreListAdapter.ViewHolder> {
    Context context;
    List<StoresList> stores;
    public StoreListAdapter(Context context, List<StoresList> stores) {
    this.context = context;
    this.stores = stores;

    }


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(context).inflate(R.layout.stores_card_view,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    StoresList store=stores.get(position);

    holder.storeName.setText(store.getStoreName());
    Picasso.with(context).load(store.getStoreImage()).into(holder.storeImage);



}

@Override
public int getItemCount()

{
    Toast.makeText(context,stores.size()+"FFFF", Toast.LENGTH_SHORT).show();
    return stores.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView storeImage;
    TextView storeName;


    public ViewHolder(View itemView) {
        super(itemView);
        Toast.makeText(context, "FFFF", Toast.LENGTH_SHORT).show();
        storeImage=(ImageView)itemView.findViewById(R.id.store_img);
        storeName=(TextView)itemView.findViewById(R.id.store_name);

    }
}
}

fragment_store xml

fragment_store xml

<FrameLayout 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"
android:background="#ffaacb"
tools:context="alprince.com.offers.StoresFragment">

<!-- TODO: Update blank fragment layout -->

    <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:id="@+id/stores_recycler_view"
      >

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

store_card_view XML

store_card_view XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"

>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"


    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="3dp"
    card_view:cardCornerRadius="6dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/store_img"
            android:src="@drawable/template"
            android:layout_gravity="center_horizontal"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/store_name"
            android:text="Store Name"
            android:layout_gravity="center_horizontal"
            />



      </LinearLayout>


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

  </LinearLayout>

当应用程序运行时,我的活动带有空的recylcerView

when application running i got this activity with empty recylcerView

08-22 05:51:51.271 27367-27367/? I/art: Late-enabling -Xcheck:jni
08-22 05:51:51.319 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.450 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.469 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.487 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.512 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.529 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.549 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.566 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.588 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.604 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.622 27367-27367/alprince.com.offers W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/alprince.com.offers-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@alprince.com.offers-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
08-22 05:51:51.623 27367-27367/alprince.com.offers W/System: ClassLoader referenced unknown path: /data/app/alprince.com.offers-1/lib/x86
08-22 05:51:51.625 27367-27367/alprince.com.offers I/InstantRun: Starting Instant Run Server for alprince.com.offers
08-22 05:51:55.893 27367-27367/alprince.com.offers 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
08-22 05:51:56.093 27367-27422/alprince.com.offers D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                     [ 08-22 05:51:56.096 27367:27367 D/         ]
                                                                     HostConnection::get() New Host Connection established 0xe9920ea0, tid 27367
08-22 05:51:56.262 27367-27422/alprince.com.offers D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
08-22 05:51:56.263 27367-27422/alprince.com.offers D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
08-22 05:51:56.272 27367-27422/alprince.com.offers D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so

                                                             [ 08-22 05:51:56.283 27367:27422 D/         ]
                                                             HostConnection::get() New Host Connection established 0xee912d50, tid 27422
08-22 05:51:56.381 27367-27422/alprince.com.offers I/OpenGLRenderer: Initialized EGL, version 1.4
08-22 05:51:56.783 27367-27422/alprince.com.offers W/EGL_emulation: eglSurfaceAttrib not implemented
08-22 05:51:56.783 27367-27422/alprince.com.offers W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee9138c0, error=EGL_SUCCESS
08-22 05:51:56.835 27367-27367/alprince.com.offers E/RecyclerView: No adapter attached; skipping layout
08-22 05:51:56.862 27367-27367/alprince.com.offers W/art: Before Android 4.1, method int 

推荐答案

每次设置新数据列表时,都应调用adapter.notifyDataSetChanged().

You should call adapter.notifyDataSetChanged() every time you setting new list of data.

看看文档.

一个好的解决方案是在适配器内部创建setItems(items)方法并在其内部调用notifyDataSetChanged().

A good solution would be to create setItems(items) method inside adapter and call notifyDataSetChanged() inside it.

这篇关于RecyclerView没有出现在片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆