0

This is my actual Activity : enter image description here

In this Activity I set All the ListView(s) dynamically and programmatically. As you can see, the problem I have is The 1st ListView takes the place she needs and the second ListView try to keep showing by internally adding a scroll bar..

I just wanted to know how can I say programmatically to let all the ListView(s) take the entire place she needs and just add a ScrollBar to all the activity (To Scroll the Menu for example)?

Here is my Activity and my Adapter :

public class SousBoissonsActivity extends Activity {
    private String nom_categorie;
    private ArrayList<String> arraySousCategoriesName;
    private ArrayList<ArrayList<Drink>> arraySousCategories;
    private ArrayList<MyAdapter> myAdapters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sousboissons_list_item);

        nom_categorie = getIntent().getStringExtra("nom_categorie");
        setTitle(nom_categorie);
        DrinksContainer.setSousCategoriesNameArray(nom_categorie);
        DrinksContainer.setSousCategoriesArray(nom_categorie);

        arraySousCategories = DrinksContainer.sousCategoriesArray;
        myAdapters = new ArrayList<MyAdapter>();

        new DoDirtyJobAsyncTask().execute();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    private class DoDirtyJobAsyncTask extends AsyncTask<Void, MyAdapter, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            for (ArrayList<Drink> arrayElement : arraySousCategories) {
                MyAdapter myAdapter = new MyAdapter(getApplicationContext(),
                        arrayElement);
                myAdapters.add(myAdapter);
                publishProgress(myAdapter);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(MyAdapter... myAdapters) {
            int currViewId = 1;
            LinearLayout ll = (LinearLayout) findViewById(R.id.sousboissons_linearlayout);
            for (MyAdapter myAdapter : myAdapters) {
                TextView sousCategorieTitle = new TextView(
                        getApplicationContext(), null);
                sousCategorieTitle.setText(myAdapter.sousCategory);

                sousCategorieTitle.setBackgroundColor(getResources().getColor(
                        R.color.green));
                sousCategorieTitle.setTextSize(19);
                sousCategorieTitle.setTextColor(getResources().getColor(
                        R.color.white));
                sousCategorieTitle.setGravity(Gravity.CENTER);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(0, 0, 0, 0);
                sousCategorieTitle.setLayoutParams(layoutParams);

                ListView listview = new ListView(getApplicationContext(), null);
                listview.setId(currViewId);
                listview.setAdapter(myAdapter);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.FILL_PARENT);
                layoutParams2.setMargins(17, 0, 17, 0);
                listview.setLayoutParams(layoutParams2);

                ll.setPadding(15, 15, 15, 0);
                ll.addView(sousCategorieTitle);
                ll.addView(listview);
                currViewId++;
            }
        }

    }

    class MyAdapter extends ArrayAdapter<Drink> {
        LayoutInflater inflat;
        private ArrayList<Drink> items;
        private String sousCategory;

        public MyAdapter(Context context, ArrayList<Drink> objects) {
            super(
                    context,
                    R.layout.activity_sousboissons_list_item_elementsouscategorie,
                    objects);
            this.items = objects;
            this.inflat = LayoutInflater.from(context);
            this.sousCategory = objects.get(0).getType();
        }

        private class ViewHolder {
            public TextView title;
            public TextView prix;
            public TextView desc;
            public TextView size;
            public ImageView img;
            public LinearLayout ll;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String currentKey = "";
            String keyDialog = "";
            ViewHolder holder = null;
            Drink element = items.get(position);

            // Instantiate Labels and Design
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflat
                        .inflate(
                                R.layout.activity_sousboissons_list_item_elementsouscategorie,
                                null);

                holder.title = (TextView) convertView
                        .findViewById(R.id.sousboissons_element_title);
                holder.desc = (TextView) convertView
                        .findViewById(R.id.sousboissons_element_desc);
                holder.prix = (TextView) convertView
                        .findViewById(R.id.sousboissons_element_prix);
                holder.size = (TextView) convertView
                        .findViewById(R.id.sousboissons_element_size);

                if (element.getPrice().size() > 1) {
                    holder.ll = (LinearLayout) convertView
                            .findViewById(R.id.sousboissons_element_firt_layout);
                    holder.img = new ImageView(getApplicationContext(), null);

                    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams1.weight = 12;
                    holder.title.setLayoutParams(layoutParams1);

                    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams2.gravity = Gravity.RIGHT;
                    layoutParams2.weight = 1;
                    holder.img.setLayoutParams(layoutParams2);
                    holder.img.setImageResource(R.drawable.arrow);
                    holder.img.setPadding(0, 15, 0, 4);

                    // Alert Dialog
                    final LinearLayout alertDialogLayout = new LinearLayout(
                            SousBoissonsActivity.this);
                    ArrayList<Dictionary> dico = element.getPrice();
                    ListView liste = new ListView(SousBoissonsActivity.this);
                    MyAdapterDialog adapt = new MyAdapterDialog(
                            SousBoissonsActivity.this, element.getPrice());
                    liste.setAdapter(adapt);
                    alertDialogLayout.addView(liste);
                    final AlertDialog.Builder alert = new AlertDialog.Builder(
                            SousBoissonsActivity.this);
                    alert.setTitle(element.getName());
                    alert.setCancelable(true);
                    alert.setPositiveButton("Retour",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.dismiss();
                                }
                            });
                    alert.setView(alertDialogLayout);

                    holder.img.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (alertDialogLayout.getParent() != null)
                                ((ViewGroup) alertDialogLayout.getParent())
                                        .removeView(alertDialogLayout);
                            alert.show();
                        }
                    });
                    holder.ll.addView(holder.img);
                } else {
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // Setting Data on Labels
            if (element != null) {
                holder.title.setText(element.getName());
                holder.desc.setText(element.getDescription());

                if (element.getPrice().size() > 1) {
                    holder.prix.setVisibility(View.GONE);
                    holder.size.setVisibility(View.GONE);
                } else {
                    Dictionary dico = element.getPrice().get(0);
                    Enumeration e = dico.keys();
                    while (e.hasMoreElements()) {
                        currentKey = (String) e.nextElement();
                    }
                    if (currentKey.equalsIgnoreCase("0")) {
                        holder.size.setVisibility(View.GONE);
                        holder.prix
                                .setText((String) dico.get(currentKey) + "€");
                    } else {
                        holder.prix
                                .setText((String) dico.get(currentKey) + "€");
                        holder.size.setText(currentKey + "cl");
                    }
                }
            }
            return convertView;
        }
    }
