Android中的自定义GridView滚动问题

时间:2013-05-27 06:14:18

标签: android gridview scroll

我有一个动态显示数据的自定义GridView。数据已加载,但滚动似乎无法正常工作。当我向下滚动gridview时,滚动条向上移动而不保留滚动位置。

以下是我的Custom GridView代码,

public class CustomAdapter extends BaseAdapter {

  private Context context;
  private List<PropertiesList> myList;

  public CustomAdapter(List<PropertiesList> paramList, Context paramContext)
  {
    this.myList = paramList;
    this.context = paramContext;
  }

  public int getCount()
  {
    return this.myList.size();
  }

  public Object getItem(int paramInt)
  {
    return Integer.valueOf(paramInt);
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup)
  {

    View localView = View.inflate(this.context, R.layout.grid_content, null);
    localView.setMinimumHeight((int)(0.3D * ((WindowManager)this.context.getSystemService("window")).getDefaultDisplay().getWidth()));
    ((TextView)localView.findViewById(R.prop_type)).setText(((PropertiesList)this.myList.get(paramInt)).PropertyAddress);

    localView.findViewById(R.id.prop_type).setBackgroundResource(color.darker_gray);
    ((TextView)localView.findViewById(R.id.prop_price)).setText(((PropertiesList)this.myList.get(paramInt)).PropertyPrice);
    localView.setId(paramInt);

    ((TextView)localView.findViewById(R.id.prop_type)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(context, "PropertyID: "+myList.get(paramInt).PropertyID, Toast.LENGTH_SHORT).show();

        }
    });


    return localView; 
  }
}

XML,

  <GridView
        android:id="@+id/camera_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center"
        android:columnWidth="110.0dip"
        android:gravity="center"
        android:horizontalSpacing="5.0dip"
        android:numColumns="auto_fit"
        android:orientation="vertical"
        android:scrollingCache="false"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5.0dip"
        android:scrollbars="vertical" />

我的代码中有任何错误的实现?请帮我解决这个问题。

提前致谢!!

1 个答案:

答案 0 :(得分:0)

 public Object getItem(int paramInt)
   {
     return Integer.valueOf(paramInt);
   }

此方法将语句更改为

 mylist.get(paramInt);`