它工作了一段时间然后突然停止工作。 我不明白我在代码中改变了什么。 我正在使用volley发送一个json数组请求,它返回id,name,icon(图像的url)。 我已经检查过,数据来自服务器。
public void setup(JSONArray response) {
ImageLoader imageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();
gridLayout.removeAllViews();
int total = response.length();
int column = 3;
int row = total / column;
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
for (int i = 0, c = 0, r = 0; i < total; i++, c++) {
if (c == column) {
c = 0;
r++;
}
final float scale = getActivity().getResources().getDisplayMetrics().density;
int pixels = (int) (90 * scale + 0.5f);
Log.e("TAG", "pixels: " + pixels);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = pixels;
param.width = pixels;
param.rightMargin = 5;
param.leftMargin = 5;
param.bottomMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
try {
jsonObject = response.getJSONObject(i);
final String id = jsonObject.getString("id");
final String name = jsonObject.getString("name");
final String icon = jsonObject.getString("icon");
LinearLayout llParent = new LinearLayout(getActivity());
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayoutParams.gravity = Gravity.CENTER;
llParent.setLayoutParams(linearLayoutParams);
llParent.setOrientation(LinearLayout.VERTICAL);
NetworkImageView iv = new NetworkImageView(getActivity());
LinearLayout.LayoutParams ivLayoutParams = new LinearLayout.LayoutParams(80, 80);
ivLayoutParams.gravity = Gravity.CENTER;
iv.setLayoutParams(ivLayoutParams);
if (icon.length() > 0) {
iv.setImageUrl(icon, imageLoader);
} else {
iv.setImageResource(R.drawable.logo);
}
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
callbacks.onFragmentInteraction(id, name);
} catch (Exception e) {
e.toString();
}
}
});
TextView tv1 = new TextView(getActivity());
tv1.setLayoutParams(linearLayoutParams);
tv1.setText(name.toUpperCase());
tv1.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
tv1.setTextColor(Color.GRAY);
llParent.setLayoutParams(param);
llParent.addView(iv);
llParent.addView(tv1);
gridLayout.addView(llParent);
jsonObject = null;
} catch (Exception e) {
}
}
progressBar.setVisibility(View.GONE);
content.setVisibility(View.VISIBLE);
}