0

Мне нужно показать расширяемый список внутри ящика навигации. Как я могу это достичь. Я сделал создание только список с помощью этого кодаРасширяемый список выдвижных ящиков

String [] menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"}; 
     dLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     dList = (ListView) findViewById(R.id.left_drawer); 
     adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu); 
     dList.setAdapter(adapter1); 

Я попытался изменить R.layout to expandable list, но он упал с нулевым указателем Exception. Я считаю, что мне нужно настроить содержимое массива в соответствии с этим списком, но я не знаю, как это сделать. Например, мне нужно сделать субтитры для «андроида».

ответ

0

main_activity.java

public class MainActivity extends ActionBarActivity implements android.widget.AdapterView.OnItemClickListener { 

    private DrawerLayout drawerLayout; 
    private ListView listView; 
    private String[] planets; 
    private ActionBarDrawerToggle drawerListener; 


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


     planets = getResources().getStringArray(R.array.planets); 
     listView = (ListView) findViewById(R.id.drawerlist); 
     //listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,object)); 
     listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, planets)); 
     listView.setOnItemClickListener(this); 

     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close){ 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       // TODO Auto-generated method stub 
       super.onDrawerClosed(drawerView); 
      } 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       // TODO Auto-generated method stub 
       super.onDrawerOpened(drawerView); 
      } 
     }; 
     drawerLayout.setDrawerListener(drawerListener); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     // TODO Auto-generated method stub 
     super.onConfigurationChanged(newConfig); 
     drawerListener.onConfigurationChanged(newConfig); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // TODO Auto-generated method stub 
     if (drawerListener.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 

    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onPostCreate(savedInstanceState); 
     drawerListener.syncState(); 
    } 

    @Override 
    //public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
     Toast.makeText(this, planets[position] + "was selected" , Toast.LENGTH_LONG).show(); 
     //Intent intent = new Intent(MainActivity.this, Second.class); 
     //intent.putExtra("sometext", temp.getText()); 
     //intent.putExtra("sometext", planets[position]); 
     //startActivity(intent); 

     if (position == 0) { 
      Intent intent = new Intent(this, Second.class); 
      startActivity(intent); 
      } 
      else if (position == 1) { 
      Intent intent = new Intent(this, third.class); 
      startActivity(intent); 
      } 
     overridePendingTransition(R.anim.slide_in, R.anim.slide_out); 

     //selectItem(position); 
    } 

// public void selectItem(int position) { 
//  // TODO Auto-generated method stub 
//  listView.setItemChecked(position, true); 
//  
// } 



} 

activity_main.xml

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

    <!-- Framelayout to display Fragments --> 

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

    <ListView 
     android:id="@+id/drawerlist" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#FFFFFF" /> 
    <!-- android:entries="@array/planets" --> 

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

MainFest

<uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="21" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

slide_in.xml

<?xml version="1.0" encoding="utf-8"?> 

<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXDelta="100%p" 
    android:toXDelta="0" 
    android:duration="500"/> 

slide_out.xml

<?xml version="1.0" encoding="utf-8"?> 


<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXDelta="0" 
    android:toXDelta="-100%p" 
    android:duration="500"/> 

strings.xml

<string-array name="planets"> 
     <item>1</item> 
     <item>2</item> 
     <item>3</item> 
     <item>4</item> 
     <item>5</item> 
     <item>6</item> 
     <item>7</item> 
     <item>8</item> 
    </string-array> 
Смежные вопросы