Alan Lai
Alan Lai

Reputation: 1104

how to enable footer being clickable and set listener on it?

This is activity

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

In the footer, i have a button but i set listener on it also no respond, i think footer must enable to be click then only can button being click.

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

how to set listener when footer being click?

Upvotes: 2

Views: 9044

Answers (4)

j2emanue
j2emanue

Reputation: 62519

The listview and button are fighting for focus and the listview is winning.

Set the following on the footer. false tells the footer it is not selectable:

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

Or, you can use an OnClickListener as FireDevil suggests here.

Upvotes: 9

K_Anas
K_Anas

Reputation: 31466

I advise you to use these tips below:

  1. make a relative layout as your root layout
  2. place on it your footer layout and your ListView
  3. add this attribute in your ListView:

    android:layout_above="@id/your_footer_layout_id"
    

See this link for more details. You can check a view is being clicked by the following onClick method:

@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;
    }
}

Upvotes: 2

FireDevil
FireDevil

Reputation: 129

There is a way easier solution:

Just set a OnClickListener to the applied View:

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

Very easy thing which solved it!

Upvotes: 8

Jason Robinson
Jason Robinson

Reputation: 31283

Change the root of your footer view to use <merge /> instead of LinearLayout. The parent of a View in a ListView must be of type AbsListView. Then, in code, you would inflate your footer View like this:

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

Edit

The <merge /> part may be unnecessary. Try just changing the inflation method first to the above.

Upvotes: 1

Related Questions