简体   繁体   English

如何在Android中将屏幕划分为小方块?

[英]How to divide the screen to small squares in android?

I want to make appliction that all the screen divide to small squares, and when i touch on one of them it change the color of the squares i touched. 我想使所有屏幕都划分为小方块,当我触摸其中一个时,它会改变我触摸的方块的颜色。 I didnt find any code that do this. 我没有找到执行此操作的任何代码。 How can i do this in android ? 我该如何在android中做到这一点?

Use GridView to achieve this kind of things 使用GridView实现这种事情

GridView 网格视图

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. GridView是一个ViewGroup,它在二维可滚动网格中显示项目。 The grid items are automatically inserted to the layout using a ListAdapter. 使用ListAdapter将网格项自动插入到布局中。

Set the Click Listener to particular item to change the respective box color. 将“单击侦听器”设置为特定项目,以更改相应的框颜色。

For Reference:- https://developer.android.com/guide/topics/ui/layout/gridview 供参考: -https : //developer.android.com/guide/topics/ui/layout/gridview

You should use a RecyclerView which recycles it's children views for better performance and you can set it's layoutmanager to GridLayoutManger. 您应该使用RecyclerView来回收其子视图以提高性能,并且可以将其layoutmanager设置为GridLayoutManger。 For the color change, you can store the colors or state into something like a SparseArray when you initialize your list of data and update the colors on an item click, then retrieve the right color from your viewholder getAdapterPosition() in the onBindViewHolder() method from your Recyclerview.Adapter 对于颜色更改,您可以在初始化数据列表并单击项目时更新颜色时将颜色或状态存储到诸如SparseArray之类的颜色中,然后在onBindViewHolder()方法中从查看器的getAdapterPosition()中检索正确的颜色。从您的Recyclerview.Adapter

Here are the references : 以下是参考资料:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager https://developer.android.com/reference/android/support/v7/widget/RecyclerView https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter https:// developer。 android.com/reference/android/support/v7/widget/GridLayoutManager

XML XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="@drawable/gradient_touchscreen"
    tools:context=".tochscreen_page_2">


    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="none"
        android:gravity="center"
        />




</android.support.constraint.ConstraintLayout>

Java Java的

public class tochscreen_page_2 extends AppCompatActivity {

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

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(tochscreen_page_2.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

ImageAdapter ImageAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
            R.drawable.gradient_charger, R.drawable.gradient_flashlight,
            R.drawable.gradient_vibrtion, R.drawable.gradient_charger,













    };
}

This is the result: http://prntscr.com/kvqe34 结果就是这样: http : //prntscr.com/kvqe34

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM