繁体   English   中英

布尔不起作用在RecyclerView中单击按钮

[英]Boolean not working on button click in RecyclerView

我为我的RecyclerView在适配器内创建了两个booleans 但是,由于某些原因,当单击相关按钮时,它们似乎不起作用,但是Toast却起作用。 有谁知道为什么booleans不能正常工作以及如何解决这个问题?

片段类

public class TabFragmentRV extends android.support.v4.app.Fragment {
    RecyclerView mRecyclerView;

    public TabFragmentRV() {}

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

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        mRecyclerView = v.findViewById(R.id.my_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        super.onActivityCreated(savedInstanceState);

        initRVAdapter();
    }

    private void initRVAdapter(){
        List<Object> itemsList = new ArrayList<>();

        // add items to the list
        itemsList.add(new MainHeader("Expand all", "Collapse all"));
        itemsList.add(new RVLineSeparator());
        itemsList.add(new SMSmessage("Item A","Item A description"));
        itemsList.add(new Phonecall("Item B","Item B description"));
        itemsList.add(new RVLineSeparator());
        itemsList.add(new RVSectionWith4CVs("Section W","W1","W1a","W2","W2a", "W3", "W3a", "W4", "W4a"));
        itemsList.add(new RVLineSeparator());
        itemsList.add(new RVTable("Item F1", "50ペンス", "50 펜스", "0,50 £", "50 пенсов"));
        itemsList.add(new RVLineSeparator());
        itemsList.add(new Phonecall("Item G1","Item G1 description"));

        RVItemsAapter itemsListAdapter = new RVItemsAapter(getContext());
        mRecyclerView.setAdapter(itemsListAdapter);

        itemsListAdapter.setCallSMSFeed(itemsList);
        itemsListAdapter.notifyDataSetChanged();
    }
}

适配器类

public class RVItemsAapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final static int TYPE_HEADER = 1, TYPE_EXPANDABLE = 2, TYPE_NONEXPANDABLE = 3, TYPE_SECTIONWITH4CARDS = 4, TYPE_TABLE = 5, TYPE_SEPARATOR = 6;
    private ArrayList myArrayList = new ArrayList();
    private boolean expandedAll;
    private boolean collapsedAll;

    private Context context;

