回收站查看特定的单元格不能单击

时间:2019-07-23 05:50:32

标签: android android-recyclerview

enter image description here

如上图所示,我无法单击包含值15值的单元格。我发现它每13个单元重复一次。在下面,我还将显示适配器类。有人可以帮我找到这个问题。预先感谢。

public class OptionAdapter extends RecyclerView.Adapter<OptionAdapter.OptionViewHolder> implements ProductDetailAdapter,
    View.OnClickListener {

private Context mContext;
private OptionInfo mOptionInfo;
private OptionInfo.SelectionType mSelectionType;
private List<Option> mOptionList;

private int mPreviousSelectedPosition;
private View mPreviousSelectedView;
private int mSelectedCount;
private int mPriceJudge;

public OptionAdapter(Context context, Store store, OptionInfo optionInfo) {
    mContext = context;
    mOptionInfo = optionInfo;
    mSelectionType = optionInfo.getSelectionType();
    mOptionList = optionInfo.getData();
    for (Option option : mOptionList) {
        if (option.isSelected()) {
            mSelectedCount++;
        }
    }

    mPriceJudge = PriceUtil.priceJudge(store);
}

@Override
public OptionViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(mContext)
            .inflate(R.layout.option_list_item_type_1, viewGroup, false);
    OptionViewHolder viewHolder = new OptionViewHolder(mContext, view, mPriceJudge, this);
    return viewHolder;
}

@Override
public void onBindViewHolder(OptionViewHolder viewHolder, int position) {
    bindOption(viewHolder, mOptionList.get(position), position);
}

/**
 * Bind options data to view
 * @param viewHolder option view holder
 * @param option option object
 * @param position for tracking the previous position of selected option
 */
public void bindOption(OptionViewHolder viewHolder, Option option, int position) {
    viewHolder.bindData(option.getOptionName(), option.getOptionPrice(), position, option.isSelected(),
            option.getZeroDispType(), mOptionInfo.getSizesHalves());
    if (option.isSelected()) {
        mPreviousSelectedPosition = position;
        mPreviousSelectedView = viewHolder.mLayout;
    }
    if (SoldOutUtil.isMeetAllSoldOutConditions(option.isTrnOrderStopOptionFlg(), option.getStartDatetimeChar(), option.getEndDatetimeChar())) {
        viewHolder.mLayout.setClickable(false);

        viewHolder.mPriceText.setText(R.string.product_detail_sold_out);
        viewHolder.mPriceText.setVisibility(View.VISIBLE);

        viewHolder.mImageView.setVisibility(View.INVISIBLE);
    }
}

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

@Override
public void onClick(View view) {
    int position = (int) view.getTag();
    Option option = mOptionList.get(position);
    ImageView imageView = (ImageView) view.findViewById(R.id.checkbox);

    /* Check current status */
    if (option.isSelected()) {
        // At least one choice in single choice case
        if (!(mSelectionType == OptionInfo.SelectionType.SINGLE && mPreviousSelectedPosition == position)) {
            imageView.setImageResource(R.drawable.ic_check_off);
            option.setSelected(false);
            mSelectedCount--;
        }
    } else {

        if (mSelectedCount <= mOptionInfo.getMax()) {
            // If Max and min equals 1 meas SINGLE choice even if the type is not single
            boolean isSingleChoice = (mOptionInfo.getMax() == 1 && mOptionInfo.getMin() == 1) || (mSelectionType == OptionInfo.SelectionType.SINGLE);
            if (isSingleChoice) {
                mOptionList.get(mPreviousSelectedPosition).setSelected(false);
                if (mPreviousSelectedView != null) {
                    ((ImageView) mPreviousSelectedView.findViewById(R.id.checkbox)).setImageResource(R.drawable.ic_check_off);
                }
                mPreviousSelectedPosition = position;
                mPreviousSelectedView = view;
                if(mSelectedCount > 0) {
                    mSelectedCount--;
                }
            }

            if (mSelectedCount < mOptionInfo.getMax()) {

                imageView.setImageResource(R.drawable.ic_check_on);
                option.setSelected(true);
                //Un-check other selections if its single selection type.
                if(isSingleChoice) {
                    unCheckOtherOptions(option);
                }
                mSelectedCount++;
            }
        }
    }
}

