欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例

 更新時(shí)間:2022年08月26日 08:43:35   作者:磚廠打工仔  
這篇文章主要為大家詳細(xì)介紹了GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例,供大家參考,具體內(nèi)容如下

用法與ListView類似,需要以下幾步:

1、定義實(shí)體類
2、自定義適配器繼承BaseAdapter
3、定義GridView內(nèi)部布局

效果圖:

代碼:

實(shí)體類:Icon.java

package com.example.a16gridviewtest.entity;

public class Icon {
? ? private int iconId;
? ? private String name;

? ? public Icon(int iconId, String name) {
? ? ? ? this.iconId = iconId;
? ? ? ? this.name = name;
? ? }

? ? public int getIconId() {
? ? ? ? return iconId;
? ? }

? ? public void setIconId(int iconId) {
? ? ? ? this.iconId = iconId;
? ? }

? ? public String getName() {
? ? ? ? return name;
? ? }

? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
}

自定義適配器:GridViewAdapter.java

package com.example.a16gridviewtest.adpater;

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.example.a16gridviewtest.R;
import com.example.a16gridviewtest.entity.Icon;

import java.util.List;

public class GridViewAdapter extends BaseAdapter {

? ? private List<Icon> mData;
? ? private Context mContext;

? ? public GridViewAdapter(List<Icon> data, Context context) {
? ? ? ? this.mData = data;
? ? ? ? this.mContext = context;
? ? }

? ? @Override
? ? public int getCount() {
? ? ? ? return mData != null ? mData.size() : 0;
? ? }

? ? @Override
? ? public Object getItem(int position) {
? ? ? ? return mData.get(position);
? ? }

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

? ? @Override
? ? public View getView(int position, View convertView, ViewGroup parent) {
? ? ? ? ViewHolder holder = null;
? ? ? ? if (convertView == null) {
? ? ? ? ? ? holder = new ViewHolder();
? ? ? ? ? ? convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid_icon, parent, false);
? ? ? ? ? ? holder.img_icon = convertView.findViewById(R.id.img_icon);
? ? ? ? ? ? holder.name = convertView.findViewById(R.id.txt_icon);
? ? ? ? ? ? convertView.setTag(holder);
? ? ? ? } else {
? ? ? ? ? ? holder = (ViewHolder) convertView.getTag();
? ? ? ? }
? ? ? ? holder.img_icon.setImageResource(mData.get(position).getIconId());
? ? ? ? holder.name.setText(mData.get(position).getName());
? ? ? ? return convertView;
? ? }

? ? class ViewHolder {
? ? ? ? private ImageView img_icon;
? ? ? ? private TextView name;
? ? }
}

MainAcitivity.java

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.gridview_layout);
? ? ? ? //視圖層View
? ? ? ? GridView gridView = findViewById(R.id.gridView);
? ? ? ? //數(shù)據(jù)源Model
? ? ? ? ArrayList<Icon> list = new ArrayList<>();
? ? ? ? list.add(new Icon(R.mipmap.icon1,"QQ"));
? ? ? ? list.add(new Icon(R.mipmap.icon2,"微信"));
? ? ? ? list.add(new Icon(R.mipmap.icon3,"電話"));
? ? ? ? list.add(new Icon(R.mipmap.icon4,"照片"));
? ? ? ? list.add(new Icon(R.mipmap.icon5,"音樂"));
? ? ? ? list.add(new Icon(R.mipmap.icon6,"Chrome"));
? ? ? ? list.add(new Icon(R.mipmap.icon7,"百度"));
? ? ? ? //控制層Controller
? ? ? ? GridViewAdapter adapter = new GridViewAdapter(list,this);
? ? ? ? gridView.setAdapter(adapter);
? ? ? ? //綁定點(diǎn)擊事件
? ? ? ? gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "你點(diǎn)擊了第" + (position+1) + "項(xiàng)", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

主布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:horizontalSpacing="10dp"
? ? android:verticalSpacing="10dp"
? ? android:numColumns="3" >

</GridView>

單個(gè)GridView布局:

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

? ? <ImageView
? ? ? ? android:id="@+id/img_icon"
? ? ? ? android:layout_width="64dp"
? ? ? ? android:layout_height="64dp"
? ? ? ? android:layout_centerInParent="true"
? ? ? ? android:src="@mipmap/icon1" />

? ? <TextView
? ? ? ? android:id="@+id/txt_icon"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/img_icon"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_marginTop="30dp"
? ? ? ? android:text="QQ"
? ? ? ? android:textSize="18sp" />
? ??
</RelativeLayout>
id/img_icon"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_marginTop="30dp"
? ? ? ? android:text="QQ"
? ? ? ? android:textSize="18sp" />
? ??
</RelativeLayout>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論