2013-03-28 31 views
0

这是我的实际活动时间: enter image description here如何在具有多个ListView的Activity中设置ScrollView?

在这个活动我设置的所有ListView控件(S)和动态编程。 正如你所看到的,这个问题我已经是第一的ListView需要的地方,她需要 和第二的ListView尽量保持通过内部加入一个滚动条显示..

我只是想知道我怎么能说编程让所有的ListView(s)在她需要的整个地方取 ,并且只需要添加一个ScrollBar到所有的活动中(例如滚动菜单)?

这里是我的活动,我的适配器:

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; 
     } 
    } 
+0

为什么不尝试用水平滑块并排创建它们?这将会更加方便用户使用。 (像这样:http://www.e-nature.ch/tech/simple-tutorial-for-a-smooth-horizo​​ntal-view-slider-with-android/) – 2013-03-28 11:03:50

+0

水平滑块很酷,但我们只能添加类到主要的片段..在这里我添加ListViews,而不是使用多个类.. – eento 2013-03-28 11:23:50

+0

你知道如何使用水平滑块动态元素? – eento 2013-03-28 11:35:36

回答

1

你不应该巢ListViews一个ScrollView内,Android将不知道哪个滚动。也许...你将能够编程设置每个ListView的固定高度,然后它就可以工作。

我推荐的是使用单个ListView,并在该列表中将所有数据分组,或使用ExpandableListView

+0

是的,你是正确的ScrollView .. ExpandableListView是不是我想要实现的设计,所以我必须解决它的方式是使1只ListView或可以使用水平滑块(@Nuno Goncalves).. – eento 2013-03-28 11:14:17

+0

如果问题是UI设计请记住,加载时可扩展ExpandableListView。当您设置适配器并将视图设置为展开状态时。界面就像有一个ListView,你可以为组(Eaux,Petillants,...)设置一个样式,为孩子设置另一个样式。 – AlexBcn 2013-03-28 11:50:38

+0

好吧我会尝试实现ExpandableListView!我认为可能会像我想但从来没有使用ExpandableListView .. – eento 2013-03-28 12:01:39

1

一般而言,在ScrollView内部添加ListView的做法很糟糕。如果你把你的ListView or any scrollable View inside the ScrollView it 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`)。

不过,如果你想实现它,然后下面是你可以尝试的方式,这是添加FooterViewHeaderViewListView

添加意见认为应高于ListView作为标题:

addHeaderView(View v); 

和下面的页脚:

addFooterView(View v); 

将所有的东西应该是什么上面ListViewListViewHeader和相同页脚添加到下面。

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); 

结果你会得到一个可滚动的视图。

相关问题