用户界面未显示在片段中(RecyclerView)

时间:2020-02-16 12:49:07

标签: android android-fragments android-recyclerview

我用带有两个视图类型(Recycler的标题,行)的RecyclerView创建了一个片段 回收商的模型非常简单,它是关于产品的东西, 最后,适配器带有两种类型的回收站支架。 现在,我尝试运行模拟器来调用片段,但是回收站中的Ui没有显示, 我正在使用此过程来多次创建具有许多View类型的recyclerView,并且运行良好。 但是现在用户界面没有显示,也没有错误出现。

片段代码

public class SelectedCategoryFragment extends Fragment implements SelectedCtategoryAdapter.onRowListener {
private RecyclerView productRecyclerView;
private ArrayList<SelectedCategoryModel> categoryModelArrayList = new ArrayList<>();

public SelectedCategoryFragment() {
    // Required empty public constructor
}


@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_selected_category, container, false);
    initialize(v);
    setUpProductRecycler();

    return v;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void setUpProductRecycler() {
    productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(Objects.requireNonNull(getActivity()),
            LinearLayoutManager.VERTICAL);
    dividerItemDecoration.setDrawable(getActivity().getResources().getDrawable(R.drawable.slide_menu_divider));

    productRecyclerView.addItemDecoration(dividerItemDecoration);

    SelectedCtategoryAdapter selectedCtategoryAdapter = new SelectedCtategoryAdapter(productList(), this);

    productRecyclerView.setAdapter(selectedCtategoryAdapter);
}

private ArrayList<SelectedCategoryModel> productList() {

    SelectedCategoryModel p1 = new SelectedCategoryModel("category Name", "", "", "", R.drawable.sweets);
    categoryModelArrayList.add(p1);

    SelectedCategoryModel p2 = new SelectedCategoryModel("sweets", "sweets1", "30", "3", R.drawable.logo);
    categoryModelArrayList.add(p2);

    SelectedCategoryModel p3 = new SelectedCategoryModel("category Name", "sweets2", "44", "", R.drawable.sweets);
    categoryModelArrayList.add(p3);

    SelectedCategoryModel p4 = new SelectedCategoryModel("category Name", "sweets3", "88", "", R.drawable.sweets);
    categoryModelArrayList.add(p4);

    SelectedCategoryModel p5 = new SelectedCategoryModel("category Name", "sweets4", "101", "", R.drawable.sweets);
    categoryModelArrayList.add(p5);

    SelectedCategoryModel p6 = new SelectedCategoryModel("category Name", "sweets5", "45", "", R.drawable.sweets);
    categoryModelArrayList.add(p6);

    SelectedCategoryModel p7 = new SelectedCategoryModel("category Name", "sweets6", "90", "", R.drawable.sweets);
    categoryModelArrayList.add(p7);

    return categoryModelArrayList;
}

private void initialize(View v) {
    productRecyclerView = v.findViewById(R.id.product_recyclerview);
}

@Override
public void onRowClick(int position) {

}
}

**the model**

public class SelectedCategoryModel {

private String productName,productDesc,productRate,deliveryCost;
private int productPhoto;

public SelectedCategoryModel(String productName, String productDesc, String productRate, String deliveryCost, int productPhoto) {
    this.productName = productName;
    this.productDesc = productDesc;
    this.productRate = productRate;
    this.deliveryCost = deliveryCost;
    this.productPhoto = productPhoto;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public String getProductDesc() {
    return productDesc;
}

public void setProductDesc(String productDesc) {
    this.productDesc = productDesc;
}

public String getProductRate() {
    return productRate;
}

public void setProductRate(String productRate) {
    this.productRate = productRate;
}

public String getDeliveryCost() {
    return deliveryCost;
}

public void setDeliveryCost(String deliveryCost) {
    this.deliveryCost = deliveryCost;
}

public int getProductPhoto() {
    return productPhoto;
}

public void setProductPhoto(int productPhoto) {
    this.productPhoto = productPhoto;
}
}

适配器


public class SelectedCtategoryAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final int header_type = 0;
    private static final int row_type = 1;
    private ArrayList<SelectedCategoryModel>arrayList=new ArrayList<>();
    public int index = -1;
    public SelectedCtategoryAdapter.onRowListener onRowListener;

    public SelectedCtategoryAdapter() {
    }

public SelectedCtategoryAdapter(ArrayList<SelectedCategoryModel> arrayList, SelectedCtategoryAdapter.onRowListener onRowListener) {
    this.arrayList = arrayList;
    this.onRowListener = onRowListener;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view;
    RecyclerView.ViewHolder recyclerViewHolder;
    if (viewType == header_type) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
        recyclerViewHolder = new HeaderHolder(view, viewType, onRowListener);
        return recyclerViewHolder;


    } else if (viewType == row_type) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_items, parent, false);
        recyclerViewHolder = new RowHolder(view, viewType, onRowListener);
        return recyclerViewHolder;


    }
    return null;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,final int position) {

    if (holder instanceof SelectedCtategoryAdapter.HeaderHolder) {
        SelectedCtategoryAdapter.HeaderHolder headerHolder = (SelectedCtategoryAdapter.HeaderHolder) holder;
        headerHolder.header.setBackgroundResource(arrayList.get(position).getProductPhoto());

    } else if (holder instanceof SelectedCtategoryAdapter.RowHolder) {
        SelectedCtategoryAdapter.RowHolder rowHolder = (SelectedCtategoryAdapter.RowHolder) holder;
        Log.i("position>>", position + "");
        rowHolder.productName.setText(arrayList.get(position).getProductName());
        rowHolder.productDesc.setText(arrayList.get(position).getProductDesc());
        rowHolder.productRate.setText(arrayList.get(position).getProductRate());
        rowHolder.deliveryCost.setText(arrayList.get(position).getDeliveryCost());
        rowHolder.productPhoto.setImageResource(arrayList.get(position).getProductPhoto());
        rowHolder.categoryProductRow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onRowListener.onRowClick(position);
                index = position;
                notifyDataSetChanged();

            }
        });
        if (index == position) {
            rowHolder.categoryProductRow.setBackgroundColor(Color.parseColor("#A4CD39"));
        } else {
            rowHolder.categoryProductRow.setBackgroundColor(Color.parseColor("#90a3a3a3"));

        }
    }

}

@Override
public int getItemCount() {
    return 0;
}


public class HeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    SelectedCtategoryAdapter.onRowListener onRowListener;
    TextView headerTitle;
    LinearLayout header;
    public HeaderHolder(View v, int viewtype, SelectedCtategoryAdapter.onRowListener onRowListener) {
        super(v);
        headerTitle = v.findViewById(R.id.category_Header_title);
        header = v.findViewById(R.id.category_Header);
        this.onRowListener = onRowListener;
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onRowListener.onRowClick(getAdapterPosition());
    }
}


public class RowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView productPhoto;
    TextView productName,productDesc,productRate,deliveryCost;
    SelectedCtategoryAdapter.onRowListener onRowListener;
    LinearLayout categoryProductRow;

    public RowHolder(View v, int viewtype, SelectedCtategoryAdapter.onRowListener onRowListener) {
        super(v);
        productPhoto = v.findViewById(R.id.product_photo_1);
        productName = v.findViewById(R.id.product_name);
        productDesc = v.findViewById(R.id.product_desc);
        productRate = v.findViewById(R.id.product_rate_1);
        deliveryCost = v.findViewById(R.id.delivery_cost);
        categoryProductRow = v.findViewById(R.id.category_product_row);
        this.onRowListener = onRowListener;
        v.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        onRowListener.onRowClick(getAdapterPosition());
    }
}


@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return header_type;
    }
  else {
        return row_type;
    }
}

public interface onRowListener {
    void onRowClick(int position);
}

}


我可以请您检查一下这里的问题吗? 谢谢

1 个答案:

答案 0 :(得分:2)

我认为将适配器中的getItemCount更改可能会有帮助

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