2015-10-07 100 views
2

我有我的GridView的問題。它不是滾動到GridView的底部。我使用了Android developer網站的GridView。下面是當我向下滾動時,屏幕底部的樣子: 因此,它將切斷最後兩個網格的底部。不過我已經將填充添加到我的GridView的底部,看起來像這樣解決了這個:GridView不能滾動到底部探測

<?xml version="1.0" encoding="utf-8"?> 
<GridView 
    android:id="@+id/gridview" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="90dp" 
    android:gravity="center" 
    android:horizontalSpacing="2dp" 
    android:numColumns="2" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="1dp" 
    android:paddingBottom="55dp" 
    /> 

的問題是55dp的填充完全符合我的三星Galaxy S5。但我懷疑這不是其他設備的情況?如果我添加更多的填充,我將在屏幕底部獲得永久的白線,即使GridView沒有完全滾動。

這裏是我實現的GridView類:

public class HomeFragment extends Fragment implements AdapterView.OnItemClickListener { 

    public HomeFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.grid_layout, container, false); 
     GridView gridview = (GridView) view.findViewById(R.id.gridview); 
     gridview.setAdapter(new GridViewAdapter(getActivity())); 
     gridview.setOnItemClickListener(this); 
     // Inflate the layout for this fragment 
     return view; 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Toast.makeText(getContext(), "" + position, 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

這是我GridViewAdapter類:

public class GridViewAdapter extends BaseAdapter { 
    private Context mContext; 

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

    public int getCount() { 
     return mThumbIds.length; 
    } 

    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; 
     DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); 
     int screenWidth = metrics.widthPixels; 
     if (convertView == null) { 
      // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(screenWidth/2, screenWidth/2)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      //imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.dog, R.drawable.cat, 
      R.drawable.horse, R.drawable.bunny, 
      R.drawable.fish, R.drawable.hamster, 
      R.drawable.hedgehog, R.drawable.bird 
    }; 
} 

所以我在短的問題是:有沒有一種方法,以確保在GridView會無需將填充添加到GridView底部即可完美地適應屏幕,我認爲這對所有設備來說都不是相同的填充?

回答

0

如果使用高於20的SDK,您可以使用

int yourNavigationBarHeight; 
Resources resources = context.getResources(); 
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); 
if(resourceId > 0) 
    yourNavigationBarHeight = resources.getDimensionPixelSize(resourceId); 
else 
    yourNavigationBarHeight = 0;