滚动时网格项目正在改变

时间:2015-02-24 19:24:19

标签: android gridview android-custom-view baseadapter

您好我正在使用自定义GridView。

GridView放在以下xml / layout文件中。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/music_home_ad_view"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <ScrollView
        android:id="@+id/music_home_root_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ScrollView>

    <FrameLayout
        android:id="@+id/music_home_promo_frame"
        android:layout_width="match_parent"
        android:layout_height="@dimen/music_home_promo_height"
        android:layout_marginTop="20dp" >

        <android.support.v4.view.ViewPager
            android:id="@+id/music_home_promo_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.raaga.home.CirclePageIndicator
            android:id="@+id/music_home_page_indicator"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_gravity="bottom"
            android:padding="10dp" />
    </FrameLayout>

    <GridView
        android:id="@+id/music_home_grid_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/music_home_promo_frame"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

GridView包含以下视图

music_grid_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@android:color/transparent"
    >

    <ImageView
        android:id="@+id/music_grid_image"
        android:layout_width="60dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:src="@drawable/new_releases" 

        />

    <TextView
        android:id="@+id/music_grid_category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/music_grid_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" 
        android:text="New Releases"
        android:textColor="@color/text_white"
        />

</RelativeLayout>

我使用了自定义适配器类并遵循this link

MusicCategoryAdapter.java

    public class MusicCategoryAdapter extends BaseAdapter{

    ArrayList<Integer> categoryImageList;
    ArrayList<String> categoryNameList;
    Context mContext;

    public MusicCategoryAdapter(Context mContext, ArrayList<Integer> categoryImageList, ArrayList<String> categoryNameList) {

        this.mContext= mContext;
        this.categoryImageList = categoryImageList;
        this.categoryNameList = categoryNameList;

    }


    @Override
    public int getCount() {

        //send the list length
        return categoryImageList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
         View grid = convertView;
          LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              if (convertView == null) {
                grid = new View(mContext);
                convertView = inflater.inflate(R.layout.music_grid_item, null);
                holder = new GridHolder();
            holder.categoryTitle = (TextView) convertView.findViewById(R.id.music_grid_category_name);
            holder.categoryImage = (ImageView)convertView.findViewById(R.id.music_grid_image);
            holder.categoryTitle.setText(categoryNameList.get(position));
            holder.categoryImage.setImageResource(categoryImageList.get(position));
                convertView.setTag(holder);
              } else {
                holder = (GridHolder) convertView.getTag();
              }
          return convertView;

    }

    public class GridHolder{
        ImageView categoryImage;
        TextView categoryTitle;
    }
}

我正面临滚动时改变位置重复项目的问题。

有人可以帮忙解决这个问题吗?我在这做了什么错误?

1 个答案:

答案 0 :(得分:0)

将scrollview放在相对布局的开头,并在相对布局的末尾结束标记。 这将使您的整个布局可滚动,您将克服这个问题。