我的应用中有一种奇怪的行为。我正在使用一个gridview和一个imageadapter类。现在,如果我将手机方向从纵向更改为横向,则会崩溃。但如果我从肖像中改变景观,那么它的效果非常好。我有两个独立的纵向和横向布局,存储在res / layout和res / layout-land
中这是我的活动课
@Override
protected void onCreate(Bundle savedBundle) {
// TODO Auto-generated method stub
super.onCreate(savedBundle);
setContentView(R.layout.rashi_grid);
RashiImageAdapter adapter = new RashiImageAdapter(RashiFragmentActivity.this);
int count = adapter.getCount();
System.out.println("Total Count From RashiFragmentactivity:-"+count);
StaggeredGridView gridView = (StaggeredGridView) findViewById(R.id.gridview);
gridView.setAdapter(adapter);
//GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(StaggeredGridView parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent product = new Intent(RashiFragmentActivity.this,productActivity.class);
product.putExtra("productname", position);
startActivity(product);
finish();
}
});
}
这是我的imageadapter类
private Context mycontext;
private LayoutInflater mInflater;
public Integer ImageId[]={
R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,
R.drawable.e,R.drawable.f
};
public String productName[]={
"a","b","c",
"d","e","f"
};
public String productprice[]={
"2450" ,"3990", "2490","2490","5100","1350"
};
public RashiImageAdapter(Context context)
{
mycontext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ImageId.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder vh;
if (v == null)
{
vh = new ViewHolder();
v = mInflater.inflate(R.layout.row_grid,parent,false);
vh.productname = (TextView)v.findViewById(R.id.productname);
vh.productprice = (TextView)v.findViewById(R.id.product_price);
vh.productpricedetails = (TextView)v.findViewById(R.id.product_pricedetails);
vh.imageView = (ImageView)v.findViewById(R.id.grid_item_image);
v.setTag(vh);
}
else
{
vh = (ViewHolder) v.getTag();
}
vh.productpricedetails.setText(productprice[position]);
vh.productname.setText(productName[position]);
vh.imageView.setImageResource(ImageId[position]);
vh.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return v;
}
这是我的用于肖像的gridview xml文件
<com.origamilabs.library.views.StaggeredGridView
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:id="@+id/gridview"
staggered:numColumns="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
staggered:drawSelectorOnTop="false"
android:layout_marginRight="0.5dp"
android:layout_marginLeft="0.5dp"
android:layout_gravity="top"
android:stretchMode="columnWidth"
/>
和横向
<com.origamilabs.library.views.StaggeredGridView
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:id="@+id/gridview"
staggered:numColumns="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
staggered:drawSelectorOnTop="false"
android:layout_marginRight="0.5dp"
android:layout_marginLeft="0.5dp"
android:layout_gravity="top"
android:stretchMode="columnWidth"
/>
我在哪里做错了?
Logcat错误
12-02 11:37:14.993: E/AndroidRuntime(7880): FATAL EXCEPTION: main
12-02 11:37:14.993: E/AndroidRuntime(7880): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
12-02 11:37:14.993: E/AndroidRuntime(7880): at com.origamilabs.library.views.StaggeredGridView.populate(StaggeredGridView.java:966)
12-02 11:37:14.993: E/AndroidRuntime(7880): at com.origamilabs.library.views.StaggeredGridView.onLayout(StaggeredGridView.java:920)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.View.layout(View.java:14072)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.View.layout(View.java:14072)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.View.layout(View.java:14072)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.View.layout(View.java:14072)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1997)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.Choreographer.doFrame(Choreographer.java:525)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.os.Handler.handleCallback(Handler.java:615)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.os.Looper.loop(Looper.java:137)
12-02 11:37:14.993: E/AndroidRuntime(7880): at android.app.ActivityThread.main(ActivityThread.java:4921)
12-02 11:37:14.993: E/AndroidRuntime(7880): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 11:37:14.993: E/AndroidRuntime(7880): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 11:37:14.993: E/AndroidRuntime(7880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
12-02 11:37:14.993: E/AndroidRuntime(7880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
12-02 11:37:14.993: E/AndroidRuntime(7880): at dalvik.system.NativeStart.main(Native Method)
还有一件事是..如果我在layout-land / grid.xml文件中将列大小2更改为1,则错误不会到来。它工作正常。为什么ithis有奇怪的行为?如何完成我的代码以将肖像更改为横向,并且布局应根据横向到纵向发生的情况进行更改。如果风景画像工作得很好那么为什么画像不能用于风景呢?
答案 0 :(得分:4)
我找到了解决方案。这是StaggeredGridView库的一个错误。
如果有人遇到同样的问题,我会发布解决方案。
您需要将以下代码更改为StaggeredGridView.java类
final int top = getPaddingTop();
for(int i = 0; i<colCount; i++){
final int offset = top + ((mRestoreOffsets != null)? Math.min(mRestoreOffsets[i], 0) : 0);
mItemTops[i] = (offset == 0) ? mItemTops[i] : offset;
mItemBottoms[i] = (offset == 0) ? mItemBottoms[i] : offset;
到此代码
final int top = getPaddingTop();
for(int i = 0; i<colCount; i++){
// final int offset = top + ((mRestoreOffsets != null)? Math.min(mRestoreOffsets[i], 0) : 0);
int offset = top;
if((mRestoreOffsets != null) && mRestoreOffsets.length == colCount)
offset += Math.min(mRestoreOffsets[i], 0);
mItemTops[i] = (offset == 0) ? mItemTops[i] : offset;
mItemBottoms[i] = (offset == 0) ? mItemBottoms[i] : offset;
答案 1 :(得分:0)
当你改变方向时,会调用覆盖方法:
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
当您更改方向时,视图会重新加载,您必须在onSaveInstanceState
方法中保留您的值并恢复或应用值才能在onRestoreInstanceState
中查看...