RecyclerView没有出现在Fragment中

时间:2017-08-22 09:39:48

标签: android android-recyclerview

我向Fragment添加了一个recyclerView,但它没有显示片段旁边的任何内容 我发现了很多同样问题的问题,但实际上我避免了这些错误 i Toast消息来自适配器中的每个方法,它在Toast中显示正常的数据 这是我的代码

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类

  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类

     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

<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

<?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

进行此活动

enter image description here

  

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 

3 个答案:

答案 0 :(得分:0)

您需要更改

adapter = new StoreListAdapter(v.getContext(), storesList);
storeRecyclerView.setAdapter(adapter);`

adapter = new StoreListAdapter(getActivity(), storesList);
storeRecyclerView.setAdapter(adapter);

答案 1 :(得分:0)

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

查看documentation

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

答案 2 :(得分:0)

代码似乎没问题,但我怀疑Tablayout存在问题。在Main类[其中你将适配器设置为ViewPager]链接

ViewPager_Adapter_Class  adapter= new ViewPager_Adapter_Class (getChildFragmentManager());

我认为你犯的错误可能在这里:

use getChildFragmentManager() instead getFragmentManager()

ViewPager_Adapter_Class:

public class ViewPager_Adapter_Class extends FragmentStatePagerAdapter {  
    public AppLevelMainFragmentAdapter(FragmentManager fm) {
        super(fm);
    }
  .................etc..,
}

因为Tablayout类可能是片段,其中你在tablayout中加载片段。如果你使用getFragmentManager()/ getSupportFragmentManager,它基本上表现得像活动生命周期,所以你需要使用getChildFragmentManager()。这是我怀疑它会对你有所帮助。