简体   繁体   English

需要在Android中为Gridview创建Footer

[英]Need to create Footer for Gridview in Android

See below layout. 见下面的布局。 I am unable to put footer button below a GridView . 我无法在GridView下面放置页脚按钮。 Any help will be appreciate. 任何帮助将不胜感激。

When Gridview fill in the screen Button is not displayed. 当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>

I have been keeping searching for a long time for a GridView which allows us to addFooterView and addHeaderView like ListView . 我一直在寻找一个GridView,它允许我们像ListView一样添加addFooterViewaddHeaderView

It's a post 2 years ago. 这是2年前的帖子。 I come here by following the link in google search result. 我是按照谷歌搜索结果中的链接来到这里的。 I don't know weather this problem has been solved or not. 我不知道天气这个问题是否已经解决。

If not, here is a library which may be helpful. 如果没有,这是一个可能有用的图书馆。 https://github.com/liaohuqiu/android-GridViewWithHeaderAndFooter . https://github.com/liaohuqiu/android-GridViewWithHeaderAndFooter

It's very simple: 这很简单:

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

Here is a screen snapshot: 这是一个屏幕快照:

屏幕截图

Hope this would be helpful. 希望这会有所帮助。 Good luck! 祝好运!

<?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>  

Works fine for me 对我来说很好

Add to GridView two attributes: 添加到GridView两个属性:

  • android:paddingBottom of the size as your Button; android:paddingBottom的大小为你的Button;
  • android:clipToPadding="false" 机器人:clipToPadding = “假”

This will make some space for button at the bottom of the grid. 这将为网格底部的按钮腾出一些空间。

I managed to add footer to my gridview with a little trick. 我设法通过一个小技巧为我的gridview添加页脚。 Lets say your adapters main data is List of MyDataInfo objects list, now when you passed all the data to adapter you can add two empty( null ) objects and check in getView whether data is null or not and inflate desired view . 让我们说你的适配器主数据是List of MyDataInfo对象列表,现在当你将所有数据传递给适配器时,你可以添加两个空( null )对象并检查getView是否数据是否为null并且膨胀所需的视图。

// 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;
    }

} }

Before this in your main activity where you set adapter you'll have something like this 在你设置适配器的主要活动之前,你会有这样的东西

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

And important thing you'll have to handle onitemclick 重要的是你必须处理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;
    }

Hope this helps ^^ 希望这有助于^^

Try adding the button first and putting the grid view on top of it. 首先尝试添加按钮并将网格视图放在其上。 Something like this ought to work: 这样的事情应该有效:

<?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>

EDIT 编辑

Based on your comments, I can see why the above does not work. 根据您的意见,我可以看出上述原因无效。 Assuming you mean that you want the button to appear only after you scroll to the bottom (and not be on the screen the entire time), here's the layout I recommend: 假设你的意思是你只想在滚动到底部之后才显示按钮(而不是整个时间都在屏幕上),这是我推荐的布局:

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

Use the same as the below: 使用与下面相同:

Edited: 编辑:

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

and Call the onScroll method to hide the same as you want...........this implementation for LOAD MORE functionality in GridView.............It works for me. 并调用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);
                    }
                }
            }
        });

check if this helps 检查这是否有帮助

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM