滚动RecyclerView

时间:2015-12-13 19:16:27

标签: android checkbox android-recyclerview

在您将此标记为重复之前,我已阅读了大量有关此内容的帖子但尚未找到我的解决方案的答案。

我有一个RecyclerView填充了数据库中的一些数据。每个RecyclerView项目最初都有一个CheckBox android:visibility="gone"。然后我按下按钮,CheckBox的每个项目RecyclerView都会出现。当我向下滚动以查看是否所有CheckBox都出现时,顶部的CheckBox会被隐藏。

我的代码:

private void setupButtonCallbacks() {
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int position=0; position<recyclerView.getChildCount();position++){
                    recyclerView.getChildAt(position).findViewById(R.id.deleteCheckCartItem).setVisibility(View.VISIBLE);
                    recyclerView.getChildAt(position).findViewById(R.id.deleteCheckCartItem).setTag(position);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

适配器:

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder>{

    List<CartList> cartItems;
    Context context;
    CartList lista;

    public CartAdapter(List<CartList> cartItems, Context context){
        this.cartItems = cartItems;
        this.context = context;
    }

    @Override
    public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_row_item, parent, false);
        CartViewHolder pvh = new CartViewHolder(view);
        return pvh;
    }

    @Override
    public void onBindViewHolder(final CartViewHolder holder, final int position) {

        holder.cartProductName.setText(cartItems.get(position).getName());
        holder.cartProductprice.setText(cartItems.get(position).getPrice());
        holder.pref1.setText(cartItems.get(position).getPreferation1());
        holder.pref2.setText(cartItems.get(position).getPreferation2());
        holder.pref3.setText(cartItems.get(position).getPreferation3());
        holder.pref4.setText(cartItems.get(position).getPreferation4());
        holder.quantity.setText(cartItems.get(position).getQuantity());
        Ion.with(holder.cartProductImage).placeholder(R.mipmap.placeholder).error(R.mipmap.placeholder).load(cartItems.get(position).getImage());
        final CartList list = cartItems.get(position);
        holder.checkBox.setTag(list);
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.checkBox.isChecked()){
                    Toast.makeText(context, "Item Checked is " + cartItems.get(position).getName(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

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

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public static class CartViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        CheckBox checkBox;
        TextView cartProductName, cartProductprice, pref1, pref2, pref3, pref4, quantity;
        ImageView cartProductImage;
        LinearLayout linearLayout;

        CartViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView)itemView.findViewById(R.id.card_view);
            linearLayout = (LinearLayout)itemView.findViewById(R.id.rootLinear);
            checkBox = (CheckBox)itemView.findViewById(R.id.deleteCheckCartItem);
            cartProductName = (TextView)itemView.findViewById(R.id.product_name_cart);
            cartProductprice = (TextView)itemView.findViewById(R.id.product_price_cart);
            pref1 = (TextView)itemView.findViewById(R.id.pref1_text_cart);
            pref2 = (TextView)itemView.findViewById(R.id.pref2_text_cart);
            pref3 = (TextView)itemView.findViewById(R.id.pref3_text_cart);
            pref4 = (TextView)itemView.findViewById(R.id.pref4_text_cart);
            quantity = (TextView)itemView.findViewById(R.id.quantity_text_cart);
            cartProductImage = (ImageView)itemView.findViewById(R.id.product_image_cart);

        }
    }

}

布局:

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp">

        <LinearLayout
            android:id="@+id/rootLinear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:visibility="gone"
                    android:focusable="false"
                    android:layout_gravity="center_vertical"
                    android:id="@+id/deleteCheckCartItem" />

结果是:

前两项具有正常行为且

First

,最后两个没有CheckBoxes可见

Second

更新 正如我所建议的那样,我试图跟踪这样的位置:

private void setupButtonCallbacks() {
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int position = 0; position < recyclerView.getChildCount(); position++) {
                    recyclerView.getChildAt(position).findViewById(R.id.deleteCheckCartItem).setVisibility(View.VISIBLE);
                    cartItems.get(position).setCheckboxIsVisible(true);
                    adapter.notifyDataSetChanged();
                }
                actionMode = startSupportActionMode(actionModeCallback);
                actionMode.setTitle(getString(R.string.selected_modifiable_items));
            }
        });
    }

适配器:

@Override
public void onBindViewHolder(final CartViewHolder holder, final int position) {
    holder.cartProductName.setText(cartItems.get(position).getName());
    holder.cartProductprice.setText(cartItems.get(position).getPrice());
    holder.pref1.setText(cartItems.get(position).getPreferation1());
    holder.pref2.setText(cartItems.get(position).getPreferation2());
    holder.pref3.setText(cartItems.get(position).getPreferation3());
    holder.pref4.setText(cartItems.get(position).getPreferation4());
    holder.quantity.setText(cartItems.get(position).getQuantity());
    Ion.with(holder.cartProductImage).placeholder(R.mipmap.placeholder).error(R.mipmap.placeholder).load(cartItems.get(position).getImage());
    lista = cartItems.get(position);
    holder.checkBox.setVisibility(lista.isCheckboxIsVisible()?View.VISIBLE:View.GONE);
    holder.checkBox.setChecked(lista.isChecked());
    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (holder.checkBox.isChecked()) {
                //Toast.makeText(context, "Item Checked is " + cartItems.get(position).getName(), Toast.LENGTH_SHORT).show();
                boolean condition = cartItems.get(position).isChecked();
                condition = isChecked;
            }


        }
    });

}

它仅用于列表中的前3个项目,然后没有选择它们......

有什么想法吗?在此先感谢!!!

3 个答案:

答案 0 :(得分:4)

您的include_directories(${CMAKE_BINARY_DIR}) config_file( ${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h ) 需要跟踪可见性和复选框状态。

CartList cartItems

然后在onBindViewHolder()

class CartList
...
    boolean checkboxIsVisible=false;//todo add setters and getters
    boolean isChecked=false;//todo add setters and getters

检查

 final CartList list = cartItems.get(position);
        holder.checkBox.setVisible(list.checkboxIsVisible?View.VISIBLE:View.GONE);
        holder.checkBox.setChecked(list.isChecked)

... @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ... cartItems.get(position).isChecked=isChecked; } 知名度

modify

答案 1 :(得分:2)

正如名称所示,RecyclerView的观点被回收。因此,如果用户选中一个项目上的复选框,则可能会显示另一个未检查的项目的相同复选框视图,该项目仍处于检查状态。

你必须这样做 checkBox.setChecked(true/false) 在每一个 onBindViewHolder()

这意味着您必须跟踪选中复选框的项目(ArrayList选中的项目,HashMap包含项目和布尔值,等等。

答案 2 :(得分:1)

我在项目中遇到了同样的问题。我做的是 在OnBindViewHolder和适配器setOnClickListner

上设置Invisible字段
mAdapterForChooser.setClickListener(new AdapterForChooser.ClickListener() {

                @Override
                public void onClick(View v, int pos) {



                    RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.selectedBg);

                    if (!mGiantChooserModels.get(pos).isSelected()) {

                        rl.setVisibility(View.VISIBLE);

                    } else {
                        rl.setVisibility(View.INVISIBLE);

                }
            });

我的持有人

@Override
public void onBindViewHolder(final ChooserInterestHolder holder, final int position) {



    //Main Trick

    if(mGiantChooserModels.get(position).isSelected())
        holder.selector.setVisibility(View.VISIBLE);
    else
        holder.selector.setVisibility(View.INVISIBLE);

    holder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            clickListener.onClick(v, position);
        }
    });
}