Android RecycleView适配器有时不会调用

时间:2015-12-14 09:02:49

标签: android android-recyclerview recycler-adapter

我在RecycleView中遇到了问题。我的屏幕有两个标签,两个标签都包含recycleview。它通常在大多数时间调用,但有时会显示空白屏幕。只是我写了log& system.out并发现了这个问题。我将在此粘贴我的适配器类代码。

    public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int HEADER = 0;
    public static final int CHILD = 1;
    List<ContentTitle> titles;
    Context context;


    private List<Item> titleData = new ArrayList<Item>();

    public ExpandableListAdapter(List<Item> data) {

        this.titleData = data;
        System.out.println("===this.titleData===" + this.titleData.size());
    }

    public void replaceContentFragment(String title,int id,int bookmarked) {
        try {

            SkillsApp SkillsApp = (SkillsApp) context.getApplicationContext();
            Fragment fragment = new ContentActivityFragment();
            FragmentManager fragmentManager = skillsUSAApp.mainActivity.getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.commit();
            SkillsApp.subtitleID = id;
            SkillsApp.isbookmark = (bookmarked == 0)? false:true;
            SkillsApp.mainActivity.getSupportActionBar().setTitle(title);
            SkillsApp.selectedSubtitleID = id;

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
        View view = null;
        try{

            context = parent.getContext();
            final SkillsApp skillsApp = (SkillsAApp)context.getApplicationContext();

            if(!skillsApp.isDrawer){
                switch (type) {
                    case HEADER:
                        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        view = inflater.inflate(R.layout.main_title, parent, false);
                        ListHeaderViewHolder header = new ListHeaderViewHolder(view);
                        return header;
                    case CHILD:
                        LayoutInflater inflater1 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        view = inflater1.inflate(R.layout.sub_title, parent, false);
                        view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Log.d("Child", "child");
                                TextView text = (TextView) view.findViewById(R.id.header_title);
                            }
                        });
                        return new RecyclerView.ViewHolder(view) {
                        };
                }
            }


        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        try{
            System.out.println("==========##########");
            final Item item = titleData.get(position);
            final SkillsAApp skillsAApp = (SkillsAApp)context.getApplicationContext();
            System.out.println("===onBindViewHolder item===" + item.mainTitle.title);
            switch (item.type) {
                case HEADER:

                    final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
                    itemController.refferalItem = item;
                    itemController.header_title.setText(item.mainTitle.title);

                    if (item.invisibleChildren == null) {
                        itemController.btn_expand_toggle.setImageResource(R.drawable.right_filled_arrow);
                    } else {
                        itemController.btn_expand_toggle.setImageResource(R.drawable.right_filled_arrow);
                    }

                    itemController.layoutItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try{

                                if(!skillsAApp.isDrawer){
                                    if (item.invisibleChildren == null) {
                                        item.invisibleChildren = new ArrayList<Item>();
                                        int count = 0;
                                        int pos = titleData.indexOf(itemController.refferalItem);
                                        while (titleData.size() > pos + 1 && titleData.get(pos + 1).type == CHILD) {
                                            item.invisibleChildren.add(titleData.remove(pos + 1));
                                            count++;
                                        }
                                        notifyItemRangeRemoved(pos + 1, count);
                                        notifyDataSetChanged();
                                        itemController.btn_expand_toggle.setImageResource(R.drawable.right_filled_arrow);
                                    } else {
                                        int pos = titleData.indexOf(itemController.refferalItem);
                                        int index = pos + 1;
                                        for (Item i : item.invisibleChildren) {
                                            titleData.add(index, i);
                                            index++;
                                        }
                                        notifyItemRangeInserted(pos + 1, index - pos - 1);
                                        notifyDataSetChanged();
                                        itemController.btn_expand_toggle.setImageResource(R.drawable.down_arrow);
                                        item.invisibleChildren = null;
                                    }

                                }

                            }catch (Exception e){
                                e.printStackTrace();
                            }

                        }
                    });

                    break;

                case CHILD:
                    View itemTextView = (View) holder.itemView;
                    TextView view = (TextView) itemTextView.findViewById(R.id.header_title);
                    view.setText(titleData.get(position).subtitle.title);
                    final int pos = position;
                    itemTextView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            try{
                                if(!skillsUSAApp.isDrawer){
                                    TextView text = (TextView) view.findViewById(R.id.header_title);
                                    FlurryAgent.logEvent("Sub Category Item");
                                    replaceContentFragment(text.getText().toString(), titleData.get(pos).subtitle.subtitleId, titleData.get(pos).subtitle.bookMarked);
                                }

                            }catch (Exception e){
                                e.printStackTrace();
                            }

                        }
                    });

                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return titleData.get(position).type;
    }

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

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

    private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
        public TextView header_title;
        public ImageView btn_expand_toggle;
        public Item refferalItem;
        public RelativeLayout layoutItem;

        public ListHeaderViewHolder(View itemView) {
            super(itemView);
            try{
                header_title = (TextView) itemView.findViewById(R.id.header_title);
                btn_expand_toggle = (ImageView) itemView.findViewById(R.id.btn_expand_toggle);
                layoutItem = (RelativeLayout) itemView.findViewById(R.id.ll_title);

            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    public static class Item {
        public int type;
        public String text;
        public ContentTitle mainTitle;
        public ContentSubTitle subtitle;
        public List<Item> invisibleChildren;

        public Item(int type, String text, List<Item> children) {
            this.type = type;
            this.text = text;
            this.invisibleChildren = children;
        }
        public Item(int type, ContentTitle title,ContentSubTitle stitle) {
            this.type = type;
            this.mainTitle = title;
            this.subtitle=stitle;
        }

        public Item(int type, String text) {
            this.type = type;
            this.text = text;
        }
    }
}

这是来自标签片段

的调用适配器
recyclerview = (RecyclerView) v.findViewById(R.id.recyclerview);
            recyclerview.setLayoutManager(new LinearLayoutManager(v.getContext(), LinearLayoutManager.VERTICAL, false));
          //  recyclerview.setLayoutManager(new LinearLayoutManager(v.getContext()));
            ExpandableListAdapter adapter = new ExpandableListAdapter(items);
            recyclerview.setAdapter(adapter);
            adapter.notifyDataSetChanged();
你能告诉我什么是问题吗?我没有收到任何控制台错误消息。请你帮我这里的任何人

0 个答案:

没有答案