在我的应用程序中,我的一个片段中有一个RecyclerView
我正在使用Reterofit
从服务器获得响应。一切都很好,但我的RecylerView
没有显示从我的服务返回的任何数据。
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 :(得分:2)
在 offersRecyclerView.addItemDecoration();
之后,在 onResponse 中添加以下行offersRecyclerView.setAdapter(offerRecyclerAdapter);
希望这会有所帮助。
答案 1 :(得分: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) {
}
});
}