效果图
添加依赖
implementation 'com.github.mtjsoft:GridPager:v3.7.0'
layout文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager android:id="@+id/gridviewpager" android:layout_width="match_parent" android:layout_height="wrap_content" app:pager_MarginTop="10dp" app:pager_MarginBottom="0dp" app:verticalSpacing="10dp" app:img_width="44dp" app:img_height="44dp" app:text_color="@color/black" app:text_size="12sp" app:imgtext_margin="5dp" app:row_count="2" app:column_count="4" app:pager_loop="true" app:point_is_show="true" app:point_width="15dp" app:point_height="2dp" app:point_is_circle="false" app:point_margin="2dp" app:point_normal_color="@color/white" app:point_select_color="#f00" app:point_margin_page="10dp" app:point_margin_bottom="10dp" app:background_color="@color/white"> </cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager> <cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager android:id="@+id/gridviewpager2" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> </cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager> </LinearLayout>
MainActivity文件
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager; public class MainActivity extends AppCompatActivity { private String[] titles = {"美食", "电影", "酒店住宿","美食", "电影", "酒店住宿"}; private int[] iconS = {R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridViewPager gridViewPager = findViewById(R.id.gridviewpager); gridViewPager // 设置数据总数量 .setDataAllCount(titles.length) // 设置背景图片(此时设置的背景色无效,以背景图片为主) // 数据绑定 .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { @Override public void displayImageText(ImageView imageView, TextView textView, int position) { // 自己进行数据的绑定,灵活度更高,不受任何限制 imageView.setImageResource(iconS[position]); textView.setText(titles[position]); } }) // Item点击 .setGridItemClickListener(new GridViewPager.GridItemClickListener() { @Override public void click(int position) { Toast.makeText(getBaseContext(), "点击了" + titles[position] + position, Toast.LENGTH_SHORT).show(); } }).show(); } }
GitHub地址 下载前给star
标签:foreground,launcher,布局,网格,import,android,drawable,ic From: https://www.cnblogs.com/Frank-dev-blog/p/16819482.html