【问题标题】:Need to create Footer for Gridview in Android需要在 Android 中为 Gridview 创建页脚
【发布时间】:2017-08-28 04:41:47
【问题描述】:

请参见下面的布局。我无法将页脚按钮放在GridView 下方。 任何帮助将不胜感激。

当Gridview填入屏幕时按钮不显示。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 >

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/gridview"
    android:text="Load More Images" />

 </RelativeLayout>

【问题讨论】:

标签: android layout gridview


【解决方案1】:

我一直在寻找一个 GridView,它允许我们 addFooterViewaddHeaderView 喜欢 ListView

这是 2 年前的帖子。我通过谷歌搜索结果中的链接来到这里。我不知道这个问题是否已经解决了。

如果没有,这里有一个可能有用的库。 https://github.com/liaohuqiu/android-GridViewWithHeaderAndFooter.

很简单:

GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);

LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);

这是屏幕截图:

希望这会有所帮助。祝你好运!

【讨论】:

    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    >  
    
     <GridView android:layout_height="match_parent" android:id="@+id/all_workouts_list" 
      android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"  android:cacheColorHint="#ffffffff" 
        android:layout_width="match_parent" 
       android:layout_above="@+id/add_workout_all_workout_list"></GridView>
    
    <Button android:layout_width="wrap_content"      
     android:id="@+id/add_workout_all_workout_list" android:layout_height="wrap_content" 
     android:text="Add Workout" android:layout_alignParentBottom="true"  
     android:layout_alignParentLeft="true"></Button>
    
    </RelativeLayout>  
    

    适合我

    【讨论】:

    • NOt listview 我想在 gridview 项目之后添加页脚。
    • 抱歉忘了更改,但它适用于列表和网格视图检查更新的代码
    【解决方案3】:

    我设法通过一个小技巧将页脚添加到我的网格视图中。假设您的适配器主要数据是 MyDataInfo 对象列表,现在当您将所有数据传递给适配器时,您可以添加两个空(null) 对象并在 getView 中检查数据是否为空并膨胀所需的视图。

    // in the adapter
    public void addVoid(YourInfo info) {
        dataList.add(info);
        dataList.add(info);
        notifyDataSetChanged();
    }
    public View getView(int position, View convertView, ViewGroup parent) {
    if (dataList.get(position) !=null) {
        View vi = convertView;
        ViewHolder holder;
        if (convertView == null || convertView.getTag().equals("stop")) {
            //inflate your gridview item
        }
        else {
            holder = (ViewHolder) vi.getTag();
        }
    
    
        // provide data to views
    
        return vi;
        }
        else {
            View v;
            if (position == dataList.size() - 2) {
                v = inflater.inflate(R.layout.show_more, null); // load more view
                v.setTag("stop");
            }
            else {
            v = inflater.inflate(R.layout.progress_bar, null); // progress bar
                v.setTag("stop");
            }
            return v;
        }
    

    }

    在此之前,在您设置适配器的主要活动中,您将拥有类似的内容

    YourAdapter adap = new YourAdapter(this, dataList);
    gridView.setAdapter(adap);
    YourInfo info = null;
    adapter.addVoid(info);
    

    重要的事情你必须处理 onitemclick

    int size = adapter.getCount();
    public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
        if (position == size - 2) {  // user selected SHOW MORE view
             int wantedPosition = size - 1;
             int firstPosition = grid.getFirstVisiblePosition();
         int wantedChild = wantedPosition- firstPosition;
             ProgressBar progBar = grid.getChildAt(wantedChild);
             progBar.setVisibility(View.Visible);
             return;
             }
        if (position == size - 1) // user selected progress bar 
             return;
        }
    

    希望对你有所帮助^^

    【讨论】:

      【解决方案4】:

      给GridView添加两个属性:

      • android:paddingBottom 的大小作为你的Button;
      • android:clipToPadding="false"

      这将为网格底部的按钮腾出一些空间。

      【讨论】:

        【解决方案5】:

        跟下面的一样使用:

        已编辑:

        <FrameLayout android:id="@+id/linear_Layout_List"
                android:layout_gravity="center_horizontal|top|bottom"
                android:layout_marginTop="90dip" android:orientation="vertical"
                android:layout_width="fill_parent" android:layout_height="fill_parent"
                android:layout_marginBottom="60dip" android:background="#E4E4E4">
                <GridView android:layout_width="fill_parent"
                    android:layout_gravity="center_horizontal|top|bottom"
                    android:columnWidth="90dp" android:numColumns="3"
                    android:horizontalSpacing="10dp" android:stretchMode="columnWidth"
                    android:gravity="center" android:id="@+id/Search_Result_Grid"
                    android:background="#E4E4E4" android:focusable="true"
                    android:verticalSpacing="10dp" android:layout_height="wrap_content"
                    android:layout_marginBottom="45dip">
                </GridView>
                <Button android:id="@+id/txt_LoadMoreGrid"
                    android:layout_width="fill_parent" android:layout_gravity="center_horizontal|bottom"
                    android:layout_height="wrap_content" android:text="Load More...."
                    android:clickable="true" android:textColor="#ffffff"
                    android:visibility="invisible" android:background="@drawable/top_bar_bg"></Button>
            </FrameLayout>
        

        并调用 onScroll 方法来隐藏你想要的一样............这个在 GridView 中 LOAD MORE 功能的实现......它适用于我。

        grid.setOnScrollListener(new OnScrollListener() {
        
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {
        
        
                    }
        
                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        
                        System.out.println("The first visible Item:"+firstVisibleItem);
                        if(width<=320 && height<=480)
                        {
                        if(firstVisibleItem+5==totalItemCount-1)
                        {
                            /*FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.NO_GRAVITY);
                                layoutParams.setMargins(0, 0, 0, 45);
                                grid.setLayoutParams(layoutParams);*/
                            tv_Load.setVisibility(tv_Load.VISIBLE);
                        }
                        else
                        {
                            /* FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.NO_GRAVITY);
                                layoutParams.setMargins(0, 0, 0, 0);
                                grid.setLayoutParams(layoutParams);*/
                                tv_Load.setVisibility(tv_Load.GONE);
                        }
                        }
                        else
                        {
                            if(firstVisibleItem+8==totalItemCount-1)
                            {
                                /*FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.NO_GRAVITY);
                                    layoutParams.setMargins(0, 0, 0, 45);
                                    grid.setLayoutParams(layoutParams);*/
                                tv_Load.setVisibility(tv_Load.VISIBLE);
                            }
                            else
                            {
                                /* FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.NO_GRAVITY);
                                    layoutParams.setMargins(0, 0, 0, 0);
                                    grid.setLayoutParams(layoutParams);*/
                                    tv_Load.setVisibility(tv_Load.GONE);
                            }
                        }
                    }
                });
        

        【讨论】:

        • 实际上 Dhaval 我在 6 个月前也遇到了同样的问题,我在网上搜索了足够多的问题,但即使我也得到了与您相同的答案,但无法成功,但在我实施后失败了靠我自己并得到积极的结果,这是我的答案,如果这个答案对你不起作用,那么请自己想想,怎么做,因为 Gridview 没有任何页脚视图。
        • 您好 Sanat,您是如何获得宽度和高度值的?谢谢。
        • if(width
        【解决方案6】:

        尝试先添加按钮并将网格视图放在其顶部。像这样的东西应该可以工作:

        <?xml version="1.0" encoding="utf-8"?>
        
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
        
            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="Load More Images" />
        
            <GridView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_above="@id/button1"
                android:columnWidth="90dp"
                android:gravity="center"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp" />
        
        </RelativeLayout>
        

        编辑

        根据您的 cmets,我可以看到为什么上述方法不起作用。假设您的意思是您希望按钮仅在滚动到底部后出现(而不是一直在屏幕上),我推荐的布局如下:

        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
        
                <GridView
                    android:id="@+id/gridview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:columnWidth="90dp"
                    android:gravity="center"
                    android:horizontalSpacing="10dp"
                    android:numColumns="auto_fit"
                    android:stretchMode="columnWidth"
                    android:verticalSpacing="10dp" />
        
                <Button
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Load More Images" />
        
            </LinearLayout>
        </ScrollView>
        

        【讨论】:

        • 它是我错误添加的。查看更新的代码。我想要在 gridview 之后的按钮,如 listview 页脚。
        • @DhavalKhant - 你试过我发布的布局了吗?它应该将按钮放在底部的gridview之后。
        • @DhavalKhant - 我有点不确定你想要什么。您能否详细说明为什么这对您不起作用?
        • 您的代码运行良好,但我想要 gridview 的页脚视图,如 LIstview 页脚
        • @DhavalKhant - 让我们看看我的要求是否正确。当 GridView 很短时,按钮应该就在它的下方,按钮下方有所有额外的空间。但是当 GridView 很高时,它应该被垂直截断,并且按钮应该位于屏幕底部。对吗?
        【解决方案7】:

        检查是否有帮助

        <Button
            android:id="@+id/button1"
            style="@android:style/ButtonBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="Load More Images" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-26
          • 2018-11-12
          • 2014-08-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多