GridView初始时加载一张图片,点击后加载另外一张大图

时间:2022-11-29 10:23:21

GridView初始时加载一张图片,点击后加载另外一张大图

<pre name="code" class="java">public class MainActivity_gv extends Activity {

private GridView gView;

private AQuery aq;

private ImageView iView;

private List<Menu1> list = new ArrayList<Menu1>();

private LayoutInflater layoutInflater;
//=============================主要代码===========================================================
private int selectPic = -1;
//=============================主要代码===========================================================

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_gv);

aq = new AQuery(this);

gView = (GridView) findViewById(R.id.mian_gv);

iView = (ImageView) findViewById(R.id.main_iv);

layoutInflater = LayoutInflater.from(this);
//=============================主要代码===========================================================
gView.setSelector(android.R.color.transparent);//设置背景为透明色
//=============================主要代码===========================================================
aq.ajax("http://192.168.0.86:8080/DownGoingMIPD/json/sort/mobile/project.txt",
JSONObject.class, new AjaxCallback<JSONObject>() {

@Override
public void callback(String url, JSONObject object,
AjaxStatus status) {
// TODO Auto-generated method stub
super.callback(url, object, status);

JSONArray jsonArray = result.optJSONArray("content");

for (int i = 0; i < jsonArray.length(); i++) {
Menu1 menu1 = new Menu1();

try {
JSONObject jsonObject = jsonArray
.getJSONObject(i);

String id = jsonObject.optString("project_id");
String name = jsonObject
.optString("project_name");

String patha = jsonObject
.optString("img_patha");

String pathb = jsonObject
.optString("img_pathb");

String next = jsonObject.optString("sort_next");

menu1.setMenu1Id(id);

menu1.setMenu1Name(name);

menu1.setMenuPicPatha(patha);
menu1.setMenuPicPathb(pathb);

menu1.setNext(next);

list.add(menu1);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Menu1Adapter_net menu1Adapter_net = new Menu1Adapter_net();

gView.setAdapter(menu1Adapter_net);

gView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int id, long arg3) {
// TODO Auto-generated method stub
//=============================主要代码===========================================================
Menu1Adapter_net ad = (Menu1Adapter_net) arg0
.getAdapter();
ad.setNotifyDataChange(id);
//=============================主要代码===========================================================
}
});

}
});

}

public class Menu1Adapter_net extends BaseAdapter {
public Menu1Adapter_net() {

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//=============================主要代码===========================================================
public void setNotifyDataChange(int id) {
selectPic = id;
super.notifyDataSetChanged();
}
//=============================主要代码===========================================================
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.gridview_item,
null);
}
ImageView iv = (ImageView) convertView
.findViewById(R.id.gridview_iv_first_tubiao);
//初始化时加载一张图片
aq.id(iv).image(list.get(position).getMenuPicPatha());
//=============================主要代码===========================================================
//点击时加载另外一张图片
if (selectPic == position) {

aq.id(iv).image(list.get(position).getMenuPicPathb());
// CENTER_CROP / centerCrop
// 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
iv.setScaleType(ScaleType.CENTER_CROP);

}
//=============================主要代码===========================================================

return convertView;

}

}

public class Menu1 {

/** 对应id **/
private String menu1Id;
/** 配图路径a **/
private String menuPicPatha;

/** 配图路径b **/
private String menuPicPathb;
private int count;
/*** 名称 **/
private String menu1Name;
/*** 顺序 **/
private String menu1Order;

/** 是否有下一级 **/
private String next;

public String getNext() {
return next;
}

public void setNext(String next) {
this.next = next;
}

public String getMenuPicPatha() {
return menuPicPatha;
}

public void setMenuPicPatha(String menuPicPatha) {
this.menuPicPatha = menuPicPatha;
}

public String getMenuPicPathb() {
return menuPicPathb;
}

public void setMenuPicPathb(String menuPicPathb) {
this.menuPicPathb = menuPicPathb;
}

/***
* 来源(数据类型)
*
* 1---push
*
* 2--其它
**/
private String menu1Type;

public String getMenu1Id() {
return menu1Id;
}

public void setMenu1Id(String menu1Id) {
this.menu1Id = menu1Id;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public String getMenu1Name() {
return menu1Name;
}

public void setMenu1Name(String menu1Name) {
this.menu1Name = menu1Name;
}

public String getMenu1Order() {
return menu1Order;
}

public void setMenu1Order(String menu1Order) {
this.menu1Order = menu1Order;
}

public String getMenu1Type() {
return menu1Type;
}

public void setMenu1Type(String menu1Type) {
this.menu1Type = menu1Type;
}
}

}