简体   繁体   中英

method onFocusChangeListener called only once

I am developing sample app to test the OnFocusChangeListener , I kept debug point on it's method public void onFocusChange(View v, boolean hasFocus) and I observed that the method is invoked only once ie after launching the app, up to this it's fine but when I scrolled down to the next list item, the item was focused but method isn't invoked. I know I am missing some little one. can you please help me to get rid off this issue. Thanks in advance.

Note The scrolling is done by using Remote control's D-Pad

java class:

package com.example.listview;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListActivity extends Activity implements OnFocusChangeListener{
    String[] items={"iOS","android","Symbian",};
    //GridView lst;
    ListView lst;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        lst=(ListView)findViewById(R.id.listView1);

        lst.setFocusableInTouchMode(false);

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,items);
        lst.setAdapter(adapter);


        }



    public void onFocusChange(View v, boolean hasFocus) {

        Toast.makeText(getBaseContext(), "FOCUSED", Toast.LENGTH_LONG).show();      
    }
}

Focus is called when you use tracking ball or remote to make it highlight (Without touching. An example could be in an android TV where you are navigating with left,up,down,right button.)

You need an onItemSelected for selection. onItemClickListener for click.

I found the way to solve the issue but still I don't know why the OnFocuschangeListener was not working? I solved it by implementing OnItemSelectedListener

package com.example.listview;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListActivity extends Activity implements OnItemSelectedListener{
    String[] items={"iOS","android","Symbian",};
    //GridView lst;
    ListView lst;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        lst=(ListView)findViewById(R.id.listView1);

        lst.setFocusableInTouchMode(false);

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,items);
        lst.setAdapter(adapter);


        }



    @Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM