添加/删除页眉和页脚动态 [英] Add / Remove header and footer dynamically

查看:185
本文介绍了添加/删除页眉和页脚动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够从我的的ListView 动态添加和删除页眉和页脚。

所以,我初始化我的活动我的页眉和页脚,那么在某些时候,我想隐藏起来,后来我需要添加previous页眉和页脚,并保持相同的适配器

所以,我发现这个解决办法,但它的丑陋,我真的希望有一个别的办法。
基本上,我有设置一个空适配器可以添加标题视图,然后设置一个空的适配器添加页脚视图。要完成我把我的真正的适配器。

编辑:我要补充一点,使用的可见性属性(GONE&安培;可见)不是这里的解决方案,因为头和放大器;页脚观点不能在适配器我的中间过程中。

 公共类TestAdapterHeader扩展ListActivity实现OnClickListener {
        私有静态的String []项目= {测试1,测试2,实验3,测试4,
                试验5,试验6,试验7,试验8,试验9,10试验,
                检验11,12试验,检验13,14试验,检验15,16试验,
                检验17,18试验,检验19,20试验};

        私人ArrayAdapter mAdapter;
        私人的LinearLayout mParentView;
        私人TextView的mHeaderView,mFooterView;

        私人布尔mViewsHidden = FALSE;


        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);

            initViews();

            mAdapter =新的ArrayAdapter<字符串>(这一点,
                    android.R.layout.simple_list_item_1,项目);
            setListAdapter(mAdapter);
        }


        私人无效initViews(){
            //主要布局
            mParentView =新的LinearLayout(本);
            mParentView.setOrientation(LinearLayout.VERTICAL);
            mParentView.setBackgroundColor(Color.BLACK);

            //按钮隐藏意见
            按钮hideViewsButton =新的按钮(这一点);
            hideViewsButton.setText(添加/删除意见);
            hideViewsButton.setOnClickListener(本);

            //列表视图
            ListView控件的ListView =新的ListView(本);
            listView.setId(android.R.id.list);
            listView.setCacheColorHint(Color.TRANSPARENT);

            mParentView.addView(hideViewsButton);
            mParentView.addView(ListView控件);

            //设置内容视图
            的setContentView(mParentView);

            AbsListView.LayoutParams LP =新AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,150);

            mHeaderView =新的TextView(本);
            mHeaderView.setTextColor(Color.WHITE);
            mHeaderView.setBackgroundColor(Color.BLUE);
            mHeaderView.setGravity(Gravity.CENTER);
            mHeaderView.setLayoutParams(LP);
            mHeaderView.setText(头);

            mFooterView =新的TextView(本);
            mFooterView.setTextColor(Color.WHITE);
            mFooterView.setBackgroundColor(Color.BLUE);
            mFooterView.setGravity(Gravity.CENTER);
            mFooterView.setLayoutParams(LP);
            mFooterView.setText(页脚);


            。getListView()addHeaderView(mHeaderView);
            。getListView()addFooterView(mFooterView);
        }


        @覆盖
        公共无效的onClick(视图v){
            mViewsHidden = mViewsHidden!;

            //删除页眉和放大器;页脚意见
            如果(mViewsHidden){
                。getListView()removeHeaderView(mHeaderView);
                。getListView()removeFooterView(mFooterView);
            }
            其他 {
                //删除ListAdapter才能够加入我们的headerView
                setListAdapter(空);
                。getListView()addHeaderView(mHeaderView);

                //设置空ListAdapter才能够加入我们的footerView
                setListAdapter(新ArrayAdapter<字符串>(TestAdapterHeader.this,-1));
                。getListView()addFooterView(mFooterView);

                //重新设置我们的适配器
                setListAdapter(mAdapter);
            }

            mParentView.requestLayout();

        }
    }
 

解决方案

您可以使用下面code做同样的

  //显示页脚视图
footerView.setVisibility(View.VISIBLE);

//隐藏页脚视图
footerView.setVisibility(View.GONE);
 

I need to be able to add and remove headers and footers from my ListView dynamically.

So I initialize my activity with my headers and footers, then at some point I want to hide them, and later I need to add the previous headers and footers, and keep the same Adapter.

So I found this solution, but it's ugly and I really hope that there is an other way.
Basically, I have to set a null adapter to be able to add the header view, and then set an empty adapter to add the footer view. To finish I set my real adapter.

Edit: I must add that using the visibility attribute (GONE & VISIBLE) is not a solution here, because the headers & footers views must not be in the adapter during my intermediate procedure.

    public class TestAdapterHeader extends ListActivity implements OnClickListener {
        private static String[] items = { "test 1", "test 2", "test 3", "test 4",
                "test 5", "test 6", "test 7", "test 8", "test 9", "test 10",
                "test 11", "test 12", "test 13", "test 14", "test 15", "test 16",
                "test 17", "test 18", "test 19", "test 20" };

        private ArrayAdapter mAdapter;
        private LinearLayout mParentView;
        private TextView mHeaderView, mFooterView;

        private boolean mViewsHidden = false;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            initViews();

            mAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items);
            setListAdapter(mAdapter);
        }


        private void initViews() {
            // The main layout
            mParentView = new LinearLayout(this);
            mParentView.setOrientation(LinearLayout.VERTICAL);
            mParentView.setBackgroundColor(Color.BLACK);

            // The button to hide the views
            Button hideViewsButton = new Button(this);
            hideViewsButton.setText("Add/Remove views");
            hideViewsButton.setOnClickListener(this);

            // The listview
            ListView listView = new ListView(this);
            listView.setId(android.R.id.list);
            listView.setCacheColorHint(Color.TRANSPARENT);

            mParentView.addView(hideViewsButton);
            mParentView.addView(listView);

            // Set the content view
            setContentView(mParentView);

            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 150);

            mHeaderView = new TextView(this);
            mHeaderView.setTextColor(Color.WHITE);
            mHeaderView.setBackgroundColor(Color.BLUE);
            mHeaderView.setGravity(Gravity.CENTER);
            mHeaderView.setLayoutParams(lp);
            mHeaderView.setText("Header");

            mFooterView = new TextView(this);
            mFooterView.setTextColor(Color.WHITE);
            mFooterView.setBackgroundColor(Color.BLUE);
            mFooterView.setGravity(Gravity.CENTER);
            mFooterView.setLayoutParams(lp);
            mFooterView.setText("Footer");


            getListView().addHeaderView(mHeaderView);
            getListView().addFooterView(mFooterView);
        }


        @Override
        public void onClick(View v) {
            mViewsHidden = !mViewsHidden;

            // Remove header & footer views
            if (mViewsHidden) {
                getListView().removeHeaderView(mHeaderView);
                getListView().removeFooterView(mFooterView);
            } 
            else {
                // Remove the ListAdapter to be able to add our headerView
                setListAdapter(null);
                getListView().addHeaderView(mHeaderView);

                // Set an empty ListAdapter to be able to add our footerView
                setListAdapter(new ArrayAdapter<String>(TestAdapterHeader.this, -1));
                getListView().addFooterView(mFooterView);

                // Re set our Adapter
                setListAdapter(mAdapter);
            }

            mParentView.requestLayout();

        }
    }

解决方案

You can use below code to do the same

// to show the footer view
footerView.setVisibility(View.VISIBLE); 

// to hide the footer view    
footerView.setVisibility(View.GONE); 

这篇关于添加/删除页眉和页脚动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