【问题标题】:RecyclerView doesn't appear in FragmentRecyclerView 没有出现在 Fragment 中
【发布时间】:2017-08-22 09:39:47
【问题描述】:

我向 Fragment 添加了一个 recyclerView,但它在 Fragment 中没有显示任何东西 我发现了很多关于同一个问题的问题,但实际上我避免了这些错误 i Toast 消息来自 Adapter 中的 Every 方法,它在 Toast 中显示数据正常 这是我的代码

StoresList 类

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>

当应用程序运行时,我得到了这个带有空 recyclerView 的活动

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 

【问题讨论】:

  • 发布您的 xml 布局
  • 同时发布您的日志
  • 添加了 xml lyout @an_droid_dev
  • 没有错误日志@FlorescuGeorgeCătălin
  • @MokhtarGhaleb 日志并不意味着总是错误

标签: android android-recyclerview


【解决方案1】:

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

看看documentation

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

【讨论】:

  • 在 recyclerview 上设置适配器后不需要调用adapter.notifyDataSetChanged()。当您想要使您的数据和/或视图无效时调用它。使用setItems() 方法添加新数据后,应调用notifyItemInserted(position)notifyItemRangeInserted(int positionStart, int itemCount)
  • 我不认为它与 notifyDataSetChanged() 相关,因为我在另一个活动而不是片段上运行类似的代码,它工作正常
【解决方案2】:

你需要改变

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

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

【讨论】:

  • 您是否在storesList 这个数组列表中获取数据?检查一次
  • 是的,我检查了每种方法中的数据,我让它们正常,我在 Toast 中显示它们并成功返回
【解决方案3】:

代码似乎没问题,但我怀疑 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()。这是我怀疑的希望它会帮助你。

【讨论】:

  • toolbarJAVA=(Toolbar)findViewById(R.id.toolbarXML); tabLayoutJAVA=(TabLayout)findViewById(R.id.tablayoutXML); pager=(ViewPager)findViewById(R.id.viewPagerXML); setSupportActionBar(工具栏JAVA); pagerAdapter=新 ViewPagerAdapter(getSupportFragmentManager()); pagerAdapter.addFragment(new StoresFragment(),"المتاجر"); pagerAdapter.addFragment(new OffersFragment(),"العروض"); pager.setAdapter(pagerAdapter); tabLayoutJAVA.setupWithViewPager(pager);
  • pagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());替换为 pagerAdapter=new ViewPagerAdapter(getChildFragmentManager());
  • 将 store_card_view XML 中的高度根 ViewGroup 从 match_parent 更改为 wrap_content ...................... 即 schemas.android.com/apk/res/android" xmlns:card_view="schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> 运行它
  • 我还没有在谷歌上搜索这条消息,我在 logCat E/RecyclerView 中找到了它:没有附加适配器;跳过布局
  • 你做了这个改变吗 --> 将 store_card_view XML 中的高度根 ViewGroup 从 match_parent 更改为 wrap_content
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2017-09-26
  • 2020-08-27
  • 2018-01-08
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多