/**
 * Un-checks other selections except the given option
 *
 * @param option
 */
private void unCheckOtherOptions(Option option) {
    for (int i = 0; i < mOptionList.size(); i++) {
        // Un select all other options other than the given option
        if (!TextUtils.equals(mOptionList.get(i).getOptionId(), option.getOptionId())) {
            mOptionList.get(i).setSelected(false);
        }
    }
}

@Override
public OptionInfo getOptionInfo() {
    return mOptionInfo;
}

public int getSelectedCount() {
    return mSelectedCount;
}

/**
 * Option view holder for option groups and size option
 */
public static class OptionViewHolder extends RecyclerView.ViewHolder {
    private final ImageView mImageView;
    public final LinearLayout mLayout;
    public TextView mTitleText;
    public TextView mPriceText;
    private Context mContext;
    private int mPriceJudge;

    public OptionViewHolder(Context context, View view, int priceJudge, View.OnClickListener onClickListener) {
        super(view);
        mContext = context;
        mPriceJudge = priceJudge;
        mLayout = (LinearLayout) view.findViewById(R.id.option_layout);
        mTitleText = (TextView) view.findViewById(R.id.option_title);
        mPriceText = (TextView) view.findViewById(R.id.option_price);
        mImageView = (ImageView) view.findViewById(R.id.checkbox);
        mLayout.setOnClickListener(onClickListener);
    }

    /**
     * Bind data to the view holder
     * @param name option name
     * @param price price text
     * @param position option position
     * @param selected the option is selected or not
     * @param zeroDispType
     * @param sizesHalves
     */
    public void bindData(String name, int price, int position, boolean selected, int zeroDispType, boolean sizesHalves) {
        mTitleText.setText(name);

        int visible = View.VISIBLE;
        if (!sizesHalves) {

            mPriceText.setText(price != 0 ?
                    PriceUtil.formatPriceString(mContext, price) :
                    PriceUtil.updateZeroDisplay(mContext, zeroDispType));

            visible = price <= 0 && mPriceJudge > 0 ? View.INVISIBLE : visible;
        }

        mPriceText.setVisibility(visible);

        mLayout.setTag(position);
        updateImage(selected);
    }

    private void updateImage(boolean isSelected) {

        mImageView.setVisibility(View.VISIBLE);

        mImageView.setImageResource(isSelected ? R.drawable.ic_check_on : R.drawable.ic_check_off);
    }
}
}

此代码添加了单元格单击方法和其他与适配器相关的基本功能。我有一个问题,就是具体如何无法单击这些单元格。从回收者的角度来看,这是非常奇怪的行为。

3 个答案:

答案 0 :(得分:0)

您正在您的 bindOption 方法中设置viewHolder.mLayout.setClickable(false);

在其他情况下,使您的viewHolder.mLayout可点击

答案 1 :(得分:0)

在构造函数之后的OptionViewHolder类中执行类似的操作。

mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=getAdapterPosition();
                itemListener.onClicked(position);
            }
        });  

itemListener是您必须调用mainActivity的接口,在该接口中初始化了适配器。

interface ItemListener{
    void onClicked(int pos);
}
ItemListener itemListener;

public void setItemListener(ItemListener itemListener) {
    this.itemListener = itemListener;
}  

单击索引视图即可从您的活动中执行下一步操作。

答案 2 :(得分:0)

public class viewHolder extends RecyclerView.ViewHolder {
        ImageView pimg, favorite;
        TextView pname, pprice, prating;

        public viewHolder(View itemView) {
            super(itemView);
            pimg = (ImageView) itemView.findViewById(R.id.pimg);
            favorite = (ImageView) itemView.findViewById(R.id.favorite);
            pname = (TextView) itemView.findViewById(R.id.pname);
            pprice = (TextView) itemView.findViewById(R.id.pprice);
            prating = (TextView) itemView.findViewById(R.id.prating);

                itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(getAdapterPosition(), v);
                    }
                });
        }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

如果您仍然无法获得点击项位置see here