item九宫格

这次代码和上次差不多


首先原生布局是





item布局的代码








Java代码

package com.example.zhdn.photo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.example.zhdn.photo.R.drawable.h;


public class MainActivity extends AppCompatActivity {
private GridView gridView;
private SimpleAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.yf_put);
adapter = new SimpleAdapter(this,getData(),
R.layout.item,
new String[]{"yf_photo"},
new int[]{R.id.yf_photo});

gridView = (GridView) findViewById(R.id.yf_put);
gridView.setAdapter(adapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
String data = (String) adapterView.getItemAtPosition(position);
 }
 });
}

private List> getData(){
List> datas = new ArrayList<>();

HashMapdata = new HashMap<>();
data.put("yf_photo",R.drawable.a);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.b);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.c);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.d);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.e);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.f);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.g);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo", h);
datas.add(data);

data = new HashMap<>();
data.put("yf_photo",R.drawable.i);
datas.add(data);

return datas;
}
}

运行结果如下

item九宫格_第1张图片

你可能感兴趣的:(item九宫格)