Android Learning Grid View

I. Introduction:

GridView is a component that displays multiple pictures in the form of tables. It displays the content in the form of rows and columns. For example, to implement the nine palace diagram, GridView is the first choice.

2. Code block:

The students who read my last blog should know that it takes a lot of time to write all the steps step by step, and the process is like that, so this grid view will go directly to the code block, and the steps are almost the same.

Add a button to activity main.xml:

<Button
        android:id="@+id/btn_gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="GridView"
        android:textAllCaps="false"/>

MainActivity added on the original basis:

Declare the control first:

private Button mBtnGridView;

In onCreate:

mBtnGridView=findViewById(R.id.btn_gridview);

In setListeners:

mBtnGridView.setOnClickListener(onClick);

On OnClick:

        case R.id.btn_gridview:
        //Jump to GridView presentation page
            intent = new Intent(MainActivity.this, GridViewActivity.class);
            break;

In activity grid view.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:padding="15dp">

    <GridView
        android:id="@+id/gv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"/>
</LinearLayout>

In GridViewActivity:

Declare the control first:

private GridView mGv;

In onCreate:

mGv=findViewById(R.id.gv);

To create a new MyGridViewAdapter before:

public class MyGridViewAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }
}

To create a new layout grid item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <ImageView
        android:id="@+id/iv_grid"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scaleType="fitCenter"
        android:background="@color/colorPrimaryDark"/>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textColor="@color/colorAccent"
        android:gravity="center"
        android:layout_marginTop="10dp"
        />
</LinearLayout>

Modify in MyGridViewAdapter:

public class MyGridViewAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MyGridViewAdapter(Context context){
        this.mContext=context;
        mLayoutInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    static class ViewHolder{
        public ImageView imageView;
        public TextView textView;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=mLayoutInflater.inflate(R.layout.layout_grid_item,null);
            holder=new ViewHolder();
            holder.imageView=view.findViewById(R.id.iv_grid);
            holder.textView=view.findViewById(R.id.tv_title);
            view.setTag(holder);
        }else {
            holder=(ViewHolder)view.getTag();
        }
        //assignment
        holder.textView.setText("ycm");
        Glide.with(mContext).load("http://i1.bvimg.com/670191/a72f2a8c0f289d48s.png").into(holder.imageView);
        return view;
    }
}

Add in GridViewActivity:

mGv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));

III. operation screenshot:


Set click and long press events as described in the ListView before, it will not be introduced.

Keywords: Android xml encoding

Added by undertaker16 on Thu, 05 Dec 2019 02:36:28 +0200