我想用 Retrofit 制作类似 Spotify 主页视图的东西,但我不明白该怎么做。请帮助他我很困惑。请帮助他这些是我的 XML 代码看起来像这些。第一个是主布局Recyclerview,第二个布局是Recyclerview RowItem List,另一个是innerItem 行列表。我想获得类似这样的东西,如何获得像 Spotify 这样的实际输出。我想展示这些,但它们给了我相同的数据,但我有不同的数据和不同的数组列表。然后如何显示不同的数据。
主要 Recyclerview.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/list_product"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
RecyclerviewItem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="@dimen/_5sdp">
<TextView
android:id="@+id/tvCatName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/_10sdp"
android:text="cat name"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/catProductList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Inneritem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="@dimen/_160sdp"
android:layout_height="@dimen/_170sdp"
app:cardPreventCornerOverlap="false">
<ImageView
android:id="@+id/imgProduct"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_bags"/>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/tvProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yello New Shirt"
android:layout_gravity="center"
android:padding="@dimen/_5sdp"
android:textStyle="bold"
android:layout_marginTop="@dimen/_2sdp"
android:textSize="@dimen/_16ssp"
android:singleLine="true"/>
<TextView
android:id="@+id/tvProductPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15.00"
android:layout_gravity="center"
android:padding="@dimen/_5sdp"
android:textStyle="bold"
android:layout_marginTop="@dimen/_2sdp"
android:textSize="@dimen/_13ssp"
android:singleLine="true"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我的代码我不知道有什么问题
Data.java
public class Data {
String categoryName;
ArrayList<ProductsModel> productsModels;
public Data() {
}
public Data(String categoryName, ArrayList<ProductsModel> productsModels) {
this.categoryName = categoryName;
this.productsModels = productsModels;
}
public ArrayList<ProductsModel> getProductsModels() {
return productsModels;
}
public void setProductsModels(ArrayList<ProductsModel> productsModels) {
this.productsModels = productsModels;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
}
InCategoryProductAdapter.java
public class InCategoryProductAdapter extends RecyclerView.Adapter<InCategoryProductAdapter.ViewHolder> {
private Context context;
private ArrayList<Data> dataArrayList;
private RecyclerView.RecycledViewPool recycledViewPool;
public InCategoryProductAdapter(Context context, ArrayList<Data> dataArrayList) {
this.context = context;
this.dataArrayList = dataArrayList;
recycledViewPool = new RecyclerView.RecycledViewPool();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_row_list,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.tvCatName.setText(dataArrayList.get(i).getCategoryName());
LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
viewHolder.catProductList.setLayoutManager(manager);
InnerItemAdapter innerItemAdapter = new InnerItemAdapter(context,dataArrayList.get(i).getProductsModels());
viewHolder.catProductList.setAdapter(innerItemAdapter);
}
@Override
public int getItemCount() {
return dataArrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView tvCatName;
RecyclerView catProductList;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvCatName = itemView.findViewById(R.id.tvCatName);
catProductList = itemView.findViewById(R.id.catProductList);
}
}
}
Fragment.java
/*set Catgeory Api if get department ID*/
private void setCategory(int dept_id) {
models = new ArrayList<>();
RetrofitInterface retrofitInterface = RetrfitClient.getRetrofitClient().create(RetrofitInterface.class);
Call<ArrayList<CategoryModel>> arrayListCall = retrofitInterface.Category_Call(dept_id);
arrayListCall.enqueue(new Callback<ArrayList<CategoryModel>>() {
@Override
public void onResponse(Call<ArrayList<CategoryModel>> call, Response<ArrayList<CategoryModel>> response) {
models = response.body();
for (int i = 0; i < models.size(); i++) {
catId = models.get(i).getCategory_id();
setCategoryProduct(catId, models);
}
//Log.d("ModelsSize", "" + models.size());
}
@Override
public void onFailure(Call<ArrayList<CategoryModel>> call, Throwable t) {
Toast.makeText(getActivity(), t.toString(), Toast.LENGTH_SHORT).show();
}
});
}
/*get category wise product From Server*/
私人无效 setCategoryProduct(int catId, final ArrayList categoryModels, final String catName) {
Log.d("getSingleCat", "" + catId);
productsModels = new ArrayList<>();
RetrofitInterface anInterface = RetrfitClient.getRetrofitClient().create(RetrofitInterface.class);
Call<ProductInCategoryResponse> inCategoryResponseCall = anInterface.IN_CATEGORY_RESPONSE_CALL(catId, 1);
inCategoryResponseCall.enqueue(new Callback<ProductInCategoryResponse>() {
@Override
public void onResponse(Call<ProductInCategoryResponse> call, Response<ProductInCategoryResponse> response) {
productsModels = response.body().getRows();
Log.d("productCat", "" + productsModels.size());
data.setCategoryName(catName);
data.setProductsModels(productsModels);
dataArrayList = new ArrayList<>();
HashMap<String,ArrayList<ProductsModel>> listHashMap = new HashMap<String, ArrayList<ProductsModel>>();
for (int j=0;j<categoryModels.size();j++){
dataArrayList.add(data);
listHashMap.put(categoryModels.get(j).getName(), productsModels);
Log.d("ListMap",""+listHashMap.put(categoryModels.get(j).getName(),productsModels));
}
Log.d("CatName",""+catName);
/*Gson gson = new Gson();
String pList = gson.toJson(productsModels);*/
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
mainRecyclerView.setLayoutManager(manager);
InCategoryProductAdapter inCategoryProductAdapter = new InCategoryProductAdapter(getActivity(), dataArrayList);
mainRecyclerView.setAdapter(inCategoryProductAdapter);
/*
Log.d("DataArray", "" + dataArrayList.size());
Log.d("GetCat", "" + data.getCategoryName());*/
// Log.d("DataList",""+dataArrayList.get(i).getCategoryName() + " :"+ dataArrayList.get(i).getProductsModels());
}
@Override
public void onFailure(Call<ProductInCategoryResponse> call, Throwable t) {
Toast.makeText(getActivity(), t.toString(), Toast.LENGTH_SHORT).show();
}
});
}