我使用创建日期的顺序从FireBase数据库获取数据,并显示在ListView中,但首先显示的是创建的第一本书: 例如:我创建了书1,然后是book2,然后是book3,然后是book4,然后是book5,然后是book6,book7和book8。
假设在我的listView中,我正在显示添加的5本最新书籍,在onScroll listView中,我显示另外两本书:
我所拥有的:我的ListView图书显示如下:
滚动列表查看我拥有的内容:
我想要的是通过添加的最新书来显示我的书,就像这样:
在滚动列表视图上我想要什么:
片段代码(显示ListView):
listAdapter = new FeedListAdapter(getActivity(), feedItems);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(myClickListener);
ref.orderByChild("date_creation").limitToFirst(5).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
Book valueBook = dataSnap.getValue(Book.class);
keyId = dataSnap.getKey();
String titreLivreToDisplay = valueBook.getNom_livre();
String descLivreToDisplay = valueBook.getDesc_livre();
String prixLivreToDisplay = valueBook.getPrix_livre();
timeToDisplay = valueBook.getDate_creation();
valueBook.setNom_livre(titreLivreToDisplay);
valueBook.setDesc_livre(descLivreToDisplay);
valueBook.setPrix_livre(prixLivreToDisplay);
valueBook.setDate_creation(timeToDisplay);
feedItems.add(valueBook);
}
Collections.sort(feedItems, new Comparator<Book>() {
@Override
public int compare(Book book1, Book book2)
{
return book1.getDate_creation().compareTo(book2.getDate_creation());
}
});
listAdapter.notifyDataSetChanged();
}
});
代码滚动ListView:
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
private int currentVisibleItemCount;
private int currentScrollState;
private int currentFirstVisibleItem;
private int totalItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int first = view.getFirstVisiblePosition();
int count = view.getChildCount();
if (scrollState == SCROLL_STATE_IDLE || (first + count > listAdapter.getCount()) ) {
listView.invalidateViews();
}
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;
}
private void isScrollCompleted() {
if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE) {
ref.orderByChild("date_creation").startAt(timeToDisplay+1).limitToFirst(2).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
Book valueBook = dataSnap.getValue(Book.class);
keyId = dataSnap.getKey();
String titreLivreToDisplay = valueBook.getNom_livre();
String descLivreToDisplay = valueBook.getDesc_livre();
String prixLivreToDisplay = valueBook.getPrix_livre();
timeToDisplay = valueBook.getDate_creation();
valueBook.setNom_livre(titreLivreToDisplay);
valueBook.setDesc_livre(descLivreToDisplay);
valueBook.setPrix_livre(prixLivreToDisplay);
valueBook.setDate_creation(timeToDisplay);
feedItems.add(valueBook);
}
listAdapter.notifyDataSetChanged();
}
});
}
}
});
适配器代码:
public FeedListAdapter(Context context, List<Book> feedItems) {
this.context = context;
this.feedItems = feedItems;
this.mStringFilterList=feedItems;
}
static class ViewHolder {
TextView name ;
TextView timestamp;
TextView statusMsg ;
TextView prix;
}
@Override
public int getCount() {
return feedItems.size();
}
@Override
public Object getItem(int location) {
return feedItems.get(location);
}
@Override
public long getItemId(int position) {
return feedItems.indexOf(getItem(position));
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final int pos = position;
if (inflater == null)
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.feed_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name_book_display);
holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
holder.statusMsg = (TextView) convertView.findViewById(R.id.desc_display);
holder.prix = (TextView) convertView.findViewById(R.id.prix_display);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Book item = feedItems.get(position);
if (!TextUtils.isEmpty(item.getNom_livre())) {
holder.name.setText(item.getNom_livre());
holder.name.setVisibility(View.VISIBLE);
} else {
holder.name.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(item.getDate_creation())) {
holder.timestamp.setText(item.getDate_creation());
holder.timestamp.setVisibility(View.VISIBLE);
} else {
holder.timestamp.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(item.getDesc_livre())) {
holder.statusMsg.setText(item.getDesc_livre());
holder.statusMsg.setVisibility(View.VISIBLE);
} else {
holder.statusMsg.setVisibility(View.GONE);
}
if (item.getPrix_livre() != null) {
holder.prix.setText(item.getPrix_livre() + " DH");
holder.prix.setVisibility(View.VISIBLE);
} else {
holder.prix.setVisibility(View.GONE);
}
return convertView;
}
答案 0 :(得分:1)
每当您将一个项目添加到映射到您的ArrayList中 ListView,将该项目添加到位置0
如下所示
feedItems.add(0,valueBook);
上一行将在顶部添加最新项。