其他分享
首页 > 其他分享> > Android:在gridlayout或gridview中创建具有动态大小的网格

Android:在gridlayout或gridview中创建具有动态大小的网格

作者:互联网

我打算制作网格基础应用程序,其中网格数量将根据用户的点击而改变.网格模式: – 2 * 2,4 * 4 … [比如,用户将点击正确的网格,然后网格数量将增加].我能够制作网格,但它不适合布局.那么,如何更改用户点击的网格数量以及如何动态设置网格的大小.

在这里,我附上了我想要实现的图像.

当用户点击不同的颜色网格时,网格数量将像第二个图像一样增加.

图片:1

enter image description here

图片:2

用户点击时网格数量增加.

enter image description here

解决方法:

最后我得到了我的问题的答案:

这里需要将SquareImageView用于imageview并以编程方式设置gridview列,现在看下面的代码:

SquareImageView.java

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to
                                                                    // width
}
}

为视图充气器创建Custome适配器,

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.SquareImageView
    android:id="@+id/image_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</com.example.SquareImageView>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

int counter = 1;

CustomGrid adapter;
static int colum = 2;

public static GridView gridview;

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

    adapter = new CustomGrid(MainActivity.this);
    gridview = (GridView) findViewById(R.id.gridview);

    gridview.setNumColumns(2);

    gridview.setAdapter(adapter);

}

public class CustomGrid extends BaseAdapter {
    private Context mContext;
    CustomGrid adgg;
    int mNumColumns = 2;
    private Random rand = new Random();
    GridView gg;
    int color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    float alfa = 0.1f;
    int rand_ind = rand.nextInt((2 * 2) - 0) + 0;

    public CustomGrid(Context c) {
        mContext = c;
        // this.Imageid = Imageid;
        // this.web = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mNumColumns * mNumColumns;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View gridView;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View grid = new View(mContext);
        grid = inflater.inflate(R.layout.layout, null);
        final View imageView = (ImageView) grid.findViewById(R.id.image_grid);

        if (convertView == null) {
            if (position == rand_ind) {
                imageView.setBackgroundColor(getColorWithAlpha(color, alfa));
            } else {
                imageView.setBackgroundColor(color);
            }
        } else {
            grid = (View) convertView;
        }

        grid.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                if (position == rand_ind) {

                    // Correct postion click time if condition occur..........


                    if (counter == 2) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 6) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.08f;
                    } else if (counter == 8) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 12) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 14) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 16) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 18) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.05f;
                    } else if (counter >= 18) {
                        // mNumColumns = mNumColumns + 1;
                        alfa = 0.8f;

                    }

                    gridview.setNumColumns(mNumColumns);
                    gridview.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                    rand_ind = rand.nextInt((mNumColumns * mNumColumns) - 0) + 0;

                } else {

                    // Here code for nagative click............ whem click
                    // nagative at time geanarate this code.........
                }
            }
        });

        return grid;
    }

    public int getColorWithAlpha(int color, float ratio) {
        int newColor = 0;
        int alpha = Math.round(Color.alpha(color) * ratio);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        newColor = Color.argb(alpha, r, g, b);
        return newColor;
    }
}

@Override
public void onClick(View v) {
}
}

我希望这个答案对任何人都有帮助.

标签:android,grid,gridview,android-gridlayout
来源: https://codeday.me/bug/20190528/1168749.html