web-dev-qa-db-fra.com

est-il possible de mettre l'adaptateur en disposition linéaire?

Est-il possible de définir l'adaptateur sur un LinearLayout?

Je ne veux pas utiliser ListView car j'utilise ScrollView. donc j'utilise LinearLayout...

En ce que j'ajoute dynamiquement des vues et maintenant je dois utiliser l'adaptateur .. donc quelqu'un a-t-il des suggestions?

Je fais ça ...

LinearLayout clientList = (LinearLayout) findViewById(R.id.clients);
adapter = new Sample1AdapterActivity(this, arrayList);
View list = (ListView) getLayoutInflater().inflate(R.layout.user_details, getListView(), false);
23
shashi2459

non tu ne peux pas. Ce que vous pouvez faire est de gonfler la ligne unique et de l'ajouter au LinearLayout. En pseudo code:

  LinearLayout linearLayout = (LinearLayout) findViewById(...);
  LayoutInflater inflater = LayoutInflater.from(this);
  for (item in arrayList) {
     View view  = inflater.inflate(R.layout.row, linearLayout, false); 
     // set item content in view
     linearLayout.addView(view)
  }
29
Blackbelt

Oui, vous pouvez en ajoutant votre propre implémentation LinearLayout spécifique pour obtenir des vues enfants de l'adaptateur. Cependant, mon implémentation de base ne fournira pas tout le code de recyclage de vue fourni par la vue de liste.

/**
 * A linear layout that will contain views taken from an adapter. It differs
 * from the list view in the fact that it will not optimize anything and
 * draw all the views from the adapter. It also does not provide scrolling.
 * However, when you need a layout that will render views horizontally and
 * you know there are not many child views, this is a good option.

 *
 * @author Vincent Mimoun-Prat @ MarvinLabs
 */
public class AdapterLinearLayout extends LinearLayout {

    private Adapter adapter;
    private DataSetObserver dataSetObserver = new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            reloadChildViews();
        }
    };

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public AdapterLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public AdapterLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public AdapterLinearLayout(Context context) {
        super(context);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public void setAdapter(Adapter adapter) {
        if (this.adapter == adapter) return;
        this.adapter = adapter;
        if (adapter != null) adapter.registerDataSetObserver(dataSetObserver);
        reloadChildViews();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (adapter != null) adapter.unregisterDataSetObserver(dataSetObserver);
    }

    private void reloadChildViews() {
        removeAllViews();

        if (adapter == null) return;

        int count = adapter.getCount();
        for (int position = 0; position < count; ++position) {
            View v = adapter.getView(position, null, this);
            if (v != null) addView(v);
        }

        requestLayout();
    }
}
19
Vincent Mimoun-Prat