显示和隐藏列表视图

时间:2017-04-19 02:04:19

标签: java android listview

我想做这样的列表视图。我想要的是当用户触摸第一项时,出现第二项。当用户触摸第3行项目时,前一项目会再次自动隐藏。我怎样才能实现这一点,而不是使用可扩展的listview?指导我实现这一目标。提前谢谢。

1 个答案:

答案 0 :(得分:0)

对不起,我有点忙,所以我现在不能写代码。但是下面是你可以用来获得你想要的东西的方式。我会在有空的时候添加代码。

  1. 有一个参数名称“ android:animateayoutchanges ”,将其应用于您的视图的父级,该视图的父级将可见且不可见。
  2. 保存您点击并展开的项目的位置。
  3. 单击该项目时,将视图的可见性设置为可见。
  4. 当您再次单击该项目位置时,请将该项目的可见性设置为已消失。
  5. 当您单击其他项目时,请将存储项目位置的可见性设置为已消失,将当前所选项目的可见性设置为可见并存储其位置。 就是这样。
  6. 使用

    android:animateayoutchanges ,以便当您将可见性设置为可见或消失并且视图向上或向下移动时,此移动将具有平滑的动画。

    我稍后会用代码更新我的答案但是你会从上面的步骤中得到一个想法。

    <强>更新

        public class ShippingAdapter extends RecyclerView.Adapter<ShippingAdapter.ViewHolder> {
    
        private ArrayList<ShippingOptions> list;
        private int expandedPosition = -1;
        private int listSize = 0;
        private int checkedPositon = -1;
        private String currency;
    
        public ShippingAdapter(ArrayList<ShippingOptions> list, String currency, AdapterInterface listener) {
            this.list = list;
            listSize = list.size();
            this.currency = currency;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_shipping_options, parent, false);
            // Layout in which you will add **android:animatelayoutchanges**
            return new ViewHolder(v);
        }
    
    
        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            ShippingOptions item = list.get(position);
            holder.name.setText(item.getName());
            holder.fee.setText(utils.getFormattedCurrency(currency, Double.parseDouble(item.getShippingFee())));
            holder.description.setText(item.getDescription());
            holder.deliveryTime.setText(item.getDeliveryTime());
            if (listSize == 1) {
                list.get(position).setExpanded(true);
            }
    
    
            **if (list.get(position).isExpanded()) {
                holder.expandableView.setVisibility(View.VISIBLE);
                holder.checkBox.setVisibility(View.VISIBLE);
                if (checkedPositon == position) {
                    holder.checkBox.setChecked(true);
                } else {
                    holder.checkBox.setChecked(false);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.parent.setElevation(12);
                    holder.parent.setTranslationZ(6);
                    holder.parent.setClipToPadding(false);
                    holder.parent.setClipToOutline(false);
                }
            } else {
                holder.expandableView.setVisibility(View.GONE);
                holder.checkBox.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.parent.setElevation(0);
                    holder.parent.setTranslationZ(0);
                }
            }**
    
            **holder.parent.setOnClickListener(new View.OnClickListener() {
                @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
                @Override
                public void onClick(View v) {
                    if (listSize > 1) {
                        if (expandedPosition == position) {
                            if (list.get(position).isExpanded()) {
                                list.get(position).setExpanded(false);
                            } else {
                                list.get(position).setExpanded(true);
                            }
                            notifyItemChanged(position);
                        } else {
                            if (expandedPosition >= 0) {
                                list.get(expandedPosition).setExpanded(false);
                                notifyItemChanged(expandedPosition);
                            }
                            expandedPosition = position;
                            list.get(expandedPosition).setExpanded(true);
                            notifyItemChanged(expandedPosition);
                        }
                    }
                }
            });**
    
    
        }
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public TextView name, fee, description, deliveryTime;
            public LinearLayout parent, expandableView;
            public CheckBox checkBox;
    
            public ViewHolder(View v) {
                super(v);
    
                name = (TextView) v.findViewById(R.id.tv_shipping_name);
                fee = (TextView) v.findViewById(R.id.tv_shipping_fee);
                description = (TextView) v.findViewById(R.id.tv_description);
                deliveryTime = (TextView) v.findViewById(R.id.tv_delivery_time);
                parent = (LinearLayout) v.findViewById(R.id.parent);
                expandableView = (LinearLayout) v.findViewById(R.id.expandable_view);
                checkBox = (CheckBox) v.findViewById(R.id.checkbox);
    
            }
    
    
        }
    }
    

    <强> list_shipping_options.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="10dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?listPreferredItemHeightSmall"
            android:gravity="center_vertical">
    
            <TextView
                android:id="@+id/tv_shipping_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@style/text_medium"
                android:textColor="@color/colorPrimary" />
    
            <CheckBox
                android:id="@+id/checkbox"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_alignParentEnd="true"
                android:background="?android:attr/listChoiceIndicatorMultiple"
                android:button="@null"
                android:checked="false"
                android:visibility="visible" />
        </RelativeLayout>
    
        <LinearLayout
            android:id="@+id/expandable_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingTop="10dp">
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/label_shipping_fee" />
    
                <TextView
                    android:id="@+id/tv_shipping_fee"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingTop="10dp">
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lable_description" />
    
                <TextView
                    android:id="@+id/tv_description"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingTop="10dp">
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/delivery_time" />
    
                <TextView
                    android:id="@+id/tv_delivery_time"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2" />
            </LinearLayout>
        </LinearLayout>
    
    </LinearLayout>
    

    <强> ShippingOptions.class

        public class ShippingOptions implements Serializable {
    
        private String id;
        private String name;
        private String shippingFee;
        private String description;
        private String deliveryTime;
        private boolean isExpanded;
        private boolean isChecked;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getShippingFee() {
            return shippingFee;
        }
    
        public void setShippingFee(String shippingFee) {
            this.shippingFee = shippingFee;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public String getDeliveryTime() {
            return deliveryTime;
        }
    
        public void setDeliveryTime(String deliveryTime) {
            this.deliveryTime = deliveryTime;
        }
    
        public boolean isExpanded() {
            return isExpanded;
        }
    
        public void setExpanded(boolean expanded) {
            isExpanded = expanded;
        }
    
        public boolean isChecked() {
            return isChecked;
        }
    
        public void setChecked(boolean checked) {
            isChecked = checked;
        }
    }