Android回收视图片段无法正常工作

时间:2017-07-12 11:25:47

标签: java android android-fragments android-recyclerview android-volley

我的适配器

public class DepartmentsAdapter extends RecyclerView.Adapter<DepartmentsAdapter.ViewHolder>{

// Store a member variable for the contacts
private List<AppDepartments> mDepartments;
// Store the context for easy access
private Context mContext;


@Override
public DepartmentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View contactView = inflater.inflate(R.layout.app_department_item, parent, false);
    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(contactView);
    return viewHolder;
}

// Involves populating data into the item through holder
@Override
public void onBindViewHolder(DepartmentsAdapter.ViewHolder holder, int position) {
    AppDepartments appDepartments = mDepartments.get(position);
    // Set item views based on your views and data model
   /* holder.txtViewName.setText(appDepartments.getDeptName());
    holder.txtViewUser.setText(appDepartments.getDeptUser());*/
    holder.txtViewName.setText("HELLO");
    holder.txtViewUser.setText("ITS VICTOR");

}

@Override
public long getItemId(int i) {
    return 0;
}

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

我的片段

public class DiscoverFragment extends Fragment {

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.fragment_discover, container, false);
    departmentsAdapter = new DepartmentsAdapter(getActivity(),departList);
    // Lookup the recyclerview in activity layout
    RecyclerView displayDeparts = (RecyclerView) rootView.findViewById(R.id.appDepartments);
    displayDeparts.setAdapter(departmentsAdapter);
    displayDeparts.setLayoutManager(new LinearLayoutManager(getActivity()));
    // Inflate the layout for this fragment
    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

}

上面的代码返回一个空白屏幕,不显示文本视图。请帮助。 我已经注释掉了模型,以便我可以使用硬编码文本进行测试。 我正在关注这篇文章This is what it looks like.

6 个答案:

答案 0 :(得分:2)

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

(不确定它会完全解决您的问题)

答案 1 :(得分:1)

在设置适配器之前,您需要将LayoutManager设置为RecyclerView

像这样改变代码

displayDeparts.setLayoutManager(new LinearLayoutManager(getActivity()));
displayDeparts.setAdapter(departmentsAdapter);

答案 2 :(得分:1)

尝试从我的代码中移动代码onViewCreated,如下所示:

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

        pref = getActivity().getSharedPreferences("AppPreference", Context.MODE_PRIVATE);
        edit = pref.edit();

        swipe_container_reports = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container_reports);

        swipe_container_reports.setColorSchemeResources(
                R.color.colorAccent,
                R.color.colorPrimaryDark,
                R.color.cardview_dark_background);

        swipe_container_reports.setOnRefreshListener(this);

        //Recycler View
        recyclerView = (RecyclerView) view.findViewById(R.id.reports_recycler);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        swipe_container_reports.setRefreshing(true);

        reportingUserTask = (ReportingUserTask) new ReportingUserTask(getContext(), Integer.valueOf(pref.getString("UserId", "")));
        reportingUserTask.setOnFinishListener(this);
        reportingUserTask.execute();

    }

当任务完成通话时:

adapter = new ReportRecyclerAdapter((ArrayList<ReportDetail>) reportDetailList, Integer.valueOf(pref.getString("UserId", "")), getContext());
recyclerView.setAdapter(adapter);

这对我来说完美无缺

答案 3 :(得分:0)

在你的代码中:  setLayoutManager(new LinearLayoutManager(getActivity()))之前的setAdapter(departmentsAdapter)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_discover, container, false);
    displayDeparts.setLayoutManager(new LinearLayoutManager(getActivity()));
    departmentsAdapter = new DepartmentsAdapter(getActivity(), departList);
    // Lookup the recyclerview in activity layout
    RecyclerView displayDeparts = (RecyclerView) rootView.findViewById(R.id.appDepartments);
    displayDeparts.setAdapter(departmentsAdapter);
    // Inflate the layout for this fragment
    return rootView;
}

答案 4 :(得分:0)

尝试进行这些更改:

@Override
public long getItemId(int i) {
    return i;
}

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

答案 5 :(得分:0)

首先,你必须设置LayoutManager,然后将adapter设置为recyclerView,如下所示:

RecyclerView displayDeparts = (RecyclerView) rootView.findViewById(R.id.appDepartments);
departmentsAdapter = new DepartmentsAdapter(getActivity(),departList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
displayDeparts.setLayoutManager(mLayoutManager);
displayDeparts.setAdapter(departmentsAdapter);