繁体   English   中英

在片段中创建回收站视图?

[英]create recycler view in fragment?

我想创建一个片段并在回收站视图中显示产品...
但是当我渲染它时显示这个错误: RecyclerView: No adapter attach; 跳过布局

下面我复制我的代码;

此代码适用于适配器 class:

 private ArrayList<Products> ProductsList;
private Context context;

public Adapter(ArrayList<Products> productsList, Context context) {
    ProductsList = productsList;
    this.context = context;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false);
    return new MyHolder(v);
}

@Override
public void onBindViewHolder(MyHolder holder, final int position) {
    Products products = ProductsList.get(position);

    holder.txtName.setText(products.getName());
    holder.txtPrice.setText("$ " + products.getPrice());
    Picasso.get().load(Config.ipValue + "/images/" + products.getPhoto()).into(holder.imgV);
    holder.imgV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(AnimationUtils.loadAnimation(context, android
                    .R.anim.slide_in_left));
        }
    });

}

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

class MyHolder extends RecyclerView.ViewHolder {

    TextView txtName;
    TextView txtPrice;
    ImageView imgV;

    public MyHolder(View itemView) {
        super(itemView);

        txtName = itemView.findViewById(R.id.rowTxtProductName);
        txtPrice = itemView.findViewById(R.id.rowTxtPrice);
        imgV = itemView.findViewById(R.id.rowImgProduct);
    }
}

这一个用于 web api class:

public class WebApiHandler {

Context context;
String apiLink = "";
ArrayList<Products> products = new ArrayList<>();

public WebApiHandler(Context context) {
    this.context = context;
}

void apiConnect(String type) {
    switch (type) {
        case "getproducts": {
            apiLink = Config.getProductsWebApi;
            break;
        }
    }

    ProgressDialog dialog = ProgressDialog.show(context, "Connecting...",
            "please wait", false, false);
    StringRequest request = new StringRequest(Request.Method.POST,
            apiLink, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            dialog.dismiss();
            showJson(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            dialog.dismiss();
        }
    });
    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(request);
}

private void showJson(String response) {
    products.clear();
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray jsonArray = jsonObject.getJSONArray("response");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String id = object.getString("id");
            String name = object.getString("name");
            String description = object.getString("description");
            String price = object.getString("price");
            String photo = object.getString("photo");
            Products p = new Products(id, name, description, price, photo);
            products.add(p);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    VerticalFragment.productsArrayList = products;
    IData iData = (IData) context;
    iData.sendData();
}}

和我的片段代码:

public class VerticalFragment extends Fragment implements IData {

RecyclerView rcVertical;
WebApiHandler webApiHandler;
static ArrayList<Products> productsArrayList = new ArrayList<>();

public VerticalFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_vertical, container, false);
    rcVertical = view.findViewById(R.id.rcVertical);
    webApiHandler = new WebApiHandler(getContext());
    webApiHandler.apiConnect("getproducts");
    rcVertical.addOnItemTouchListener(new RecyclerTouchListener(getContext(), rcVertical,
            new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, int position) {
                    ProductActivity.products = productsArrayList.get(position);
                    startActivity(new Intent(getContext(), ProductActivity.class));
                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));

    return view;
}

@Override
public void sendData() {
    Adapter adapter = new Adapter(productsArrayList, getContext());
    rcVertical.setLayoutManager(new LinearLayoutManager((getContext())));
    rcVertical.setItemAnimator(new DefaultItemAnimator());
    rcVertical.setAdapter(adapter);
}}

我应该说我创建了一个接口并有一个方法我说它是 sendData

当显示 recyclerview 时没有适配器附加到 recyclerview 时,这是一个常见错误。 它不会对您的应用程序造成任何伤害,但您可以通过设置没有数据的空适配器来避免它,稍后当您获取数据时,将其提供给您的适配器并通知它

编辑 - 添加示例

在适配器中为您的列表创建设置器。 之后,在您的片段中将适配器设为全局,并在 onCreateView 中创建您的适配器实例并将其附加到 recyclerview

adapter = new Adapter(new ArrayList<>(), getContext());
rcVertical.setAdapter(adapter);
...Initialize recyclerview...

然后在您的 sendData 接口中将您加载的值放入其中

adapter.setData(productsArrayList);
adapter.notifyDataSetChanged();
 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM