両側に 1 つずつ、2 つのナビゲーション ドロワーを持つメニューを作成しています。
すべてが正常に機能していますが、主なアクティビティから左のアクティビティと右のアクティビティに移入タスクを分離したいので、レイアウトを分離しました(構造を改善するためだけに)
これは主なレイアウトです:
<!-- The main content view -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<ListView
android:id="@+id/main_list_viewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="true"
android:paddingLeft="1dp" >
</ListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
</RelativeLayout>
<!-- The left navigation drawer -->
<include layout="@layout/activity_left" />
<!-- The right navigation drawer -->
<include layout="@layout/activity_right" />
そして、左の引き出しのレイアウト:
<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");
}
}
}
しかし、私の問題は、mainActivity を開始するときに leftActivity が初期化されておらず、リストビューが空であることです。メイン メニューの上に表示されるため、オプションではないと思います。これを解決する方法はありますか?前もって感謝します ;)