gridview九宫图+textview文字+点击事件
时间: 2023-07-31 22:09:03 浏览: 97
好的,您可以参考以下代码实现GridView的九宫图和TextView文字,以及点击事件:
1. 在布局文件中添加GridView和TextView:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是一段文字"/>
</LinearLayout>
```
2. 创建GridView的item布局文件(grid_item.xml),添加ImageView和TextView:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片"/>
</LinearLayout>
```
3. 创建一个GridView的适配器(GridAdapter.java):
```java
public class GridAdapter extends BaseAdapter {
private Context mContext;
private List<Integer> mList;
public GridAdapter(Context context, List<Integer> list) {
mContext = context;
mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = convertView.findViewById(R.id.imageView);
viewHolder.textView = convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageResource(mList.get(position));
viewHolder.textView.setText("图片" + position);
return convertView;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
}
```
4. 在Activity中使用GridView和TextView,并设置点击事件:
```java
public class MainActivity extends AppCompatActivity {
private GridView mGridView;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = findViewById(R.id.gridView);
mTextView = findViewById(R.id.textView);
// 图片资源
List<Integer> list = new ArrayList<>();
list.add(R.drawable.pic1);
list.add(R.drawable.pic2);
list.add(R.drawable.pic3);
list.add(R.drawable.pic4);
list.add(R.drawable.pic5);
list.add(R.drawable.pic6);
list.add(R.drawable.pic7);
list.add(R.drawable.pic8);
list.add(R.drawable.pic9);
GridAdapter adapter = new GridAdapter(this, list);
mGridView.setAdapter(adapter);
// GridView的item点击事件
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "点击了第" + position + "张图片", Toast.LENGTH_SHORT).show();
}
});
// TextView的点击事件
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了文字", Toast.LENGTH_SHORT).show();
}
});
}
}
```
希望这个示例能够帮助您实现GridView的九宫图和TextView文字,并设置点击事件!
阅读全文