向后导航时使用RecyclerView(导航组件)

时间:2020-03-30 21:33:46

标签: android android-recyclerview

有一个带有RecyclerView的RuleListFragment片段。第二个片段RuleFragment包含详细信息。返回时,RecyclerView消失。可能是什么问题呢?日志显示未分配适配器,但我不知道在哪里以及为什么。

public class RuleListFragment extends Fragment {
    private RecyclerView recyclerView;
    private RuleAdapter ruleAdapter;

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

    public void setRecyclerView(View view) {
        if (recyclerView == null) {
            Log.d(TAG, "new!");
            recyclerView = view.findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } else {
            Log.d(TAG, "old!");
        }

        if (ruleAdapter == null) {
            List<Rule> rules = Labs.get(getActivity()).getRules();
            ruleAdapter = new RuleAdapter(rules);
            recyclerView.setAdapter(ruleAdapter);
        } else {
            updateRecyclerView();
        }
    }

    public void updateRecyclerView() {
        Log.d(TAG, ruleAdapter == null ? "ruleAdapter = null" : "ruleAdapter != null");
        Log.d(TAG, recyclerView == null ? "recyclerView = null" : "recyclerView != null");
        Log.d(TAG, recyclerView.getAdapter() == null ? "recyclerView.getAdapter() = null" : "recyclerView.getAdapter() != null");
        Log.d(TAG, recyclerView.getLayoutManager() == null ? "recyclerView.getLayoutManager() = null" : "recyclerView.getLayoutManager() != null");
        List<Rule> rules = Labs.get(getActivity()).getRules();
        ruleAdapter.setRules(rules);
        ruleAdapter.notifyDataSetChanged();
    }

    private class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        Rule rule;
        ImageView icon;
        TextView title;

        private ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            icon = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
        }

        private void bind (Rule rule) {
            this.rule = rule;

            int iconId = getResources().getIdentifier(rule.getIcon(), "drawable", getActivity().getPackageName());
            icon.setImageResource(iconId);
            title.setText(rule.getTitle());
        }

        @Override
        public void onClick(View view) {
            Bundle args = new Bundle();
            args.putSerializable("ruleId", rule.getId());
        Navigation.findNavController(view).navigate(getLayout(rule.getType()), args);
        }
    }

    private int getLayout(int i) {
        switch (i) {
            case 1:
                return R.id.generalFragment;
            case 2:
                return R.id.leaderFragment;
            default:
                return R.id.ruleFragment;
        }
    }

    private class RuleAdapter extends RecyclerView.Adapter<ViewHolder> {
        private List<Rule> rules;
        private RuleAdapter (List<Rule> rules) {
            this.rules = rules;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.item_recycler_rule, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Rule rule = rules.get(position);
            holder.bind(rule);
        }

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

        public void setRules(List<Rule> rules) {
            this.rules = rules;
        }
    }
}
public class RuleFragment extends Fragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getArgument();

        setHasOptionsMenu(true);
    }

    public void getArgument() {
        int ruleId = getArguments().getInt("ruleId");
        rule = Labs.get(getActivity()).getRule(ruleId);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rule, container, false);

        ///

        return view;
    }



    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_rule, menu);

        ///

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.elect) {
            ///
        }
        return super.onOptionsItemSelected(item);
    }
}

登录首次启动

2020-03-31 02:25:50.190 25212-25212/? D/Constraints: new!

重新登录

020-03-31 02:25:59.023 25212-25212/ D/Constraints: old!
2020-03-31 02:25:59.023 25212-25212/ D/Constraints: ruleAdapter != null
2020-03-31 02:25:59.023 25212-25212/ D/Constraints: recyclerView != null
2020-03-31 02:25:59.023 25212-25212/ D/Constraints: recyclerView.getAdapter() != null
2020-03-31 02:25:59.023 25212-25212/ D/Constraints: recyclerView.getLayoutManager() != null
2020-03-31 02:25:59.065 25212-25212/ E/RecyclerView: No adapter attached; skipping layout

2 个答案:

答案 0 :(得分:0)

如果我没记错的话,我从某处读到似乎无法在RecyclerView中设置onCreateView,否则您可能会遇到类似您的问题。

您可以尝试将setRecyclerView(view)移到onViewCreated吗?

答案 1 :(得分:0)

我不知道这个决定的正确性,但是我重新编写了这样的代码。现在,无论适配器== null还是适配器,适配器都由RecyclerView分配! =空。还删除了onResume和notifyDataSetChanged()。仅当您需要更改RuleListFragment中的列表时,才使用后者。

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

    public void setRecyclerView() {
        if (ruleAdapter == null) {
            ruleAdapter = new RuleAdapter(Labs.get(getContext()).getRules());
        } else {
            ruleAdapter.setRules(Labs.get(getActivity()).getRules());
        }
        recyclerView.setAdapter(ruleAdapter);
    }