OnScrollListener无法在自定义ListView适配器中工作

时间:2015-04-16 07:26:02

标签: android listview methods callback onscrolllistener

我需要在我的自定义ListView适配器中捕获滚动事件,所以我在其中实现了OnScrollListener,但是它的方法没有被调用。我到处寻找答案,但没有运气,没有错误没有例外。

可能出现什么问题?

ListView适配器:

import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner; 


    public class PhotosListViewAdapter extends ArrayAdapter<ImageItemsSetter> implements OnScrollListener {

        DeleteImageListener dListener;
        private Context context;
        private int layoutResourceId;
        private ArrayList<ImageItemsSetter> data = new ArrayList<ImageItemsSetter>();   

        //standard constructor
        public PhotosListViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItemsSetter> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;            
        }           

        static class ViewHolder {

            public ImageView Img; 
            public EditText quantity;
            public Spinner spinner;
            public Button delete;

        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder = null;            

            ImageItemsSetter image = data.get(position);

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder(); 

            holder.Img = (ImageView) row.findViewById(R.id.Img);

            holder.quantity = (EditText) row.findViewById(R.id.quantity);
            // holder.quantity.addTextChangedListener(new GenericTextWatcher(holder.quantity));           

            holder.spinner = (Spinner) row.findViewById(R.id.photo_format);
            holder.delete = (Button) row.findViewById(R.id.deleteImage);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag(); 
        }           

         holder.Img.setImageBitmap(image.getTheImage());            
         holder.quantity.setText(image.getQuantity());


        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context,
                R.array.formats_array, android.R.layout.simple_spinner_item);       
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
        holder.spinner.setAdapter(adapter);

        //The delete button
        holder.delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {           

                if (dListener != null) {  
                    dListener.onDeletePressed(position); 
                }       
            }           
            });      

        /*
        holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {     

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {                         
                dListener.onFormatChanged(parent.getItemAtPosition(pos).toString(), data.get(position).getName());              
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub              
            }

        });

        holder.quantity.addTextChangedListener(new TextWatcher() {

               @Override
               public void afterTextChanged(Editable s) { }

               @Override    
               public void beforeTextChanged(CharSequence s, int start,
                 int count, int after) {  }

               @Override    
               public void onTextChanged(CharSequence s, int start,
                 int before, int count) {
                   dListener.onQuantityChanged(s.toString(), data.get(position).getName());
                }
              });

        */

        return row;
        }




        //Interface to send selected image's position for deletion
        public void setDeleteImageListener(DeleteImageListener listener) {  
            this.dListener = listener;  
        } 

        public static interface DeleteImageListener { 
            public void onDeletePressed(int position);
            public void onQuantityChanged(String quantity, String name);
            public void onFormatChanged(String format, String name);
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            System.out.println("state changed");
        }


        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            System.out.println("scroleld");
        }               





    }

2 个答案:

答案 0 :(得分:2)

您没有在任何地方致电setOnScrollListener(this)。在构造函数中执行它应该可以正常工作: - )

在代码中的某处,您可能会遇到以下情况:

PhotosListViewAdapter adapter = new PhotosListViewAdapter();
someListView.setAdapter(adapter);

添加

someListView.setOnScrollListener(adapter);

答案 1 :(得分:1)

您可以使用&#39; OnScrollListener&#39;来实现您的列表视图所使用的活动或片段。然后像这样设置listview:

 listview.setOnScrollListener(this);