7
votes

Ajouter 2 ListViews dans le tiroir de navigation, un seul travail

J'essaie de créer un tiroir de navigation avec 2 ListView à l'intérieur.

La première liste appelée "MDRAWERLIST" forte> est bien affichée. - Il n'y a qu'un seul élément dans cette liste. P>

La deuxième liste d'envoi appelée "mlistprochedechezvous" strong> n'est jamais affiché. Strong> em> em> em> - Il y a 3 éléments dans cette listeView. P>

Lorsque je mets dans des commentaires, la première liste de liste, la seconde n'est pas affichée, je pense donc qu'il y a un problème lors de la création de la deuxième liste de liste .. mais je n'ai pas 't savoir où? p>

Voici le code de la mise en page de tiroir de navigation: p> xxx pré>

et voici un morceau de code de ma classe de MainActivity: P>

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList, mListProcheDeChezVous;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

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

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Declaration of the 2 listview's 
        mDrawerList = (ListView) findViewById(R.id.dernieres_news);
        mListProcheDeChezVous= (ListView) findViewById(R.id.pres_de_chez_vous);

        LayoutInflater inflater = getLayoutInflater();

        // Add header news title
        ViewGroup header_news = (ViewGroup)inflater.inflate(R.layout.header_dernieres_news, mDrawerList, false);
        mDrawerList.addHeaderView(header_news, null, false);

        // Add header "proche de chez vous title"
        ViewGroup header_pres_de_chez_vous = (ViewGroup)inflater.inflate(R.layout.header_pres_de_chez_vous, mListProcheDeChezVous, false);
        mListProcheDeChezVous.addHeaderView(header_pres_de_chez_vous, null, false);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        /*
         * FIRST ADAPTER FOR FIRST LISTVIEW
         */

        String[] names=new String[]{"Dernières News"};

        /*Array of Images*/
        int[] image = new int[] {R.drawable.ic_action_feed};

        List<HashMap<String, String>> listinfo = new ArrayList<HashMap<String, String>>();
        listinfo.clear();
        for(int i=0;i<1;i++){
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("name", names[i]);
            hm.put("image", Integer.toString(image[i]));
            listinfo.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "image", "name" };
        int[] to = { R.id.img, R.id.txt };
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to);
        mDrawerList.setAdapter(adapter);

        /*
         * SECOND ADAPTER FOR SECOND LISTVIEW
         */

        String[] names_pres_de_chez_vous = new String[]{"Finistère", "Morbihan", "Côtes d'Armor"};

        /*Array of Images*/
        int[] image_pres_de_chez_vous = new int[] {R.drawable.ic_action_gear, R.drawable.ic_action_gear, R.drawable.ic_action_gear};

        List<HashMap<String, String>> listinfo_pres_de_chez_vous = new ArrayList<HashMap<String, String>>();
        listinfo_pres_de_chez_vous.clear();
        for(int i=0;i<3;i++){
            HashMap<String, String> hm_pres_de_chez_vous = new HashMap<String, String>();
            hm_pres_de_chez_vous.put("name", names_pres_de_chez_vous[i]);
            hm_pres_de_chez_vous.put("image", Integer.toString(image_pres_de_chez_vous[i]));
            listinfo_pres_de_chez_vous.add(hm_pres_de_chez_vous);
        }

        // Keys used in Hashmap
        String[] from_pres_de_chez_vous = { "image", "name" };
        int[] to_pres_de_chez_vous = { R.id.img, R.id.txt };
        SimpleAdapter adapter_pres_de_chez_vous = new SimpleAdapter(getBaseContext(), listinfo_pres_de_chez_vous, R.layout.drawer_list_item_pres_de_chez_vous, from_pres_de_chez_vous, to_pres_de_chez_vous);
        mListProcheDeChezVous.setAdapter(adapter_pres_de_chez_vous);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        mListProcheDeChezVous.setOnItemClickListener(new DrawerItemClickListener());


1 commentaires

DrawerLayout ne prend en charge que deux vues sur les enfants. Pourquoi avez-vous besoin de deux listwieviews quand même?


4 Réponses :


4
votes

Selon la création d'un Guide de tiroir de navigation , < Code> DrawerLayouts ne devrait avoir que deux enfants. Le premier est la vue principale du contenu, et la seconde est la vue de tiroir.

Votre framelayout avec un identifiant de "content_frame" est interprété comme la vue avec le contenu principal et votre listview avec un identifiant de "dernieres_news" est interprété comme la disposition du tiroir.

Le troisième listview est ainsi ignoré.

Si vous avez besoin des deux listview dans le cadre du tiroir, vous devez les envelopper dans une autre mise en page telle qu'un linearlayout . .


3 commentaires

Merci, il est clair avec votre explication. J'ai essayé de regrouper les 2 ListViews dans une mise en page linéaire, mais j'ai cette erreur lorsque l'application est démarrée: Impossible de démarrer l'activité ComponentInfo {com.example.android.navigationdraweexample / Co m.example.android.na VIGATIONDRAWEREXAMPL E.MainSactivité}: java.lang.classcastException: android.widget.linearlayout $ $ LayOutParams ne peut pas être jeté sur android.support.v4.widget.drawerlayout $ SmyoutParams


Hey Wawanopoulos, j'ai aussi rencontré le problème comme vous êtes comme vous.


Juste au cas où quelqu'un trébuche à l'occasion d'un clascastexception comme @wawanopoulos et moi. Au moins dans ma raison de cas, le code environnant de l'activité qui a toujours utilisé l'ancien Single ListView pour OpenDrawer (), Fermédrawer (), etc.



3
votes

Wrap La listeView à Linearlayout est une excellente solution. Je viens de faire de cette façon et ça marche. Voici ma démonstration XML:

<LinearLayout
        android:id="@+id/drawer_Linearlayout"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="240dp"
            android:layout_height="wrap_content" 
            android:text="ABC"/>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>


0 commentaires

0
votes

La première listeView a Android: Layout_height = "match_parent" Cela signifie qu'il occupe tout le tiroir ... utiliser Android: Layout_height = "Wrap_Content" Dans les deux ListViews


0 commentaires

1
votes

Vous pouvez définir plus d'une liste de vision / n'importe quelle vue personnalisée Ajouter à l'intérieur d'un groupe de visiteurs:

Code pour l'activité XML de tiroir principal: P>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawerlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

<!-- This is the fragment Layout -->

        <FrameLayout
            android:id="@+id/fragmnetlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>

 <!-- this LinearLayout as a Drawerlayout inside it create two ListView With Its Items Title -->

        <LinearLayout
            android:id="@+id/lldrawercontent"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvsocialsites"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#12ccad"
                android:gravity="center"
                android:text="@string/socialsites"
                android:textSize="15sp" />

            <ListView
                android:id="@+id/drawerlistleft"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffccee"
                android:choiceMode="singleChoice"
                android:divider="@null" />

            <TextView
                android:id="@+id/tvweakdays"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#12ccad"
                android:gravity="center"
                android:text="@string/weaksdy"
                android:textSize="15sp" />

            <ListView
                android:id="@+id/drawerlistweakdy"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#32fbdc"
                android:choiceMode="singleChoice" >
            </ListView>
        </LinearLayout>

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


0 commentaires