Android笔记之GridView

完整Demo链接:https://pan.baidu.com/s/1d_G9aCwBxpiYQcdQhwSDDw,提取码:5deh

效果图

Android笔记之GridView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gvMovie"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="2dp"
    android:numColumns="2"
    android:verticalSpacing="2dp" />

MainActivity.java

package com.bu_ish.grid_view_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private GridView gvMovie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gvMovie = findViewById(R.id.gvMovie);
        ArrayList<Movie> movies = new ArrayList<>();
        movies.add(new Movie("刀", "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=0bdb508d4c90f60310bd9415587bd87e/ac345982b2b7d0a28dadd821cdef76094b369a22.jpg", "赵文卓"));
        movies.add(new Movie("破坏之王", "https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=9bf1fbb98f5494ee932f074b4c9c8b9b/241f95cad1c8a786a13830086409c93d70cf50a1.jpg", "周星驰"));
        movies.add(new Movie("第一滴血3", "https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=1f2bd403df1373f0e13267cdc566209e/d52a2834349b033b95ddb0a21bce36d3d539bd75.jpg", "史泰龙"));
        movies.add(new Movie("杀手之王", "https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=526d4c9c337adab429dd1311eabdd879/adaf2edda3cc7cd92764cb6d3c01213fb80e913d.jpg", "李连杰"));
        movies.add(new Movie("我是谁", "https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=1944c477cfea15ce55e3e85bd7695196/7e3e6709c93d70cf21b800fffedcd100bba12b85.jpg", "成龙"));
        gvMovie.setAdapter(new MovieGridViewAdapter(this, movies));
    }
}

item_movie.xml

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

    <ImageView
        android:id="@+id/ivThumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        tools:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/movie_text_view_background"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textColor="@android:color/white"
            tools:text="断刀客" />

        <TextView
            android:id="@+id/tvStarring"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@drawable/movie_text_view_background"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textColor="@android:color/white"
            tools:text="主演:赵文卓" />
    </LinearLayout>
</FrameLayout>

movie_text_view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#55000000" />
    <corners android:radius="5dp" />
</shape>

MovieGridViewAdapter.java

package com.bu_ish.grid_view_demo;

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

import com.bumptech.glide.Glide;

import java.util.List;

public class MovieGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<Movie> movies;

    public MovieGridViewAdapter(Context context, List<Movie> movies) {
        this.context = context;
        this.movies = movies;
    }

    @Override
    public int getCount() {
        return movies.size();
    }

    @Override
    public Movie getItem(int position) {
        return movies.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_movie, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Movie movie = movies.get(position);
        Glide.with(context).load(movie.getThumbnail()).into(viewHolder.ivThumbnail);
        viewHolder.tvName.setText(movie.getName());
        viewHolder.tvStarring.setText(movie.getStarring());
        return convertView;
    }

    private class ViewHolder {
        private ImageView ivThumbnail;
        private TextView tvName, tvStarring;

        public ViewHolder(View itemView) {
            ivThumbnail = itemView.findViewById(R.id.ivThumbnail);
            tvName = itemView.findViewById(R.id.tvName);
            tvStarring = itemView.findViewById(R.id.tvStarring);
        }
    }
}

Android笔记之GridView

上一篇:Spring Security 报There is no PasswordEncoder mapped for the id "null"


下一篇:@RequestMapping和@GetMapping和PostMapping