2016-11-27 88 views
0

在我的应用程序中,我在我的片段之一中使用RecyclerView,其中我从服务器使用Reterofit获得响应。每件事情都很好,但我的RecylerView没有显示从我的服务返回的任何数据。即使是RecyclerView不显示任何数据

int count = offerRecyclerAdapter.getItemCount(); 

我的适配器正在返回从服务器返回正确数量的数据计数。

内部片段:

@Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     offersRecyclerView = (RecyclerView) view.findViewById(R.id.offer_recycler); 

     MotorCityArabiaGlobal global = (MotorCityArabiaGlobal) getActivity().getApplication(); 
     ApiInterface apiService=global.getClient().create(ApiInterface.class); 
     Call<OfferResponse> call = apiService.getOffers(4); 
     call.enqueue(new Callback<OfferResponse>() { 
      @Override 
      public void onResponse(Call<OfferResponse> call, Response<OfferResponse> response) { 
       OfferResponse offers = response.body(); 
       if(offers!=null){ 

        List<Offer> result = response.body().getResult(); 
        offerRecyclerAdapter = new OffersAdapter(getActivity(),result); 

        offersRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
        offersRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
        offersRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL)); 
        offersRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { 
         @Override 
         public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
          return false; 
         } 

         @Override 
         public void onTouchEvent(RecyclerView rv, MotionEvent e) { 

         } 

         @Override 
         public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

         } 
        }); 

        int count = offerRecyclerAdapter.getItemCount(); 


        offerRecyclerAdapter.notifyDataSetChanged(); 


       } 
      } 

      @Override 
      public void onFailure(Call<OfferResponse> call, Throwable t) { 

      } 
     }); 
    } 

适配器类别:

public class OffersAdapter extends RecyclerView.Adapter<OffersAdapter.MyViewHolder> { 
    private List<Offer> offersList= Collections.emptyList(); 
    private LayoutInflater inflater; 
    Context ctx; 

    public OffersAdapter(Context ctx,List<Offer> offersList) { 
     this.ctx = ctx; 
     this.inflater = LayoutInflater.from(ctx); 
     this.offersList = offersList; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
//  View view = LayoutInflater.from(parent.getContext()) 
//    .inflate(R.layout.offer_list_layout, parent, false); 
     View view = inflater.inflate(R.layout.offer_list_layout,parent,false); 
     return new MyViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     Offer offer = offersList.get(position); 
     holder.title.setText(offer.getOffer_title()); 
     holder.price.setText(offer.getPrice()); 
     holder.offerCount.setText(offer.getOffer_count()); 

    } 

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

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView title, offerCount, price; 
     public ImageView image; 

     public MyViewHolder(View view) { 
      super(view); 
      title = (TextView) view.findViewById(R.id.title); 
      offerCount = (TextView) view.findViewById(R.id.offer_count); 
      price = (TextView) view.findViewById(R.id.price); 
      image = (ImageView)view.findViewById(R.id.car_img); 
     } 
    } 
} 

home_layout.xml

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

    > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/header_wrapper" 
    > 
    <!-- Row1--> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 

     > 
     <!-- Column1 row 1--> 
      <LinearLayout 
       android:layout_width="140dp" 
       android:layout_height="125dp" 
       android:layout_weight="1" 
       android:background="@drawable/new_car_selector" 
       android:clickable="true" 

       android:layout_marginTop="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginBottom="5dp" 
       ></LinearLayout> 

     <!-- Column2 row 1--> 
     <LinearLayout 
      android:layout_width="140dp" 
      android:layout_height="125dp" 
      android:layout_weight="1" 
      android:background="@drawable/find_car_selector" 
      android:clickable="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 

      android:layout_marginBottom="5dp" 
      ></LinearLayout> 


    </LinearLayout> 

    <!-- Row2--> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="2" 

     > 
     <!-- Column1 row 2--> 


     <!-- Column2 row 2--> 
     <LinearLayout 
      android:layout_width="140dp" 
      android:layout_height="125dp" 
      android:layout_weight="1" 
      android:background="@drawable/sell_car_selector" 
      android:clickable="true" 

      android:layout_marginTop="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginBottom="10dp" 
      ></LinearLayout> 

     <LinearLayout 
      android:layout_width="140dp" 
      android:layout_height="125dp" 
      android:layout_weight="1" 
      android:background="@drawable/compare_car_selector" 
      android:clickable="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 

      android:layout_marginBottom="10dp" 
      ></LinearLayout> 


    </LinearLayout> 

