2016-08-21 7 views
12

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

अपने एपीआई स्तर यह अनुमति देता है।

संबंधित मुद्दे