为什么我的LinearLayout占用空间即使我在GridView中设置了Visibility?

时间:2017-10-17 06:16:53

标签: android android-layout gridview

我在网格视图中有一个LinearLayout,它是默认设置的可见性GONE, 在单击按钮上单击我显示LinearLayout,然后在另一个按钮单击我也设置我的LinearLayout可见性再次消失。所以问题是我的布局已经成功但是它仍然占用空间,我从最后一天开始就遇到了这个问题,并且还搜索了所有的解决方案但是没有为我工作,

我的代码是: -

这是我在适配器中填充的XML文件:

grid_view_dash_item.xml: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/itemName"
            style="@style/textColorBlack_14sp"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="Check Engine" />

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/imgWacsItem"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginEnd="10dp"
            android:src="@drawable/battery" />

        <Switch
            android:id="@+id/switchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:theme="@style/SwitchTheme"
            android:visibility="gone" />


        <IncrementDecrementWidgetActivity
            android:id="@+id/number_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="14dp"
            android:layout_marginTop="2dp"
            app:textSize="18sp" />

        <LinearLayout
            android:id="@+id/addCommentLayout"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/imgAddItemComment"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginStart="15dp"
                android:src="@drawable/edit_icon" />

        </LinearLayout>


        <Switch
            android:id="@+id/switchViewNoShow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="-30dp"
            android:theme="@style/SwitchTheme"
            android:visibility="invisible" />

    </LinearLayout>


        <LinearLayout
        android:id="@+id/layoutEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="5dp">

        <EditText
            android:id="@+id/editTextPopup"
            android:layout_width="250dp"
            android:layout_height="80dp"
            android:background="@android:drawable/editbox_background_normal"
            android:gravity="top"
            android:hint="Enter Comment"
            android:padding="10dp"
            android:textColor="#000000" />

        <Button
            android:id="@+id/closePopupBtn"
            style="@style/buttonRegular"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/default_button_height"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="OK" />
        </LinearLayout>



    <LinearLayout
        android:id="@+id/pupUpWindowLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout"
        android:layout_gravity="start"
        android:orientation="horizontal"
       >

        <TextView
            android:id="@+id/textEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:text="text" />

    </LinearLayout>

</LinearLayout>

这是适配器的代码: -

 GridViewWacsItemAdapter extends RecyclerView.Adapter<GridViewWacsItemAdapter.ViewHolder> {

    Context mContext;
    List<WacsDashboardItemResponse.SubGroup.ItemArray> itemArray;

    public GridViewWacsItemAdapter(Context mContext, List<WacsDashboardItemResponse.SubGroup.ItemArray> itemArray) {
        this.mContext = mContext;
        this.itemArray = itemArray;
    }

    @Override
    public GridViewWacsItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_dash_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final GridViewWacsItemAdapter.ViewHolder holder, int position) {

        if (itemArray.get(position).responseType.responseTypeList.equalsIgnoreCase("CHOICE_LIST1")) {
            holder.switchView.setVisibility(View.VISIBLE);
            holder.number_button.setVisibility(View.GONE);
            holder.addCommentLayout.setVisibility(View.VISIBLE);

            holder.switchViewNoShow.setVisibility(View.INVISIBLE);

        } else if (itemArray.get(position).responseType.responseTypeList.equalsIgnoreCase("CHOICE_LIST2")) {

            holder.switchView.setVisibility(View.GONE);
            holder.number_button.setVisibility(View.VISIBLE);
            holder.addCommentLayout.setVisibility(View.GONE);

            holder.switchViewNoShow.setVisibility(View.GONE);

            holder.number_button.setRange(0, 4);
        }

        holder.imgWacsItem.setDefaultImageResId(R.drawable.progress_bar_default);  // image for loading...
        holder.imgWacsItem.setImageUrl(itemArray.get(position).icon, AppController.getInstance().getImageLoader());
        holder.itemName.setText(itemArray.get(position).itmeDesc);

        holder.number_button.setOnClickListener(new IncrementDecrementWidgetActivity.OnClickListener() {
            @Override
            public void onClick(View view) {

                String number = holder.number_button.getNumber();
                // Toast.makeText(mContext, "number " + number, Toast.LENGTH_SHORT).show();
            }
        });

holder.closePopupBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        holder.textEdit.setVisibility(View.VISIBLE);
        holder.textEdit.setText(holder.editTextPopup.getText().toString());
        holder.layoutEdit.setVisibility(View.GONE);

    }
});
        holder.addCommentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(mContext, "Clicked on View", Toast.LENGTH_SHORT).show();
              holder.layoutEdit.setVisibility(View.VISIBLE);

            }

        });

    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView itemName, textEdit;
        Switch switchView, switchViewNoShow;
        NetworkImageView imgWacsItem;
        IncrementDecrementWidgetActivity number_button;
        LinearLayout pupUpWindowLayout, addCommentLayout,layoutEdit;
        Button closePopupBtn;
         EditText editTextPopup;


        public ViewHolder(View itemView) {

            super(itemView);

            itemName = (TextView) itemView.findViewById(R.id.itemName);
            imgWacsItem = (NetworkImageView) itemView.findViewById(R.id.imgWacsItem);
            switchView = (Switch) itemView.findViewById(R.id.switchView);
            switchViewNoShow = (Switch) itemView.findViewById(R.id.switchViewNoShow);
            closePopupBtn=(Button)itemView.findViewById(R.id.closePopupBtn);
            editTextPopup=(EditText)itemView.findViewById(R.id.editTextPopup);
            textEdit = (TextView) itemView.findViewById(R.id.textEdit);
            pupUpWindowLayout = (LinearLayout) itemView.findViewById(R.id.pupUpWindowLayout);
            addCommentLayout = (LinearLayout) itemView.findViewById(R.id.addCommentLayout);
            layoutEdit = (LinearLayout) itemView.findViewById(R.id.layoutEdit);
            layoutEdit.setVisibility(View.GONE);


            number_button = (IncrementDecrementWidgetActivity) itemView.findViewById(R.id.number_button);

        }
    }

}

我赞赏的任何类型的帮助: - 谢谢

1 个答案:

答案 0 :(得分:0)

为布局添加背景颜色,看看它是否真的消失了。可能有其他观点占用空间,而不是您正在寻找的LinearLayout。