网格视图GridView
作者:互联网
首先建立gridViewActivity的Java文件
1 public class GridViewActivity extends AppCompatActivity { 2 private GridView GV; 3 @Override 4 protected void onCreate(@Nullable Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_gridview); 7 GV=findViewById(R.id.gv); 8 GV.setAdapter(new MygridAdapter(GridViewActivity.this));//为gridview设置适配器 9 GV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 10 @Override 11 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 12 Toast.makeText(GridViewActivity.this, "点击位置:"+i, Toast.LENGTH_SHORT).show(); 13 } 14 }); 15 GV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 16 @Override 17 public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { 18 Toast.makeText(GridViewActivity.this, "长按位置:"+i, Toast.LENGTH_SHORT).show(); 19 return true;//这里如果返回false的话,处理完长按事件后还会处理点击事件,也就是说会弹出两个提示框,为了让他只显示一个对话框改为true 20 //即处理完该事件后不会再处理其他事件,结束 21 } 22 }); 23 } 24 }
然后建立activity_gridview.xml文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 android:padding="15dp"> 6 <GridView 7 android:id="@+id/gv" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:numColumns="3" 11 android:horizontalSpacing="10dp" 12 android:verticalSpacing="10dp"/> 13 <!--numColumns代表列数,horizontalSpacing是水平距离,verticalSpacing是垂直距离--> 14 15 </LinearLayout>
建立适配器MygridAdapter的Java文件
1 public class MygridAdapter extends BaseAdapter { 2 private Context gContext; 3 private LayoutInflater gLayoutInflater; 4 public MygridAdapter(Context context){ 5 this.gContext=context; 6 gLayoutInflater=LayoutInflater.from(context); 7 } 8 9 @Override 10 public int getCount() { 11 return 9; 12 }//设置数量是多少 13 14 @Override 15 public Object getItem(int i) { 16 return null; 17 } 18 19 @Override 20 public long getItemId(int i) { 21 return 0; 22 } 23 24 static class ViewHolder{ 25 public ImageView imageview; 26 public TextView textView; 27 } 28 29 @Override 30 public View getView(int i, View view, ViewGroup viewGroup) { 31 ViewHolder holder=null; 32 if(view==null){ 33 view=gLayoutInflater.inflate(R.layout.layout_grid_item,null); 34 holder=new ViewHolder(); 35 holder.imageview=view.findViewById(R.id.imv_grid); 36 holder.textView=view.findViewById(R.id.tv_timu); 37 view.setTag(holder); 38 }else{ 39 holder= (ViewHolder) view.getTag(); 40 } 41 //对控件赋值 42 holder.textView.setText("我的女神!!!"); 43 Glide.with(gContext).load("https://img0.baidu.com/it/u=1038436406,198337591&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=711").into(holder.imageview); 44 return view; 45 } 46 }
然后建立单个文件的样式,layout_grid_item.xml文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 android:gravity="center_horizontal"> 6 <ImageView 7 android:id="@+id/imv_grid" 8 android:layout_width="match_parent" 9 android:layout_height="100dp" 10 android:scaleType="centerCrop" 11 android:background="@color/black"/> 12 <TextView 13 android:id="@+id/tv_timu" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="好吧,又是我" 17 android:textColor="@color/purple_500" 18 android:gravity="center" 19 android:layout_marginTop="10dp"/> 20 21 </LinearLayout>
最后注意要在AndroidMainfest.xml文件中添加activity_gridview.xml。
标签:GV,GridView,int,网格,视图,Override,holder,public,view 来源: https://www.cnblogs.com/konglong-cm/p/15854086.html