为什么我的GridView在The End截止?

时间:2018-08-20 12:20:06

标签: java android android-gridview android-gridlayout

Screenshoot of the cropped end of the GridView

在上图中,底部的项目像红色箭头一样被切除。 也许Gridview的大小是整个屏幕,并且NavigationBottomView似乎覆盖了GridView的一部分。我想指定从屏幕顶部到BottomNavigationView顶部的GridView的高度。

下面是我的xml代码。

1>具有NavigationBottomView(home.xml)的主屏幕xml代码

JTree

2>使用GridView(site_list.xml)的Xml代码

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".home">
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="520dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="32dp"
            android:layout_gravity="left|center_vertical"
            android:text="All Of "
            android:textColor="@color/colorPrimaryDark"
            android:textSize="60sp"
            android:textStyle="bold"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="196dp"
            android:layout_centerInParent="true"
            android:layout_gravity="left|center_vertical"
            android:text="G"
            android:textColor="#8e1414"
            android:textSize="60dp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="240dp"
            android:layout_gravity="left|center_vertical"
            android:text="IST"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="60dp"
            android:textStyle="bold"/>
    </FrameLayout>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>
</RelativeLayout>

3>在GridView(list_item.xml)中设计项目的xml代码

<?xml version="1.0" encoding="utf-8"?>
<GridView 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#FFFFFF"
    tools:context=".home"
    android:id="@+id/gridView"
    android:columnWidth="96dp"
    android:numColumns="2"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center">
</GridView>

4>创建名为Site(Site.java)的数据类型的类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    >
    <ImageView
        android:layout_width="144dp"
        android:layout_height="144dp"
        android:id="@+id/image"
        tools:src="@mipmap/ic_launcher"
        android:layout_gravity="center_vertical"
        />
    <TextView
        android:layout_width="144dp"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        tools:text="Gistsite"
        android:textAppearance="?android:textAppearanceMedium"
        android:textStyle="bold"
        android:gravity="center_horizontal"
        android:textSize="16sp"
        />
</LinearLayout>

5>通过自定义ArrayAdapter(SiteAdapter.java)创建的SideAdapter代码

public class Site {
    private String msite_name;
    private String msite_url;
    private int msite_imagesource;

    public Site(String name, String url, int imagesource){
        msite_name = name;
        msite_url = url;
        msite_imagesource = imagesource;
    }

    public String getMsite_name() {
        return this.msite_name;
    }
    public String getMsite_url(){
        return this.msite_url;
    }
    public int getMsite_imagesource(){
        return this.msite_imagesource;
    }
}

6>一个片段代码,指示上面所附的捕获屏幕(OrganizationFragment.java)

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class SiteAdapter extends ArrayAdapter<Site> {
    private Site currentSite;

    public SiteAdapter(Activity context, ArrayList<Site> sites){
        super(context,0,sites);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if(listItemView == null){
            listItemView = 
 LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }
        currentSite = getItem(position);

    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
        imageView.setImageResource(currentSite.getMsite_imagesource());

        TextView name = (TextView) listItemView.findViewById(R.id.name);
        name.setText(currentSite.getMsite_name());

        return listItemView;
    } 
}

7>代表主主屏幕(home.java)的Java代码

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;

public class OrganizationFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View organizationView = inflater.inflate(R.layout.site_list, null);
        final ArrayList<Site> organizations = new ArrayList<Site>();
        organizations.add(new Site("Gist 총학생회", "https://www.facebook.com/gistunion/",R.drawable.gistunion));
        organizations.add(new Site("Gist 동아리연합회", "https://www.facebook.com/gistclubunite/",R.drawable.clubnight));
        organizations.add(new Site("Gist 하우스", "https://www.facebook.com/GISTcollegeHOUSE/",R.drawable.gisthouse));
        organizations.add(new Site("Gist 문화행사위원회", "https://www.facebook.com/Moonhangwe/",R.drawable.moonhangwe));
        organizations.add(new Site("Gist 신문", "https://www.facebook.com/pg/GistSinmoon/posts/",R.drawable.gistnews));
        organizations.add(new Site("Gist 홍보대사", "http://blog.naver.com/PostList.nhn?blogId=gist1993&from=postList&categoryNo=28",R.drawable.gionnare));

        SiteAdapter organizationAdapter = new SiteAdapter(getActivity(), organizations);
        GridView gridViewO = 
(GridView)organizationView.findViewById(R.id.gridView);
        gridViewO.setAdapter(organizationAdapter);
     gridViewO.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     {
            @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Site site = organizations.get(position);
                    openWebPage(site.getMsite_url());
                }
        });
        return organizationView;
    }
    public void openWebPage(String url){
        Uri webpage = Uri.parse(url);
        Intent siteMove = new Intent(Intent.ACTION_VIEW, webpage);
        if(siteMove.resolveActivity(getActivity().getPackageManager()) != null)
            startActivity(siteMove);
    }
}

如果您能阅读我提出的问题并给他们一些建议,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

发生这种情况是因为您已在xml中设置了FrameLayout高度520dp

设置frameLayout高度匹配,还使用android:layout_above="@+id/navigation"

尝试此代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       android:layout_above="@+id/navigation"
        android:orientation="horizontal">
       <OtherViews.../>
    </FrameLayout>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>
</RelativeLayout>