2012-05-07 46 views
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(); 
      } 
     } 
    }); 

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

+0

什么是页脚? – user370305

+0

页脚出现在列表视图的最后一个元素的下方 –

回答

2

我建议你使用以下提示:

  1. 做出相对布局作为根布局上您的页脚布局
  2. 的地方,你的ListView
  3. 在你的ListView添加此属性:

    android:layout_above="@id/your_footer_layout_id" 
    

this林k了解更多详情。您可以检查一个观点被点击通过以下方法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; 
    } 
} 
+0

我已经编辑了这个问题,请在你的页脚的xml add android:clickable =“true” –

+0

然后在你的活动中设置一个clicklistener对象 –

+0

看看 –

1

更改页脚视图的根目录以使用<merge />而不是LinearLayout。在ListView中的View的父级必须是AbsListView。然后,在代码中,你会夸大你的页脚View这样的:

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

编辑

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

+0

第二个布局使用线性布局,但不允许使用线性布局 –

+0

因此,使用我发布的方法对其进行膨胀。第二版的含义是什么? –

+0

第二个布局是main_particularcategoryallnewslistfv xml 在这个xml中,我使用线性布局,但它不能被添加到列表视图,因为listview使用的是absolutelistview布局。 –

8

还有另外一种选择和设置的页脚以下。有创造了另一个方法重载,但它并没有在文档上来(至少在我的IDE它没有),我只好在网上查询资料:

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

其中假告诉页脚其不可选。我自己测试了这个,脚注内的按钮现在对触摸作出响应。我希望这是一个可接受的答案。

否则,你将不得不使用ontouchlistener作为listview和按钮争取焦点和listview获胜。

7

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

只需设置一个OnClickListener所施加View

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

这解决了这个问题很容易的事情!

+0

我这样做了,但是将'onClickListener'设置为视图中的一个按钮,并调用jsemanue提到的函数。否则'onItemClick'就会被解雇。 – theblang

相关问题