Android:GridLayout 大小和 View.GONE 行为

问题描述 投票:0回答:5

我想做一个

GridLayout
,当其中一个子项的
Visibility
设置为
GONE
时,它会被下一个子项替换。

使用

GridLayout
,将任何子项的可见性设置为
View.Gone
只会隐藏视图。 我怎样才能让它表现得像上图一样?

android android-gridlayout
5个回答
6
投票

解决与以下相同的问题:

package ua.vsgroup.widgets;

import android.content.Context;
import android.support.v7.widget.GridLayout;
import android.util.AttributeSet;
import android.view.View;

public class vsGridLayout extends GridLayout {

    View[] mChild = null;

    public vsGridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public vsGridLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public vsGridLayout(Context context) {
        this(context, null);
    }

    private void arrangeElements() {

        mChild = new View[getChildCount()];
        for (int i = 0; i < getChildCount(); i++) {
            mChild[i] = getChildAt(i);
        }

        removeAllViews();
        for (int i = 0; i < mChild.length; i++) {
            if (mChild[i].getVisibility() != GONE)
                addView(mChild[i]);
        }
        for (int i = 0; i < mChild.length; i++) {
            if (mChild[i].getVisibility() == GONE)
                addView(mChild[i]);
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        arrangeElements();
        super.onLayout(changed, left, top, right, bottom);

    }


}

6
投票

如果您不需要再次使视图可见,您可以通过从 GridLayout 中删除视图来解决。

private void hideView(View view) {
    GridLayout gridLayout = (GridLayout) view.getParent();
    for (int i = 0; i <  gridLayout.getChildCount(); i++) {
        if (view == gridLayout.getChildAt(i)) {
            gridLayout.removeViewAt(i);
            break;
        }
    }
}

5
投票

解决方案是使用 RecyclerView 和 GridLayoutManager。关键是使用notifyItemRemoved 通知适配器有关已删除项目的更改。 RecyclerViews 中有很大的自定义空间,例如消失项目的精美动画、屏幕上剩余项目的重新排列、项目装饰等。您可以根据您的特定需要应用所有这些自定义和删除项目的附加逻辑问题。

活动

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<String> dataSet = getSampleDataSet();
        recyclerView = (RecyclerView) findViewById(R.id.grid);
        recyclerView.setAdapter(new MyAdapter(dataSet));
        recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));
    }

    private List<String> getSampleDataSet() {
        List strings = new ArrayList();
        strings.add("one");
        strings.add("two");
        strings.add("three");
        strings.add("four");
        strings.add("five");
        strings.add("six");

        return strings;
    }
}

适配器

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    List<String> dataSet;



    public MyAdapter(List<String> dataSet) {
        this.dataSet = dataSet;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView tileView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(tileView);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.view.setText(dataSet.get(position));

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataSet.remove(position);
                notifyItemRemoved(position); // this notifies the adapter about item being removed
            }
        });

    }

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


class MyViewHolder extends RecyclerView.ViewHolder {
    TextView view;

    public MyViewHolder(TextView itemView) {
        super(itemView);
        view = itemView;
    }
}

活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

网格项目

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridItem"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary"
    android:textColor="@android:color/white"
    android:gravity="center"
    android:text="Tile"/>

结果 前:

点击4后。在实际设备上,您将能够看到此操作的漂亮框架动画。


3
投票

基于this答案。谢谢哥们。

创建您的自定义 GridLayout 小部件。

package com.isolpro.pricelist.custom;    

public class RearrangingGridLayout extends GridLayout {
  private final List<View> views = new ArrayList<>();

  public RearrangingGridLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  public RearrangingGridLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public RearrangingGridLayout(Context context) {
    this(context, null);
  }

  private void arrangeElements() {
    removeAllViews();

    for (int i = 0; i < views.size(); i++) {
      if (views.get(i).getVisibility() != GONE)
        addView(views.get(i));
    }
  }

  public void saveViews() {
    for (int i = 0; i < getChildCount(); i++) {
      views.add(getChildAt(i));
    }
  }

  public void hideViewAtIndex(int index) {
    if (index >= 0 && index < views.size()) {
      views.get(index).setVisibility(GONE);
      arrangeElements();
    }
  }

  public void showViewAtIndex(int index) {
    if (index >= 0 && index < views.size()) {
      views.get(index).setVisibility(VISIBLE);
      arrangeElements();
    }
  }
}

使用方法如下:

绑定(渲染)后保存GridLayout的所有子级,使用以下代码

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    RearrangingGridLayout rglPrices = findViewById(R.id.rglPrices);

    rglPrices.saveViews();
  }

现在,隐藏您的视图

rglPrices.hideViewAtIndex(indexOfView);

同样,展示你的观点

rglPrices.showViewAtIndex(indexOfView);

你就完成了!

对于我的项目,保留孩子们的位置至关重要,所以我选择了索引。通过更新显示和隐藏函数,您可以轻松修改代码以使用其他内容,例如视图 id。


0
投票

这太棒了答案

对于那些想要将视图而不是索引传递到函数中的人。

public void hideView(View child) {
    for (int i = 0; i < views.size(); i++) {
        if (views.get(i).getId() == child.getId()) {
            views.get(i).setVisibility(GONE);
            arrangeElements();
            break;
        }
    }
}

public void showView(View child) {
    for (int i = 0; i < views.size(); i++) {
        if (views.get(i).getId() == child.getId()) {
            views.get(i).setVisibility(VISIBLE);
            arrangeElements();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.