效果图:
代码:
UserGridviewAdapter
package com.example.gridviewpro.Adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.gridviewpro.R;
import com.example.gridviewpro.bean.UserGridviewBean;
import java.util.List;
public class UserGridviewAdapter extends BaseAdapter {
private Context mContext;
private List<UserGridviewBean> mUserGridviewBeanList;
public UserGridviewAdapter(Context context, List<UserGridviewBean> userGridviewBeanList) {
mContext = context;
mUserGridviewBeanList = userGridviewBeanList;
}
@Override
public int getCount() {
return mUserGridviewBeanList == null ? 0 : mUserGridviewBeanList.size();
}
@Override
public Object getItem(int i) {
return getItem(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = View.inflate(mContext, R.layout.gridview_item, null);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
UserGridviewBean userGridviewBean = mUserGridviewBeanList.get(i);
viewHolder.mImageView.setImageResource(R.drawable.img);
viewHolder.mUserName.setText(userGridviewBean.getUserName());
viewHolder.mPower.setText(String.valueOf(userGridviewBean.getPower()));
return view;
}
static
class ViewHolder {
View mView;
ImageView mImageView;
TextView mUserName;
TextView mPower;
ViewHolder(View view) {
this.mView = view;
this.mImageView = (ImageView) view.findViewById(R.id.img);
this.mUserName = (TextView) view.findViewById(R.id.userName);
this.mPower = (TextView) view.findViewById(R.id.power);
}
}
}
UserGridviewBean
package com.example.gridviewpro.bean;
public class UserGridviewBean {
private int mImage;
private String mUserName;
private int mPower;
public UserGridviewBean(int image, String userName, int power) {
mImage = image;
mUserName = userName;
mPower = power;
}
public int getImage() {
return mImage;
}
public void setImage(int image) {
mImage = image;
}
public String getUserName() {
return mUserName;
}
public void setUserName(String userName) {
mUserName = userName;
}
public int getPower() {
return mPower;
}
public void setPower(int power) {
mPower = power;
}
}
MainActivity
package com.example.gridviewpro;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import com.example.gridviewpro.Adapter.UserGridviewAdapter;
import com.example.gridviewpro.bean.UserGridviewBean;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<UserGridviewBean> mUserGridviewBeanList = new ArrayList<>();
private GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DateResourse();
mGridView = findViewById(R.id.gv);
mGridView.setAdapter(new UserGridviewAdapter(MainActivity.this, mUserGridviewBeanList));
//监控item点击情况
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, mUserGridviewBeanList.get(i).getUserName(), Toast.LENGTH_SHORT).show();
}
});
}
private void DateResourse() {
for (int i = 0; i < 100; i++) {
mUserGridviewBeanList.add(new UserGridviewBean(R.drawable.img, "张三" + i, 100 + i));
}
}
}
item文章来源:https://www.toymoban.com/news/detail-672484.html
<?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:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_gravity="center"/>
</LinearLayout>
activity_main.xml文章来源地址https://www.toymoban.com/news/detail-672484.html
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"/>
</LinearLayout>
到了这里,关于Android studio之GridView使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!