0
我的onScrollListener似乎永遠不會被調用。我看了其他幾個教程,並在我的ArrayAdapter中嵌入了onscrolllistener。但它不記錄事件。我的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) {
}
}
感謝你我基本意識到這一點,但我想知道在代碼中我究竟在哪裏實現該方法(listview.setOnscrollListe ...),因爲我不明白處理任何地方的listview ...再次感謝。我嘗試了創建listfragment的地方,但沒有這樣的運氣。 – bungleofsketches
所以我也試着實現'ListView list =(ListView)findViewById(android.R.id.list); list.setOnScrollListener(this);'在上面的適配器構造函數中,它找不到方法'findViewById'(它的拼寫正確等) – bungleofsketches
Dude ..不要在你的適配器中找到listview。如果你使用的是listfragment那麼那裏是一個名爲getListView()的方法。這將返回listview對象,並且您可以按照上面的回答中所述在該對象上設置onscrolllistener。 –