抽屉式导航栏布局上的应用程序的所有活动 [英] Navigation Drawer Layout on all activities of the application
问题描述
我有两个活动 - MainActivity用户登录页面,第二个活动在ListView中显示用户的详细信息(用户制作项目)。我想创建一个抽屉式导航栏的布局,会出现在活动。 我在这里找到了解决办法。但问题是,我的第二个活动延伸ListActivity在ListView控件来显示用户的详细资料,所以我不能延长别的。这里是我的第二个活动:
公共类ProjectList扩展ListActivity {
ArrayList的<串GT;时listItems =新的ArrayList<串GT;();
ArrayAdapter<串GT;适配器;
ListView控件的ListView;
JSONArray项目= NULL;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.project_list_inflator);
ListView控件=(ListView控件)findViewById(android.R.id.list);
适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,listItems中);
listView.setAdapter(适配器);
新HttpGetHandler()执行();
}私有类HttpGetHandler扩展的AsyncTask<弦乐,太虚,太虚> {
字符串jsonUrl =某些URL;
字符串imgUrl的=http://canvasflip.com/protected/app/elements/userElements/;
@覆盖
保护无效doInBackground(字符串... PARAMS){
HTTPGET HTTPGET =新HTTPGET(jsonUrl);
// HTTPGET httpGet1 =新HTTPGET(imgUrl的);
尝试{
HTT presponse HTT presponse = MainActivity.httpClient.execute(HTTPGET);
HttpEntity httpEntity = HTT presponse.getEntity();
InputStream的内容= httpEntity.getContent();
字符串结果= convertToString(内容);
的JSONObject的JSONObject =新的JSONObject(结果);
项目= jsonObject.getJSONArray(项目)。
ProjectList.this.runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
尝试{
的for(int i = 0; I< projects.length();我++){
的JSONObject p值= projects.getJSONObject(ⅰ);
字符串title = p.getString(标题);
listItems.add(职称);
adapter.notifyDataSetChanged();
} }赶上(例外五){
Log.d(MSG,e.toString());
}
}
});
}赶上(例外五){
的System.out.println(e.getMessage());
}
返回null;
} 公共字符串convertToString(的InputStream的InputStream)抛出IOException
的BufferedReader的BufferedReader =新的BufferedReader(新的InputStreamReader(InputStream的));
串线=;
字符串结果=;
而((行= bufferedReader.readLine())!= NULL){
结果+ =行; }
inputStream.close();
返回结果;
}
}
}
只是使具有抽屉式导航implementaion源新活动
像公共类DrawerActivity扩展ListActivity
覆盖DrawerLayout为
保护DrawerLayout drawerLayout;
块引用>在你的抽屉活动的onCreate初始化抽屉布局,
drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout1);
和让你MainActivity扩展DrawerAcivity。
然后再更换的setContentView给出。
//的setContentView(R.layout.activity_main); LayoutInflater吹气=(LayoutInflater)getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看内容查看= inflater.inflate(R.layout.activity_main,空,假);
drawerLayout.addView(内容查看,0);我为我的整个应用成功的做到了这一点。
快乐编码:)
I have two activities - MainActivity for user login page, and the second activity displaying user details(projects made by user) in a ListView. I want to create a Navigation Drawer Layout that will appear in both activities. I found the solution here. But the problem is, my second activity extends ListActivity to display user details in ListView, so I cannot extend anything else. here is my second 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; } } }
解决方案just make a new Activity that has the source of Navigation drawer implementaion
like
public class DrawerActivity extends ListActivity
Override the DrawerLayout asprotected DrawerLayout drawerLayout;
in onCreate of your drawer Activity initialize the drawer layout as,
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
And let your MainActivity extends the DrawerAcivity.
then replace the setContentView as given.
// 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);
I have done this for my whole application successfully.
happy coding :)
这篇关于抽屉式导航栏布局上的应用程序的所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!