带有回收站视图的Android主机详细信息

时间:2017-12-08 05:58:22

标签: android android-fragments android-recyclerview recycler-adapter master-detail

我正在学习机器人,我对这个概念不熟悉。 这是我的适配器类。它显示错误,指出 CategoryList 不是 封闭类 。我无法理解错误。请帮助我理解它。提前谢谢。

public class CategoryAdapter extends RecyclerView.Adapter<CategoryHolder>    {

private final Context context;
private final List<Category> categories;

public CategoryAdapter(Context context, List<Category> categories) {
    this.context    = context;
    this.categories = categories;
}

@Override
public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    // Here it's showing an error: CategoryList is not an enclosing class.
    return new CategoryHolder(layoutInflater, parent);
}

@Override
public void onBindViewHolder(CategoryHolder holder, int position) {
    final Category category = categories.get(position);
    holder.bind(category);
}

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

这是我的第一个片段。它包含类别列表,如谷歌,脸书等。点击这些项目中的任何一项,它应该打开另一个片段,其中包含有关该类别的一些细节。

public class CategoryList extends Fragment {

private RecyclerView recyclerView;

public CategoryList() { }

private void updateUI() {
    List<Category> categories = new ArrayList<>();
    categories.add(new Category(1L, "Google", "Hello world!! This is Google."));
    categories.add(new Category(2L, "Facebook", "Hello world!! This is Facebook"));
    categories.add(new Category(3L, "WhatsApp", "Hello world!! This is WhatsApp"));
    categories.add(new Category(4L, "LinkedIn", "Hello world!! This is LinkedIn"));
    CategoryAdapter categoryAdapter = new CategoryAdapter(getActivity(), categories);
    recyclerView.setAdapter(categoryAdapter);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view     = inflater.inflate(R.layout.fragment_category_list, container, false);
    recyclerView  = (RecyclerView) view.findViewById(R.id.category_recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    updateUI();
    return view;
}

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

    private Category category;
    private final TextView name;
    private final TextView description;

    public CategoryHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.category_layout, parent, false));
        this.name        = (TextView) itemView.findViewById(R.id.category_name);
        this.description = (TextView) itemView.findViewById(R.id.category_description);
    }

    public void bind(final Category category) {
        this.category = category;
        this.name.setText(category.getName());
        this.description.setText(category.getDescription());
    }

    @Override
    public void onClick(View v) {
        Fragment fragment = CategoryDetail.getInstance(category);
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.detail_container, fragment)
                .commit();
    }
}
}

这是包含类别详细信息的片段。目前我还没有在updateUI()方法中写任何东西。

public class CategoryDetail extends Fragment {

private Category category;

public CategoryDetail() { }

public static CategoryDetail getInstance(Category category) {
    CategoryDetail categoryDetail   = new CategoryDetail();
    categoryDetail.category         = category;
    return categoryDetail;
}

private void updateUI() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.category_detail, container, false);
    updateUI();
    return view;
}
}

1 个答案:

答案 0 :(得分:0)

您没有创建ViewHolder类。根据您的代码,您想创建

public class CategoryHolder extends RecyclerView.ViewHolder {
public CategoryHolder(View view) {
//code for the constructor for the ViewHolder goes here.
}
}