0

我的 onScrollListener 似乎永远不会被调用。我遵循了其他几个教程并将 onscrolllistener 嵌入到我的 ArrayAdapter 中。但是它没有记录事件。

我遵循了对他/她有用的粗略格式以及他的评论者的建议,但我无法理解。

缺少什么代码?

  package com.example.android.navigationdrawerexample;

  import android.content.Context;
  import android.graphics.Typeface;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AbsListView;
  import android.widget.AbsListView.OnScrollListener;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;

  import com.nostra13.universalimageloader.core.ImageLoader;
  import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

  import java.util.ArrayList;

  public class ArticleAdapter extends ArrayAdapter<Article> implements OnScrollListener{

// declaring our ArrayList of items
private ArrayList<Article> objects;

/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public ArticleAdapter(Context context, int textViewResourceId, ArrayList<Article> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;


}

public View getView(int position, View convertView, ViewGroup parent){

    // assign the view we are converting to a local variable
    View v = convertView;

    // first check to see if the view is null. if so, we have to inflate it.
    // to inflate it basically means to render, or show, the view.
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.article_button, parent, false);
    }

    /*
     *   i refers to the current Item object.
     */
    Article i = objects.get(position);

    if (i != null) {

        TextView heading = (TextView) v.findViewById(R.id.heading);
        // check to see if each individual textview is null.
        // if not, assign some text!
        if (heading != null){
            heading.setText(i.getHeading());
        }

        ImageView featuredimage = (ImageView) v.findViewById(R.id.featuredimage);
        String imageUrl = i.getImageurl();
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this.getContext()));
        imageLoader.displayImage(imageUrl, featuredimage);

        Typeface museo = Typeface.createFromAsset(getContext().getAssets(),"fonts/Museo_Slab_500.otf");
        heading.setTypeface(museo);
        heading.setTextSize(25);
    }
    // the view must be returned to our activity
    return v;
}

public void onScroll(AbsListView arg0, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    Log.v("entered onScroll", " " + firstVisibleItem + visibleItemCount
            + totalItemCount);
    if (((firstVisibleItem + visibleItemCount) >= totalItemCount - 1)) {
        Log.v("entered if", " " + firstVisibleItem + visibleItemCount
                + totalItemCount);
        // if we're at the bottom of the listview, load more data
        addData(totalItemCount, 10);
    }
}

private void addData(int totalItemCount, int productId) {

    Toast.makeText(getContext(), "last item", Toast.LENGTH_SHORT).show();
}

public void onScrollStateChanged(AbsListView arg0, int arg1) {

}

}
4

1 回答 1

0

这是因为你刚刚实现了接口,并且没有设置监听器。您需要通过说 listview.setOnScrollListener(this) 来设置监听器。我的建议是创建一个单独的类来实现 onscrolllisetner 并且将类对象传递给 listview.setonscrolllisetener(new MyScrollListener());

希望这可以帮助。

于 2013-11-09T14:14:20.667 回答