OnScrollListener not working inside custom ListVie

2019-08-17 17:02发布

I need to catch scrolling events inside my custom ListView Adapter, so I have implemented OnScrollListener in it, but its methods are not being called. I was looking everywhere for an answer but without any luck, no errors no exceptions.

What could be wrong?

ListView Adapter:

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条回答
▲ chillily
2楼-- · 2019-08-17 17:18

You are not calling setOnScrollListener(this) anywhere. Do it in the constructor and it should work fine :-)

Somewhere in your code you probably have something like this:

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

Add

someListView.setOnScrollListener(adapter);
查看更多
来,给爷笑一个
3楼-- · 2019-08-17 17:21

You can implement your activity or fragment where your listview is used with 'OnScrollListener' and then just set listview like this:

 listview.setOnScrollListener(this); 
查看更多
登录 后发表回答