Android:无法同时设置:setOnItemClickListener setOnScrollListener?

时间:2011-10-31 20:13:40

标签: android onitemclicklistener

class MyActivity extends Activity implements OnScrollListener,  OnItemClickListener {
...
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);  // why not work with previous line?
...

添加ListView.setOnScrollListener(this)后

onItemClick(...)不再触发。

有人知道为什么吗?

感谢。

2 个答案:

答案 0 :(得分:2)

我不明白为什么它不起作用,

试试这个:

  getListView().setOnScrollListener(onAnswersScrolled);
  getListView().setOnItemClickListener(onAnswerClicked);

private OnItemClickListener onAnswerClicked = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // 
                Intent i = new Intent();

                setResult(RESULT_OK, i);
                finish();
        }

    };

    private OnScrollListener onAnswersScrolled = new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // not used
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // do something
            }
        }
    };

source

答案 1 :(得分:0)

试试这个。这是我显示自定义listView的代码。这个对我有用。

public class MyActivity extends ListActivity implements OnScrollListener  {

MyAdapter adapter= new MyAdapter(this);

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(adapter); 
    getListView().setOnItemClickListener(this);
    getListView().setOnScrollListener(this);
}

public class MyAdapter extends BaseAdapter{

    private LayoutInflater mInflater;
    ViewHolder holder = null;

    public MyAdapter(Context context){
        this.mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /**Original holder
         * 
         * 
         * */
        //ViewHolder holder = null;
        if (convertView == null) {

            holder=new ViewHolder();  

        }else {

            holder = (ViewHolder)convertView.getTag();
        }

        return convertView;
    }

    class myButtonListener implements OnClickListener {
        private int position;


        myButtonListener(int pos) {
            position = pos;
        }

        @Override
        public void onClick(View v) {
            int vid=v.getId();

        }
    }

}
希望它有所帮助。