0

既知のアイテムで ListView を作成しようとしています。すべての項目は既に ArrayList にあります。

onScrollListener (onScroll メソッド) で一番下にヒットしたことを追跡し、これを行います。

if(firstVisibleItem + visibleItemCount >= totalItemCount)
    // move first item to end of arraylist

問題は; スクロールが一番下にある場合、新しく追加されたアイテムが即座に表示されます。私がやりたいのは、同じ表示項目を画面に表示することです。スクロールした場合のみ新しいアイテムを表示します。

同じ項目を繰り返したいのですが、わかりませんでした。

4

2 に答える 2

0

基本的に、アクティビティに単純な listView を実装できますが、独自に作成した (BaseAdapter から拡張された) 特別な BaseAdapter を使用します。

リストの2番目のパラメーターとして次のようなものを使用できます。

    package com.example.joignabilite;

    import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class ContactAdapter extends BaseAdapter {

        // The list of contact
        private List<Contact> mListP;

        //COntexte of the adapter
        private Context mContext;

        //Mecanism to manage graphism from xml file
        private LayoutInflater mInflater;

        private int white = 0xFFFFFFFF;
        private int grey = 0x33000000;

        public ContactAdapter(Context context, List<Contact> aListP) {
              mContext = context;
              mListP = aListP;
              mInflater = LayoutInflater.from(mContext);
            }

        public int getCount() {
              return mListP.size();
            }

        public Object getItem(int position) {
              return mListP.get(position);
            }

            public long getItemId(int position) {
              return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                  LinearLayout layoutItem;
                  //(1) : Réutilisation des layouts
                  if (convertView == null) {
                    //Initialisation de notre item à partir du  layout XML "Contact_layout.xml"
                    layoutItem = (LinearLayout) mInflater.inflate(R.layout.item_contact, parent, false);
                  } else {
                    layoutItem = (LinearLayout) convertView;
                  }

                  //(2) : Récupération des TextView de notre layout      
                  TextView tv_Nom = (TextView)layoutItem.findViewById(R.id.repertoire_item);

                  //(3) : Renseignement des valeurs       
                  tv_Nom.setText(mListP.get(position).nom);

                  //On mémorise la position de la "Contact" dans le composant textview
                  tv_Nom.setTag(position);        

                  if(position % 2 == 0)
                      layoutItem.setBackgroundColor(grey);
                  else
                      layoutItem.setBackgroundColor(white);

                  //On ajoute un listener
                  tv_Nom.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        //Lorsque l'on clique sur le nom, on récupère la position du "Contact"
                        Integer position = (Integer)v.getTag();

                        //On prévient les listeners qu'il y a eu un clic sur le TextView "TV_Nom".
                        sendListener(mListP.get(position), position);

                    }

                  });

                  //On retourne l'item créé.
                  return layoutItem;
                }   

            /**
             * Interface pour écouter les évènements sur le nom d'une Contact
             */
            public interface ContactAdapterListener {
                public void onClickNom(Contact item, int position);
            }

            //Contient la liste des listeners
            private ArrayList<ContactAdapterListener> mListListener = new ArrayList<ContactAdapterListener>();
            /**
             * Pour ajouter un listener sur notre adapter
             */
            public void addListener(ContactAdapterListener aListener) {
                mListListener.add(aListener);
            }

            private void sendListener(Contact item, int position) {
                for(int i = mListListener.size()-1; i >= 0; i--) {
                    mListListener.get(i).onClickNom(item, position);
                }
            }

    }
于 2013-07-23T16:08:51.063 に答える