eento
  • 761
  • 4
  • 20
  • 53
  • Why don't you try and create them side by side with an horizontal slider? That would be much more user friendly imho. (something like this: http://www.e-nature.ch/tech/simple-tutorial-for-a-smooth-horizontal-view-slider-with-android/) – Nuno Gonçalves Mar 28 '13 at 11:03
  • Horizontal Slider is cool but we only can Add class to the main fragment.. here I'm adding ListViews and not using multiple class.. – eento Mar 28 '13 at 11:23
  • Do you know how to use Horizontal Slider with dynamic elements ? – eento Mar 28 '13 at 11:35
  • Try to use this solution [ListView insideof ScrollView][1] [1]: http://stackoverflow.com/questions/15968766/listview-inside-scroll-view-magic/21560592#21560592 – Alan Donizete Feb 04 '14 at 18:49

2 Answers2

1

You should not nest ListViews inside a ScrollView, Android won't know which to scroll. Maybe... you will be able to set programatically the fixed height for each ListView, and then it will work.

What I would recommend is either to use a single ListView, and group in that list all the data, or to use an ExpandableListView.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • yes you are right for the ScrollView.. ExpandableListView is not the design I want to achieve so the way I have to solve it is to make just 1 ListView or may be use horizontal slider (@Nuno Goncalves).. – eento Mar 28 '13 at 11:14
  • If the problem is UI design keep in mind that ExpandableListView can be expanded when loading. When you set the Adapter and get the view set it expanded. The interface is like having 1 ListView and you can set one style for groups (Eaux,Petillants,...) and another for the childs. – AlexBcn Mar 28 '13 at 11:50
  • ok ok I will try to implement ExpandableListView ! I think could be like I want but never use ExpandableListView.. – eento Mar 28 '13 at 12:01
1

Generally its a bad practice to add the ListView inside ScrollView. If you put your ListView or any scrollable View inside theScrollViewit won't work properly because when you touch the screen ,main focus of your touch is on parent view(ScrollView) not the child View (ListView`).

Still if you want to implement it then below is the way you can try out, which is to add the FooterView or HeaderView in your ListView.

Add views that should be above ListView as a header:

    addHeaderView(View v);

and that below as a footer:

    addFooterView(View v);

Put everything what should be above ListView to Header of ListView and the same with footer to add below.

LayoutInflater inflater = LayoutInflater.from(this);
mTop    = inflater.inflate(R.layout.view_top, null);
mBottom = inflater.inflate(R.layout.view_bottom, null);

list.addHeaderView(mTop);
list.addFooterView(mBottom);
// add header and footer before setting adapter
list.setAdapter(mAdapter);

In result you'll get one scrollable view.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102