2016-08-21 3 views
12

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(); 
    } 
} 

ответ

5

Я думаю, что проблема с тем, как вы прокруткой:

gridview.scrollBy(0, 44); 

Это перемещает представление сетки, а не содержимое сетки. Вы можете проверить это, добавив следующее в макет внутри CustomGridView компонента:

android:fastScrollAlwaysVisible="true" 

Теперь вы можете видеть, что это не GridView содержания, которое в настоящее время взаимодействовало с на кнопках.

Вместо этого, я предлагаю вам попробовать использовать:

gridview.scrollListBy(44); 

, если ваш уровень API позволяет.

Смежные вопросы