我的OnScrollListener没有被调用,缺少什么代码?

时间:2013-11-09 13:53:27

标签: android android-listfragment

我的onScrollListener似乎永远不会被调用。我接下来的几个教程并在我的ArrayAdapter中嵌入了onscrolllistener。但是它没有记录事件。

我遵循这种粗略的格式,这对他/她和他的评论者的建议,但我无法得到它。

缺少什么代码?

  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) {

}

}

1 个答案:

答案 0 :(得分:0)

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

希望这有帮助。