开发者

how to put headerview in the gridview

I have to put header view on top of grid view like list view. Although I know it doesn't have support for that. But is there any way to have it. Just开发者_运维百科 wanted to know if anyone has done work on it ?


You always have a workaround like having textview for header and footer with gridview in between

in case of listview the answer will be like this :

You can of course put arbitray elements around your ListView. For example you can define a layout with two TextViews and a ListView between them. If you do this, you must assign the id "@android:id/list" to the ListView, as the ListActivity searches for a view with this id. If you do this then one TextView will always be visible above the List (header) and the other will be visible below the ListView. If you want to display the header / footer view only if see the beginning / end of the list you can use view.setHeaderView() or view.setFooterView(). For example:

public class MyList extends ListActivity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                "Ubuntu", "Solaris", "Android", "iPhone", "Linux", "Windows7",
                "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
        View header = getLayoutInflater().inflate(R.layout.header, null);
        View footer = getLayoutInflater().inflate(R.layout.footer, null);
        ListView listView = getListView();
        listView.addHeaderView(header);
        listView.addFooterView(footer);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, names));

    }
}

for case of gridview you need to do your own layout that will have -textview on the top -gridview -textview on the bottom and that it the concept is the same

you just do not have to say listView.addHeaderView(header); cause you will make that effect with the design on the layout

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