</LinearLayout> 
    <!-- Header Wrapper ends--> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/header_wrapper" 
     android:id="@+id/offers_recycler_wrapper" 
     android:orientation="vertical" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="CAR OFFERS" 
      android:textColor="@color/colorAccentDark" 
      android:textStyle="bold" 
      android:textSize="16sp" 
      android:layout_marginLeft="5dp" 

      /> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/offer_recycler" 
      ></android.support.v7.widget.RecyclerView> 

    </LinearLayout> 
</RelativeLayout> 

offer_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="?android:attr/selectableItemBackground" 
    android:orientation="vertical" 
    android:paddingTop="6dp" 
    android:paddingBottom="5dp" 
    > 
    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="60dp" 
     android:src="@drawable/sell_car_select" 
     android:id="@+id/car_img" 

     /> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/meta_data" 
     android:orientation="vertical" 
     android:layout_toEndOf="@id/car_img" 
     android:layout_marginLeft="5dp" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TITLE" 
      android:textSize="16dp" 
      android:textColor="#000" 
      android:textStyle="bold" 
      android:id="@+id/title" 

      /> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginTop="3dp" 
      > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/price" 
       android:text="13,000" 
       android:textStyle="bold" 
       android:textColor="@color/colorAccentDark" 
       android:textSize="16dp" 
       /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/curr" 
       android:textColor="#BDBDBD" 
       android:text=" SAUDI RAYAL" 
       android:textSize="12dp" 

       /> 


     </LinearLayout> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/offer_count_wrapper" 
    android:orientation="horizontal" 
    android:layout_marginTop="3dp" 
    > 
<ImageView 
    android:layout_width="12dp" 
    android:layout_height="12dp" 
    android:src="@drawable/ic_offer_orange" 
    /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="12dp" 
     android:text=" 3 Offers" 
     android:id="@+id/offer_count" 
     /> 
</LinearLayout> 


    </LinearLayout> 

</RelativeLayout> 
+0

试着改变你的RecyclerView'安卓layout_height'到'match_parent' – Budius

+0

@Budius它没有工作,你把代码很多 –

+0

,所以这是一个建议,什么似乎更明显,我。但@ SepJapRo2.4答案似乎对我来说。您忘了将适配器添加到回收站 – Budius

回答

0

您没有将适配器设置为RecyclerView。使用setAdapter方法。请看下面的代码,

List<Offer> results = new ArrayList<Offer>(); 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    offersRecyclerView = (RecyclerView) view.findViewById(R.id.offer_recycler); 
    offersRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    offersRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
    offersRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL)); 

    offerRecyclerAdapter = new OffersAdapter(getActivity(), results) 
    offersRecyclerView.setAdapter(offerRecyclerAdapter); 

    offersRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { 
     @Override 
     public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
      return false; 
     } 

     @Override 
     public void onTouchEvent(RecyclerView rv, MotionEvent e) { 

     } 

     @Override 
     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

     } 
    }); 


    MotorCityArabiaGlobal global = (MotorCityArabiaGlobal) getActivity().getApplication(); 
    ApiInterface apiService=global.getClient().create(ApiInterface.class); 
    Call<OfferResponse> call = apiService.getOffers(4); 
    call.enqueue(new Callback<OfferResponse>() { 
     @Override 
     public void onResponse(Call<OfferResponse> call, Response<OfferResponse> response) { 
      OfferResponse offers = response.body(); 
      if(offers != null){ 

       List<Offer> result = response.body().getResult(); 
       results.clear(); 
       results.addAll(result); 
       offerRecyclerAdapter.notifyDataSetChanged(); 
      } 
     } 

     @Override 
     public void onFailure(Call<OfferResponse> call, Throwable t) { 

     } 
    }); 
} 
+1

呃,我忘了连接适配器。感谢它现在的工作。 –

2

加入线之下的onResponseoffersRecyclerView.addItemDecoration();

offersRecyclerView.setAdapter(offerRecyclerAdapter); 

希望这有助于。