如何启用可点击的页脚并在其上设置监听器?

Ala*_*Lai 2 android listview footer

这是活动

private View footer;
private Button btnmore;

linear = (LinearLayout) findViewById(R.id.layout_content);
    linear.setVisibility(View.VISIBLE);

    LayoutInflater liInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist, null));
linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslistfv, null));
    footer = (View)getLayoutInflater().inflate(R.layout.main_particularcategoryallnewslistfv, null);
btnmore = (Button)findViewById(R.id.btn_more); 

ListView lv = (ListView) findViewById(android.R.id.list);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.main_particularcategoryallnewslist, from, to);
    lv.addFooterView(footer); <-- how to set footer being clickable?
    lv.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

在页脚中,我有一个按钮但我设置了监听器也没有响应,我认为页脚必须启用才能点击然后才能点击按钮.

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Intent intent = new Intent(Main_ParticularCategoryAllNews.this,
                    Main_ParticularNewsDetail.class);
            bundle = new Bundle();
            bundle.putInt("newsid", newsid[arg2]);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });

    btnmore.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            if (isOnline() == true) {
                linear2.setVisibility(View.VISIBLE);
                AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
                alpha.setDuration(15);
                alpha.setFillAfter(true);
                linear.startAnimation(alpha);
                btnrefresh.setVisibility(View.INVISIBLE);
                webservice.UpdateMoreCatNews(catnewsid);
                int secondsDelayed = 3;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        startActivity(new Intent(Main_ParticularCategoryAllNews.this,
                                Main_AllLatestNews.class));
                        finish();
                    }
                }, secondsDelayed * 1000);
            } else {
                toast = Toast
                        .makeText(
                                Main_ParticularCategoryAllNews.this,
                                "Your device is not connect to internet, fail to update news!",
                                Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_VERTICAL
                        | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

如何在页脚点击时设置监听器?

j2e*_*nue 9

列表视图和按钮正在争夺焦点,列表视图正在赢得胜利.

在页脚上设置以下内容.false告诉页脚它不可选:

 mylistView.addFooterView(footerView, null, **false**);
Run Code Online (Sandbox Code Playgroud)

或者,你可以OnClickListener在这里使用FireDevil.


小智 8

有一种更简单的解决方案:

只需设置一个OnClickListener应用View:

View view = inflater.inflate(R.layout.xxx, null);
    view.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            //do something
        }
    });
Run Code Online (Sandbox Code Playgroud)

很容易解决它的事情!