我解析图像 url 论坛 json 数组并添加它们 staggeredgridview 所以布局看起来像 pinterest :)
将方向空间切换到纵向或纵向到陆地空间我创建两个布局文件,其中一个是布局端口(它有 2 列),另一个是布局土地(它有 3 列)。
当我尝试将土地切换到端口时,它会正确更改,但端口会返回 ArrayIndexOutOfBoundsException
错误
09-05 20:34:55.412: E/ViewRootImpl(26391): sendUserActionEvent() mView == null
09-05 20:35:00.182: E/dalvikvm(26391): The following issues were seen:
09-05 20:35:04.302: E/AndroidRuntime(26391): FATAL EXCEPTION: main
09-05 20:35:04.302: E/AndroidRuntime(26391): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.origamilabs.library.views.StaggeredGridView.populate(StaggeredGridView.java:965)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.origamilabs.library.views.StaggeredGridView.onLayout(StaggeredGridView.java:919)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.onLayout(CustomViewAbove.java:476)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2191)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2005)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1218)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4975)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer.doFrame(Choreographer.java:561)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.os.Handler.handleCallback(Handler.java:725)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.os.Looper.loop(Looper.java:137)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.app.ActivityThread.main(ActivityThread.java:5279)
09-05 20:35:04.302: E/AndroidRuntime(26391): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 20:35:04.302: E/AndroidRuntime(26391): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-05 20:35:04.302: E/AndroidRuntime(26391): at dalvik.system.NativeStart.main(Native Method)
公共类 MainActivity 扩展 Activity {
private static final String TAG_CATEGORY = "catName";
private static final String TAG_ID = "id";
private static String categoryId;
// token ve tokenValue
static String Token = "token";
static String tokenValue = "********";
private static ArrayList<HashMap<String, String>> contactList;
private static String[] urlArray;
SlidingMenu menu;
jsonParser jsonParser;
JSONArray jArray;
ListView lvSiliding;
PARSEJSONCATEGORYCONTENT categoryContent;
PARSEJSONCATEGORY categoryName;
public static StaggeredGridView gridView;
Display display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(5);
menu.setFadeDegree(0.0f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindWidth(400);
menu.setMenu(R.layout.sliding_menu);
jsonParser = new jsonParser();
contactList = new ArrayList<HashMap<String, String>>();
lvSiliding = (ListView) findViewById(R.id.listView_sliding_menu);
gridView = (StaggeredGridView) MainActivity.this
.findViewById(R.id.staggeredGridView1);
categoryName = new PARSEJSONCATEGORY();
categoryName.execute();
categoryContent = new PARSEJSONCATEGORYCONTENT();
categoryContent.execute("6");
lvSiliding.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
categoryId = ((TextView) view.findViewById(R.id.categoryID))
.getText().toString();
menu.toggle();
Toast.makeText(getApplicationContext(), categoryId,
Toast.LENGTH_LONG).show();
categoryContent = new PARSEJSONCATEGORYCONTENT();
categoryContent.execute(categoryId);
}
});
}
// parse sliding menu item
private class PARSEJSONCATEGORY extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
String urlCatName = "******";
jArray = jsonParser.getJSONFromUrltoCategoryName(urlCatName, Token,
tokenValue);
try {
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
// Storing each json item in variable
String catName = c.getString(TAG_CATEGORY);
String catID = c.getString(TAG_ID);
Log.d("catName", catName);
Log.d("catID", catID);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CATEGORY, catName);
map.put(TAG_ID, catID);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
ListAdapter adapter1 = new SimpleAdapter(MainActivity.this,
contactList, R.layout.list_item, new String[] {
TAG_CATEGORY, TAG_ID }, new int[] {
R.id.categoryName, R.id.categoryID });
lvSiliding.setAdapter(adapter1);
}
}
// parse girdView item
private class PARSEJSONCATEGORYCONTENT extends
AsyncTask<String, Void, String[]> {
protected String[] doInBackground(String... params) {
String identifier = params[0];
String urlCatContent = "*******";
jArray = jsonParser.getJSONFromUrltoCategoryContent(urlCatContent,
Token, tokenValue, identifier, "0", "10");
List<String> list = new ArrayList<String>();
try {
// looping through All Contacts
for (int i = 0; i < jArray.length(); i++) {
JSONObject k = jArray.getJSONObject(i);
// Storing each json item in variable
String catName = k.getString("ipad_URL");
Log.d("ipad_URL", catName);
list.add(catName);
urlArray = new String[list.size()];
list.toArray(urlArray);
}
String arrayLenght = Integer.toString(urlArray.length);
Log.d("arrayLenght", arrayLenght);
} catch (JSONException e) {
e.printStackTrace();
}
return urlArray;
}
protected void onPostExecute(String[] args) {
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
gridView.setItemMargin(margin + 10); // set the GridView margin
gridView.setPadding(margin + 10, margin + 10, margin + 10,
margin + 10); // have the margin on the
// sides as well
StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this,
R.id.imageView1, urlArray);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
categoryContent.cancel(true);
}
}
}
布局土地
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:staggered="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity" >
<com.origamilabs.library.views.StaggeredGridView
android:id="@+id/staggeredGridView1"
staggered:numColumns="3"
staggered:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
布局端口
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:staggered="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity" >
<com.origamilabs.library.views.StaggeredGridView
android:id="@+id/staggeredGridView1"
staggered:numColumns="2"
staggered:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>