2016-10-07 2 views
2

Je veux créer une feuille de fond de navigation comme celle dans Google Maps Navigation BottomSheet où nous avons la liste des directions pour l'itinéraire. J'utilise BottomSheetBehaviour android pour ouvrir la Bottomsheet. Le problème auquel je suis actuellement confronté est que la liste ne s'affiche pas dans la mise en page de la feuille de fond quand elle apparaît. La vue est simplement vide. J'ai également essayé de gonfler la vue moi-même à l'intérieur d'un NestedScrollView pour obtenir le même résultat, mais cela ne s'est pas non plus manifesté.Android listview ne s'affiche pas dans la feuille de fond

Ceci est mon bottomsheet xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/bottomSheet1" 
android:orientation="vertical" 
android:paddingLeft="20dp" 
android:paddingRight="20dp" 
android:elevation="4dp" 
android:background="@color/white" 
app:layout_behavior="android.support.design.widget.BottomSheetBehavior" 
android:clipToPadding="true" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:text="This is a sheet with listview." 
    android:textSize="20dp" 
    android:layout_marginTop="10dp" 
    android:textColor="@color/primaryText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:text="This is a secondary text!" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<android.support.v4.widget.NestedScrollView 
    android:visibility="gone" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/nestedLinearLayout" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 


    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

<ListView 
    android:id="@+id/bottomSheetListview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

Et dans mon code java,

View bottomSheetView = findViewById(R.id.bottomSheet1); 
ListView listView = (ListView) findViewById(R.id.bottomSheetListview); 
List<String> listData = getListData(); //returns a simple array list of strings, about 15 items 
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData)); 
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView); 
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 

que je fais quelque chose de mal ici? Pourquoi le litview ou le scrollview n'apparaissent-ils pas?

enter image description here

Edité Mon ensemble de l'activité

public class BottomSheetActivity extends AppCompatActivity { 

     private static final String TAG = "==> BottomSheetActivity"; 

     BottomSheetBehavior mBottomSheetBehavior; 
     Button peek; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_bottom_sheet); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 

      this.peek = (Button) findViewById(R.id.peek); 

      peek.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        showSheet1(); 
       } 
      }); 

     } 

     private List<String> getListData(){ 
      List<String> stringList = new ArrayList<>(); 
      for (int i = 0; i < 15; i++) { 
       stringList.add("This is string number "+i); 
      } 

      return stringList; 
     } 

     private void showSheet1(){ 
      if(mBottomSheetBehavior != null){ 
       //hide any previous bottom sheets 
       mBottomSheetBehavior.setHideable(true); 
       mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 
      } 
      //initialize a new sheet 
      View bottomSheetView = findViewById(R.id.bottomSheet1); 

      ListView listView = (ListView) bottomSheetView.findViewById(R.id.bottomSheetListview); 
      listView.setVisibility(View.VISIBLE); 
      List<String> listData = getListData(); 
      listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData)); 

      mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView); 
      mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
     } 


    } 

activity_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.vedamic.androidtutorial.BottomSheetActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_bottom_sheet" /> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

    <include layout="@layout/layout_bottom_sheet_1" /> 

</android.support.design.widget.CoordinatorLayout> 

content_bottom_shee.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.vedamic.androidtutorial.BottomSheetActivity" 
    tools:showIn="@layout/activity_bottom_sheet"> 

    <Button 
     android:id="@+id/peek" 
     android:text="peek 1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/expand" 
     android:text="peek 2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/flipboardSheet" 
     android:text="Flipbard BottomSheets" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <ListView 
     android:id="@+id/testListView" 
     android:visibility="gone" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 
+0

Peut-être que c'est parce que votre NestedScrollView a layout_height = "match_parent"? –

+0

J'ai essayé de définir la visibilité de NestedScrollView sur GONE et j'ai ré-exécuté l'application, toujours les mêmes résultats. – Yogesh

Répondre

2

J'importé votre xml et les studios Android « Preview » a en effet montré que le NestedScrollView avec android:layout_height="match_parent" prend tout l'espace de sorte que le ListView n'a pas encore d'espace à gauche.

enter image description here

Lors du réglage du NestedScrollView-android:visibility="gone" le ListView a assez d'espace.

enter image description here

La seule raison maintenant que rien ne montre est peut-être parce que votre listData est vide?

EDIT

OK, je mis en œuvre toutes les infos et le style est très salissant. Tout d'abord, le bouton peek 1 se trouve derrière la barre d'outils.

enter image description here

Mais je peux toujours cliquer, donc cela se produit:

enter image description here

+0

Merci pour l'effort, mais j'ai débogué le processus et mes données de la liste ont montré 15 articles aussi getView() de l'adaptateur a été appelé 15 fois. Le problème persiste. D'autres idées sur comment cela a pu se passer? – Yogesh

+0

Pourriez-vous fournir le code entier de la classe d'activité dans laquelle vous ajoutez la feuille du bas? –

+1

Je viens de poster le code d'activité! – Yogesh

1

Ne faites pas votre visibilité listview disparu au début. Bottom devrait savoir quelle taille il doit être. Si vous avez besoin de faire disparaître sa visibilité, faites-le après la feuille de fond créée.

Et essayez dans votre feuille inférieure content_bottom_shee.xml parent linearlayout

android:fillViewport="true"