GridView无法正常滚动到底部

时间:2015-10-07 11:34:47

标签: java android xml gridview

我的GridView存在问题。它不会自动滚动到GridView的底部。我使用了Android developer网站上的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的底部,我认为它不会是所有的填充设备

1 个答案:

答案 0 :(得分:0)

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

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;