2012-05-07 68 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

相關問題