我需要在导航抽屉里面展示一个可扩展的列表。我怎样才能做到这一点。 我已经通过此代码创建了一个列表
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崩溃了。我相信我需要设置阵列内容以适应列表,但我不知道如何。我需要为&#34; android&#34;制作潜艇例如。
答案 0 :(得分: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>