【问题标题】:How to modify preference of a FooterView dynamically?如何动态修改 FooterView 的首选项?
【发布时间】:2015-09-30 08:33:19
【问题描述】:

非常基本的问题: 我正在尝试动态启用/禁用ListViewFooterView。 基本上,只要LocationListener 获得新位置,我想通过setEnable(true/false) 功能使其可点击。我正在考虑一个从LocationListener.onLocationChange() 方法中调用的公共方法,没有返回,但它必须更改FooterView 首选项,但我不知道如何对其进行编码,以及它是否可以工作。有什么建议吗?

在 onCreate 中:

@Override
protected void onCreate(Bundle bundle) {
    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView footerView = (TextView)inflater.inflate(R.layout.footer_view, null);
    myListView.addFooterView(footerView);
    //if (!enabler) footerView.setEnabled(false);
    //else footerView.setEnabled(true);
    footerView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {}
    });
}

LocationListener 方法

@Override
public void onLocationChanged(Location currentLocation) {
    if (null == mLastLocationReading || mLastLocationReading.getTime() > currentLocation.getTime()){
        mLastLocationReading = currentLocation;
        enabler=true;
    }
    if (currentLocation.getTime() > mLastLocationReading.getTime())
        mLocationManager.removeUpdates(this);
}

因此,即使侦听器找到新位置,它也不会触发启用 footerView 的 if 条件(我在其中注释掉了代码)。 我试图从一个没有乐趣的方法访问 footerView.setEnable。任何帮助将不胜感激。

【问题讨论】:

    标签: android android-listview textview


    【解决方案1】:

    为什么您的解决方案不起作用是因为 onCreate() 仅在创建 Activity 时被调用一次。

    尝试以下方法:

    class YourActivity extends Activity implements LocationListener {
    
        private TextView footerView;
    
        @Override
        protected void onCreate(Bundle bundle) {
            LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            footerView = (TextView)inflater.inflate(R.layout.footer_view, null);
            myListView.addFooterView(footerView);
            footerView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {}
            });
        }
    
        @Override
        public void onLocationChanged(Location currentLocation) {
            if (null == mLastLocationReading || mLastLocationReading.getTime() > currentLocation.getTime()){
                mLastLocationReading = currentLocation;
                enableFooterView(true);
            }
            if (currentLocation.getTime() > mLastLocationReading.getTime())
                mLocationManager.removeUpdates(this);
        }
    
        private void enableFooterView(boolean enable) {
            footerView.setEnabled(enable);
        }
    }
    

    【讨论】:

    • 谢谢,但它不起作用,IDE 在 footerView 上给我一个错误“无法解析符号”。是不是因为变量的作用域?
    • 您介意简单解释一下我该怎么做吗?我还在学习。
    • 参见private TextView footerView; 这称为制作变量属性。
    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    相关资源
    最近更新 更多