2

这是活动

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

4 回答 4

9

列表视图和按钮正在争夺焦点,而列表视图正在获胜。

在页脚上设置以下内容。false告诉页脚它是不可选择的:

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

或者,您可以使用OnClickListenerFireDevil 在此处建议的方法。

于 2013-02-26T14:09:31.887 回答
8

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

只需将 a 设置OnClickListener为应用View

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

解决它的非常简单的事情!

于 2013-09-04T20:59:16.387 回答
2

我建议您使用以下提示:

  1. 制作一个相对布局作为你的根布局
  2. 将您的页脚布局和您的 ListView 放在上面
  3. 在您的 ListView 中添加此属性:

    android:layout_above="@id/your_footer_layout_id"
    

有关更多详细信息,请参阅链接。您可以通过以下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;
    }
}
于 2012-05-07T06:44:23.193 回答
1

将页脚视图的根更改为使用<merge />而不是LinearLayout. Viewa in a的父级ListView必须是 类型AbsListView。然后,在代码中,你会像这样膨胀你的页脚View

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

编辑

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

于 2012-05-07T05:45:37.590 回答