如何强制GridView加载单元格

时间:2016-08-21 11:32:55

标签: android gridview

GridView以编程方式滚动,并且没有新项目从底部出现。

我尝试使用以下行进行更新,但不会强制GridView加载新项目。

imageAdapter.notifyDataSetChanged();
gridview.invalidateViews();
gridview.setAdapter(imageAdapter);

enter image description here

简化了应用程序,现在可以通过单击按钮来触发滚动,但即将出现的空项仍然出现。这是一些代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CustomGridView gridview = (CustomGridView) findViewById(R.id.gridView1);
        final ImageAdapter imageAdapter = new ImageAdapter(this);
        gridview.setAdapter(imageAdapter);

        gridview.setNumColumns(3);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
        linearParams.width=66*3;
        gridview.setLayoutParams(linearParams);


        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                gridview.scrollBy(0, 44);
            }
        });
    }
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {

        return 300;
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(66, 66));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0); // 8 8 8 8
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(R.drawable.asdf);
        return imageView;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.j4nos.moviebuffs12.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="198dp"
        android:layout_height="match_parent"
        android:layout_marginTop="66dp"
        android:layout_marginLeft="0dp"
        android:layout_marginBottom="0dp">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <com.j4nos.moviebuffs12.CustomGridView
                android:id="@+id/gridView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0dp"
                android:columnWidth="66dp"

                android:horizontalSpacing="0dp"
                android:scrollbarAlwaysDrawHorizontalTrack="true"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                android:scrollbars="horizontal"
                android:stretchMode="none"
                android:verticalSpacing="0dp"
                android:listSelector="@null"
                android:scaleType="centerCrop">

            </com.j4nos.moviebuffs12.CustomGridView>

        </LinearLayout>

    </HorizontalScrollView>

</RelativeLayout>
public class CustomGridView extends GridView {

    public CustomGridView(Context context) {
        super(context);
    }

    public CustomGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /* ADD THIS */
    @Override
    public int computeVerticalScrollOffset() {
        return super.computeVerticalScrollOffset();
    }
}

1 个答案:

答案 0 :(得分:5)

我认为问题在于你滚动的方式:

gridview.scrollBy(0, 44);

这会移动gridview,而不是gridview的内容。您可以通过在CustomGridView组件中的布局中添加以下内容来验证这一点:

android:fastScrollAlwaysVisible="true"

现在您可以看到它不是按下按钮时与之交互的gridview内容。

相反,我建议您尝试使用:

gridview.scrollListBy(44);

如果您的API级别允许它。