Android 抽屉布局填充来自其他活动的菜单
我正在创建一个带有两个导航抽屉的菜单,每侧一个: 一切都正常工作,但现在我想将填充任务从主活动分离到左活动和右活动,因此我将布局分离(只是为了更好的结构) 这是主布局:Android 抽屉布局填充来自其他活动的菜单,android,drawerlayout,Android,Drawerlayout,我正在创建一个带有两个导航抽屉的菜单,每侧一个: 一切都正常工作,但现在我想将填充任务从主活动分离到左活动和右活动,因此我将布局分离(只是为了更好的结构) 这是主布局: 和左侧抽屉布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/left_drawer
和左侧抽屉布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#111"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/buttons_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/login_image_button"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="@string/login_image"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/logout_image_button"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="@string/login_image"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<ListView
android:id="@+id/left_list_viewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:divider="@android:color/black"
android:dividerHeight="0dp" >
</ListView>
现在,我在左侧导航抽屉列表查看器中填充了一些假人。
public class LeftActivity extends SherlockActivity implements OnClickListener{
private ListView leftListView;
private ArrayAdapter<String> listAdapter;
// Session Manager Class
SessionManager session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left);
moveTaskToBack(true);
leftListView = (ListView) findViewById(R.id.left_list_viewer);
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.asList(planets));
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add("Ceres");
listAdapter.add("Pluto");
listAdapter.add("Haumea");
listAdapter.add("Makemake");
listAdapter.add("Eris");
// Set the ArrayAdapter as the ListView's adapter.
leftListView.setAdapter(listAdapter);
// OnClickListener's for all button, after pressed it will send for the
// onClick method the button pressed
ImageButton loginImageButton = (ImageButton) findViewById(R.id.login_image_button);
loginImageButton.setOnClickListener(this);
ImageButton logouImageButton = (ImageButton) findViewById(R.id.logout_image_button);
logouImageButton.setOnClickListener(this);
}
// Method to decide what to do from what button was pressed
public void onClick(View v) {
session = new SessionManager(getApplicationContext());
switch (v.getId()) {
case R.id.login_image_button:
if (session.isLoggedIn() == false) {
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Log.d("MoodyDebud",
"Entrará aqui se o utilizador ja estiver logado e em vez de vir para aqui irá para as defeniçoes de utilizador");
}
break;
case R.id.logout_image_button:
if (session.isLoggedIn() == true) {
session.logoutUser();
} else {
// só entra neste else caso o utilizador ainda nao esteja
// loggado entao é reencaminhado para o LoginActivity
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
break;
default:
throw new RuntimeException("Unknown button ID");
}
}
公共类LeftActivity扩展SherlockActivity实现OnClickListener{
私有ListView-leftListView;
专用阵列适配器列表适配器;
//会话管理器类
会话管理器会话;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局。活动左);
moveTaskToBack(真);
leftListView=(ListView)findViewById(R.id.left\u list\u查看器);
//创建并填充行星名称列表。
字符串[]行星=新字符串[]{“水星”、“金星”、“地球”、“火星”,
“木星”、“土星”、“天王星”、“海王星”};
ArrayList planetList=新的ArrayList();
planetList.addAll(Arrays.asList(planets));
//使用行星列表创建ArrayAdapter。
listAdapter=new ArrayAdapter(此,R.layout.simplerow,
行星学家);
//添加更多行星。如果传递的是字符串[]而不是列表
//在ArrayAdapter构造函数中,不能添加更多项。
//否则将发生异常。
添加(“Ceres”);
添加(“Pluto”);
添加(“Haumea”);
添加(“Makemake”);
添加(“Eris”);
//将ArrayAdapter设置为ListView的适配器。
setAdapter(listAdapter);
//OnClickListener的for all按钮,按下后将发送
//按一下按钮
ImageButton loginImageButton=(ImageButton)findViewById(R.id.login\u image\u按钮);
loginImageButton.setOnClickListener(这个);
ImageButton logouImageButton=(ImageButton)findViewById(R.id.logout\u image\u按钮);
logouImageButton.setOnClickListener(此);
}
//方法从按下的按钮决定要执行的操作
公共void onClick(视图v){
session=newsessionmanager(getApplicationContext());
开关(v.getId()){
案例R.id.login\u image\u按钮:
if(session.isLoggedIn()==false){
意向意向=新意向(getApplicationContext(),
物流活动(类);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
星触觉(意向);
}否则{
Log.d(“MoodyDebud”,
“将水资源和水资源作为水资源的防御措施进行评估”);
}
打破
案例R.id.注销\图像\按钮:
if(session.isLoggedIn()==true){
session.logoutUser();
}否则{
//在其他地方,我们可以利用这些资源
//后勤活动部
意向意向=新意向(getApplicationContext(),
物流活动(类);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
星触觉(意向);
}
打破
违约:
抛出新的RuntimeException(“未知按钮ID”);
}
}
}
但我的问题是,当我启动mainActivity时,LeftActivity未初始化,listview为空,我认为这不是一个选项,因为它将位于主菜单上方。
有什么办法解决这个问题吗?提前感谢;) 没人能帮忙?!甚至是一些裁判?!
public class LeftActivity extends SherlockActivity implements OnClickListener{
private ListView leftListView;
private ArrayAdapter<String> listAdapter;
// Session Manager Class
SessionManager session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left);
moveTaskToBack(true);
leftListView = (ListView) findViewById(R.id.left_list_viewer);
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.asList(planets));
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add("Ceres");
listAdapter.add("Pluto");
listAdapter.add("Haumea");
listAdapter.add("Makemake");
listAdapter.add("Eris");
// Set the ArrayAdapter as the ListView's adapter.
leftListView.setAdapter(listAdapter);
// OnClickListener's for all button, after pressed it will send for the
// onClick method the button pressed
ImageButton loginImageButton = (ImageButton) findViewById(R.id.login_image_button);
loginImageButton.setOnClickListener(this);
ImageButton logouImageButton = (ImageButton) findViewById(R.id.logout_image_button);
logouImageButton.setOnClickListener(this);
}
// Method to decide what to do from what button was pressed
public void onClick(View v) {
session = new SessionManager(getApplicationContext());
switch (v.getId()) {
case R.id.login_image_button:
if (session.isLoggedIn() == false) {
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Log.d("MoodyDebud",
"Entrará aqui se o utilizador ja estiver logado e em vez de vir para aqui irá para as defeniçoes de utilizador");
}
break;
case R.id.logout_image_button:
if (session.isLoggedIn() == true) {
session.logoutUser();
} else {
// só entra neste else caso o utilizador ainda nao esteja
// loggado entao é reencaminhado para o LoginActivity
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
break;
default:
throw new RuntimeException("Unknown button ID");
}
}