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

时间:2012-05-07 05:14:30

标签: 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);

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

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

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

4 个答案:

答案 0 :(得分:9)

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

在页脚上设置以下内容。 false告诉页脚无法选择:

 mylistView.addFooterView(footerView, null, **false**);

或者,你可以使用OnClickListener作为FireDevil建议的地方。

答案 1 :(得分:8)

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

只需将OnClickListener设置为已应用的View

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

很容易解决它的事情!

答案 2 :(得分:2)

我建议您使用以下提示:

  1. 将相对布局设为根布局
  2. 将页脚布局和ListView
  3. 放在上面
  4. 在ListView中添加此属性:

    android:layout_above="@id/your_footer_layout_id"
    
  5. 有关详细信息,请参阅this链接。您可以通过以下onClick方法检查视图是否被点击:

    @Override
    public void onClick(View v) {
    
        switch(v.getId()){
    
        case R.id.my_footer: 
            Toast.makeText(getContext(), "footer clicked",Toast.LENGTH_LONG).show();
            break;
    
        case R.id.my_header: 
            Toast.makeText(getContext(), "header clicked", Toast.LENGTH_LONG).show();
            break;
        }
    }
    

答案 3 :(得分:1)

更改页脚视图的根目录以使用<merge />而不是LinearLayoutViewListView的父级必须为AbsListView类型。然后,在代码中,您会像这样膨胀您的页脚View

liInflater.inflate(R.layout.main_particularcategoryallnewslistfv, lv, false);

修改

<merge />部分可能是不必要的。尝试首先将膨胀方法改为上述方法。