//    private List items;

    public RVItemsAapter(Context context) { this.context = context}

    public void setCallSMSFeed(List<Object> myArrayList){
        this.myArrayList = (ArrayList) myArrayList;
    }

    @Override
    public int getItemViewType(int position) {
        if (myArrayList.get(position) instanceof MainHeader) {
            return TYPE_HEADER;
        } else if (myArrayList.get(position) instanceof Phonecall) {
            return TYPE_EXPANDABLE;
        } else if (myArrayList.get(position) instanceof SMSmessage) {
            return TYPE_NONEXPANDABLE;
        } else if (myArrayList.get(position) instanceof RVSectionWith4CVs) {
            return TYPE_SECTIONWITH4CARDS;
        } else if (myArrayList.get(position) instanceof RVTable) {
            return TYPE_TABLE;
        } else if (myArrayList.get(position) instanceof RVLineSeparator) {
            return TYPE_SEPARATOR;
        }
        throw new IllegalArgumentException("Item at position " + position + " is not an instance of either Phonecall or SMSmessage");
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType=holder.getItemViewType();
        switch (viewType){
            case TYPE_HEADER:
                MainHeader mainHeader = (MainHeader) myArrayList.get(position);
                ((MHeaderViewHolder)holder).showMHeaderDetails(mainHeader);
                break;
            case TYPE_EXPANDABLE:
                Phonecall call = (Phonecall) myArrayList.get(position);
                ((CallViewHolder)holder).showCallDetails(call);
                break;
            case TYPE_NONEXPANDABLE:
                SMSmessage sms = (SMSmessage) myArrayList.get(position);
                ((SMSViewHolder)holder).showSmsDetails(sms);
                break;
            case TYPE_SECTIONWITH4CARDS:
                RVSectionWith4CVs section4 = (RVSectionWith4CVs) myArrayList.get(position);
                ((Section4ViewHolder)holder).showSection4Details(section4);
                break;
            case TYPE_TABLE:
                RVTable mytbl = (RVTable) myArrayList.get(position);
                ((TblViewHolder)holder).showTblDetails(mytbl);
                break;
            case TYPE_SEPARATOR:
                ((SeparatorViewHolder)holder).showSeparatorDetails();
                break;
            default:
                throw new IllegalArgumentException("unexpected viewType: " + viewType);
        }
    }

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

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int layout;
        RecyclerView.ViewHolder viewHolder;
        switch (viewType){
            case TYPE_HEADER:
                layout = R.layout.rv_header;
                View mainheaderView = LayoutInflater
                        .from(parent.getContext())
                        .inflate(layout, parent, false);
                viewHolder = new MHeaderViewHolder(mainheaderView);
                break;
            case TYPE_EXPANDABLE:
                layout = R.layout.cardview_dualline_withexpandability;
                View callsView = LayoutInflater
                        .from(parent.getContext())
                        .inflate(layout, parent, false);
                viewHolder = new CallViewHolder(callsView);
                break;
            case TYPE_NONEXPANDABLE:
                layout = R.layout.cardview_dualline_sansexpandability;
                View smsView = LayoutInflater
                        .from(parent.getContext())
                        .inflate(layout, parent, false);
                viewHolder = new SMSViewHolder(smsView);
                break;
            case TYPE_SECTIONWITH4CARDS:
                layout = R.layout.rv_item_sectionwith4cvs;
                View section4View = LayoutInflater
                        .from(parent.getContext())
                        .inflate(layout, parent, false);
                viewHolder = new Section4ViewHolder(section4View);
                break;
            case TYPE_TABLE:
                layout = R.layout.cardview_tableview_withexpandability;
                View tblView = LayoutInflater
                        .from(parent.getContext())
                        .inflate(layout, parent, false);
                viewHolder = new TblViewHolder(tblView);
                break;
            case TYPE_SEPARATOR:
                layout = R.layout.lineseparatorforrecyclerview;
                View separatorView = LayoutInflater
                        .from(parent.getContext())
                        .inflate(layout, parent, false);
                viewHolder = new SeparatorViewHolder(separatorView);
                break;
            default:
                throw new IllegalArgumentException("unexpected viewType: " + viewType);
        }
        return viewHolder;
    }

    public class MHeaderViewHolder extends RecyclerView.ViewHolder {
        private Button btnExpandAll, btnCollapseAll;

        MHeaderViewHolder(View itemView) {
            super(itemView);
            btnExpandAll = itemView.findViewById(R.id.btn_expandall);
            btnCollapseAll = itemView.findViewById(R.id.btn_collapseall);

            btnExpandAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    expandedAll = true;
                    collapsedAll = false;
                    notifyDataSetChanged();

                    Toast.makeText(context,"expand clicked",Toast.LENGTH_SHORT).show();
                }
            });

            btnCollapseAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    expandedAll = false;
                    collapsedAll = true;
                    notifyDataSetChanged();

                    Toast.makeText(context,"collapse clicked",Toast.LENGTH_SHORT).show();
                }
            });
        }

        void showMHeaderDetails(MainHeader call){
            // code ommitted for easier reading
        }
    }

    public class CallViewHolder extends RecyclerView.ViewHolder {
        final Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);

        private TextView arrowexpandcollapseTextView, callerNameTextView, callTimeTextView;
        private LinearLayout llFacilityInformation;

        CallViewHolder(View itemView) {
            super(itemView);
            arrowexpandcollapseTextView = itemView.findViewById(R.id.tv_cvwithexpandability_arrowexpandcollapse);
            callerNameTextView = itemView.findViewById(R.id.tv_cvwithexpandability_title);
            callTimeTextView = itemView.findViewById(R.id.tv_cvwithexpandability_subtitle);
            llFacilityInformation = itemView.findViewById(R.id.ll_cvwithexpandability_subtitle);

            arrowexpandcollapseTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llFacilityInformation.getVisibility() == View.GONE) {
                        expandLL(llFacilityInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseLL(llFacilityInformation, arrowexpandcollapseTextView);
                    }
                }
            });

            callerNameTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llFacilityInformation.getVisibility() == View.GONE) {
                        expandLL(llFacilityInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseLL(llFacilityInformation, arrowexpandcollapseTextView);
                    }
                }
            });

            llFacilityInformation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llFacilityInformation.getVisibility() == View.GONE) {
                        expandLL(llFacilityInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseLL(llFacilityInformation, arrowexpandcollapseTextView);
                    }
                }
            });


            // boolean decision making
            if (expandedAll) {
                expandLL(llFacilityInformation, arrowexpandcollapseTextView);
            } else if (collapsedAll) {
                collapseLL(llFacilityInformation, arrowexpandcollapseTextView);
            }
        }

        void showCallDetails(Phonecall call){
            arrowexpandcollapseTextView.setText(R.string.fa_icon_chevron_down);
            arrowexpandcollapseTextView.setTypeface(iconFont);
            llFacilityInformation.setVisibility(View.GONE);

            String callerName = call.getCallerName();
            String callTime = call.getCallTime();

            callerNameTextView.setText(callerName);
            callTimeTextView.setText(callTime);
        }
    }

    public class SMSViewHolder extends RecyclerView.ViewHolder {
        private TextView senderNameTextView, smsContentTextView;

        SMSViewHolder(View itemView) {
            super(itemView);
            senderNameTextView = itemView.findViewById(R.id.tv_cvsansexpandability_title);
            smsContentTextView = itemView.findViewById(R.id.tv_cvsansexpandability_subtitle);
        }

        void showSmsDetails(SMSmessage sms){
            // code ommitted for easier reading
        }
    }

    public class Section4ViewHolder extends RecyclerView.ViewHolder {
        final Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);
        private LinearLayout llSectionInformation;
        private TextView arrowexpandcollapseTextView, sectionNameTextView, ticketofficetitleTextView, ticketofficedetailsTextView, ticketmachinestitleTextView, ticketmachinesdetailsTextView, gatestitleTextView, gatesdetailsTextView, atmspayphonestitleTextView, atmspayphonesdetailsTextView;

        Section4ViewHolder(View itemView) {
            super(itemView);
            // Initiate views
            arrowexpandcollapseTextView = itemView.findViewById(R.id.tv_section4headerforrv_expandcollapsearrow);
            sectionNameTextView = itemView.findViewById(R.id.tv_section4headerforrv_title);
            // ommitted code
            llSectionInformation = itemView.findViewById(R.id.ll_section4_cards);

            llSectionInformation.setVisibility(View.GONE);

            arrowexpandcollapseTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llSectionInformation.getVisibility() == View.GONE) {
                        expandLL(llSectionInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseLL(llSectionInformation, arrowexpandcollapseTextView);
                    }
                }
            });

            sectionNameTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llSectionInformation.getVisibility() == View.GONE) {
                        expandLL(llSectionInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseLL(llSectionInformation, arrowexpandcollapseTextView);
                    }
                }
            });


            // boolean decision making
            if (expandedAll) {
                expandLL(llSectionInformation, arrowexpandcollapseTextView);
            } else if (collapsedAll) {
                collapseLL(llSectionInformation, arrowexpandcollapseTextView);
            }
        }

        void showSection4Details(RVSectionWith4CVs section){
            // code ommitted for easier reading
        }
    }

    public class TblViewHolder extends RecyclerView.ViewHolder {
        final Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);

        private Button btnMale, btnFemale, btnWheelchairuser, btnBabychanging;
        private RelativeLayout rlFacilityInformation;
        private TextView arrowexpandcollapseTextView, sectionNameTextView;

        TblViewHolder(View itemView) {
            // code ommitted

            arrowexpandcollapseTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (rlFacilityInformation.getVisibility() == View.GONE) {
                        expandRL(rlFacilityInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseRL(rlFacilityInformation, arrowexpandcollapseTextView);
                    }
                }
            });

            sectionNameTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (rlFacilityInformation.getVisibility() == View.GONE) {
                        expandRL(rlFacilityInformation, arrowexpandcollapseTextView);
                    } else {
                        collapseRL(rlFacilityInformation, arrowexpandcollapseTextView);
                    }
                }
            });

            // boolean decision making
            if (expandedAll) {
                expandRL(rlFacilityInformation, arrowexpandcollapseTextView);
            } else if (collapsedAll) {
                collapseRL(rlFacilityInformation, arrowexpandcollapseTextView);
            }
        }
    }

    public class SeparatorViewHolder extends RecyclerView.ViewHolder {
        private View lSeparator;

        SeparatorViewHolder(View itemView) {
            super(itemView);
            lSeparator = itemView.findViewById(R.id.rv_lineseparator);
        }

        void showSeparatorDetails(){
            TypedValue tValueD = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.dividerColor, tValueD, true);

            lSeparator.setBackgroundResource(tValueD.resourceId);
        }
    }

    private void expandLL(final LinearLayout llFacilityInformation, final TextView arrowexpandcollapseTextView) {
        llFacilityInformation.setVisibility(View.VISIBLE);
        arrowexpandcollapseTextView.setText(R.string.fa_icon_chevron_up);
    }

    private void collapseLL(final LinearLayout llFacilityInformation, final TextView arrowexpandcollapseTextView) {
        llFacilityInformation.setVisibility(View.GONE);
        arrowexpandcollapseTextView.setText(R.string.fa_icon_chevron_down);
    }

    private void expandRL(final RelativeLayout rlFacilityInformation, final TextView arrowexpandcollapseTextView) {
        rlFacilityInformation.setVisibility(View.VISIBLE);
        arrowexpandcollapseTextView.setText(R.string.fa_icon_chevron_up);
    }

    private void collapseRL(final RelativeLayout rlFacilityInformation, final TextView arrowexpandcollapseTextView) {
        rlFacilityInformation.setVisibility(View.GONE);
        arrowexpandcollapseTextView.setText(R.string.fa_icon_chevron_down);
    }
}

在此处输入图片说明

这是因为您仅在创建ViewHolder时扩展或折叠视图,而在回收它们时不绑定它们。 要解决此问题,您需要在onBindViewHolder方法中正确绑定视图(不在其构造函数中):

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    int viewType=holder.getItemViewType();
    switch (viewType){
        // ...
        case TYPE_EXPANDABLE:
            Phonecall call = (Phonecall) myArrayList.get(position);
            ((CallViewHolder)holder).bind(call);
            break;
        // ...

然后调整您的ViewHolder

public void bind(Phonecall call) {
    showCallDetails(call);
    if (expandedAll) {
        expandRL(rlFacilityInformation, arrowexpandcollapseTextView);
    } else if (collapsedAll) {
        collapseRL(rlFacilityInformation, arrowexpandcollapseTextView);
    }
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM