片段未加载到主框架中。在 api 通过改造调用 Android

Fragment not getting loaded in main frame. on api call through retrofit in Android

这是我在导航抽屉中使用的片段之一。我从 api 填充数据并显示在 recyclerview 中。我已经检查了日志,它在日志中显示了数据,但回收站视图中没有填充数据。我尝试使用断点和日志。数据来自数组列表,我还尝试使用正在显示的静态数据。我无法理解我失踪的地方。由于这个片段,其他片段也没有加载到主框架中。它看起来是空白的。提前致谢。

public class HomeFragment extends Fragment {

private Context mContext;
private RecyclerView rvMain;
ArrayList<DataModel> arrayList;
private ProgressBar pb;
private ApiInterface apiInterface;

public HomeFragment(Context mContext) {
    this.mContext = mContext;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

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

    Log.d("Fragment", "onViewCreated: ");
    rvMain = view.findViewById(R.id.main_category_rv);
    pb = view.findViewById(R.id.main_pb);
    arrayList = new ArrayList<>();

    prepareData();
    pb.setVisibility(View.GONE);
}

private void prepareData() {
    pb.setVisibility(View.VISIBLE);
    apiInterface = ApiRequest.createService(ApiInterface.class);

    Call<MainCategoryModel> call = apiInterface.readJson();

    call.enqueue(new Callback<MainCategoryModel>() {
        @Override
        public void onResponse(Call<MainCategoryModel> call, Response<MainCategoryModel> response) {
            if (response.isSuccessful()) {
                for (int i = 0; i < response.body().getData().getCategory().size(); i++) {
                    arrayList.add(new DataModel(response.body().getData().getCategory().get(i).getTitle(), response.body().getData().getCategory().get(i).getImage()));
                }
            } else {
                Log.d("Response", "onResponse: " + response.errorBody());
            }
        }

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

        }
    });
    DataAdapter adapter = new DataAdapter(mContext, arrayList, new DataAdapter.RecyclerViewClickListener() {
        @Override
        public void onClick(View view, int position) {

        }
    });
    rvMain.setAdapter(adapter);

    AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(mContext, 500);
    rvMain.setLayoutManager(layoutManager);
  }
}

首先,在 activity 中使您的适配器在任何方法之外成为全局

DataAdapter adapter;

然后你需要通知你的适配器你已经添加了数据

yourAdapter.notifyDataSetChanged();

你的 prepareData 方法应该是这样的

private void prepareData() {
    pb.setVisibility(View.VISIBLE);
    apiInterface = ApiRequest.createService(ApiInterface.class);

    adapter = new DataAdapter(mContext, arrayList, new DataAdapter.RecyclerViewClickListener() {
        @Override
        public void onClick(View view, int position) {

        }
    });

    AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(mContext, 500);

    Call<MainCategoryModel> call = apiInterface.readJson();

    call.enqueue(new Callback<MainCategoryModel>() {
        @Override
        public void onResponse(Call<MainCategoryModel> call, Response<MainCategoryModel> response) {
            if (response.isSuccessful()) {
                for (int i = 0; i < response.body().getData().getCategory().size(); i++) {
                    arrayList.add(new DataModel(response.body().getData().getCategory().get(i).getTitle(), response.body().getData().getCategory().get(i).getImage()));
                }
                adapter.notifyDataSetChanged(); // notify your adapter here
            } else {
                Log.d("Response", "onResponse: " + response.errorBody());
            }
        }

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

        }
    });
    rvMain.setAdapter(adapter);
    rvMain.setLayoutManager(layoutManager);
  }
}

为什么静态数据显示列表,因为它是同步的,但是改造入队使用异步调用,它可能需要很少的时间来获取数据