应用程序所有活动的导航抽屉布局
Navigation Drawer Layout on all activities of the application
我有两个活动 - 用于用户登录页面的 MainActivity,第二个 activity 在 ListView 中显示用户详细信息(用户创建的项目)。我想创建一个将出现在两个活动中的导航抽屉布局。 但问题是,我的第二个 activity 扩展了 ListActivity 以在 ListView 中显示用户详细信息,因此我无法扩展任何其他内容。这是我的第二个 activity:
public class ProjectList extends ListActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView listView;
JSONArray projects = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project_list_inflator);
listView = (ListView)findViewById(android.R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
new HttpGetHandler().execute();
}
private class HttpGetHandler extends AsyncTask<String, Void, Void> {
String jsonUrl = "some url";
String imgUrl = "http://canvasflip.com/protected/app/elements/userElements/";
@Override
protected Void doInBackground(String... params) {
HttpGet httpGet = new HttpGet(jsonUrl);
//HttpGet httpGet1 = new HttpGet(imgUrl);
try {
HttpResponse httpResponse = MainActivity.httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream content = httpEntity.getContent();
String result = convertToString(content);
JSONObject jsonObject = new JSONObject(result);
projects = jsonObject.getJSONArray("projects");
ProjectList.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
for(int i = 0; i<projects.length(); i++) {
JSONObject p = projects.getJSONObject(i);
String title = p.getString("title");
listItems.add(title);
adapter.notifyDataSetChanged();
}
}catch(Exception e) {
Log.d("MSG", e.toString());
}
}
});
}catch(Exception e) {
System.out.println(e.getMessage());
}
return null;
}
public String convertToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine())!=null) {
result += line;
}
inputStream.close();
return result;
}
}
}
只需创建一个新的 Activity,其中包含导航抽屉实现的源代码
喜欢public class DrawerActivity extends ListActivity
将 DrawerLayout 覆盖为
protected DrawerLayout drawerLayout;
在抽屉的 onCreate 中 Activity 将抽屉布局初始化为,
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
并让您的 MainActivity 扩展 DrawerAcivity。
然后替换给定的 setContentView。
// setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_main, null, false);
drawerLayout.addView(contentView, 0);
我已经成功完成了我的整个应用程序。
编码愉快 :)
我有两个活动 - 用于用户登录页面的 MainActivity,第二个 activity 在 ListView 中显示用户详细信息(用户创建的项目)。我想创建一个将出现在两个活动中的导航抽屉布局。
public class ProjectList extends ListActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView listView;
JSONArray projects = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project_list_inflator);
listView = (ListView)findViewById(android.R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
new HttpGetHandler().execute();
}
private class HttpGetHandler extends AsyncTask<String, Void, Void> {
String jsonUrl = "some url";
String imgUrl = "http://canvasflip.com/protected/app/elements/userElements/";
@Override
protected Void doInBackground(String... params) {
HttpGet httpGet = new HttpGet(jsonUrl);
//HttpGet httpGet1 = new HttpGet(imgUrl);
try {
HttpResponse httpResponse = MainActivity.httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream content = httpEntity.getContent();
String result = convertToString(content);
JSONObject jsonObject = new JSONObject(result);
projects = jsonObject.getJSONArray("projects");
ProjectList.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
for(int i = 0; i<projects.length(); i++) {
JSONObject p = projects.getJSONObject(i);
String title = p.getString("title");
listItems.add(title);
adapter.notifyDataSetChanged();
}
}catch(Exception e) {
Log.d("MSG", e.toString());
}
}
});
}catch(Exception e) {
System.out.println(e.getMessage());
}
return null;
}
public String convertToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine())!=null) {
result += line;
}
inputStream.close();
return result;
}
}
}
只需创建一个新的 Activity,其中包含导航抽屉实现的源代码
喜欢public class DrawerActivity extends ListActivity
将 DrawerLayout 覆盖为
protected DrawerLayout drawerLayout;
在抽屉的 onCreate 中 Activity 将抽屉布局初始化为,
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
并让您的 MainActivity 扩展 DrawerAcivity。
然后替换给定的 setContentView。
// setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_main, null, false);
drawerLayout.addView(contentView, 0);
我已经成功完成了我的整个应用程序。
编码愉快 :)