tôi thấy hai giải pháp:
Cách đơn giản nhất: lựa chọn có diện tích và danh sách lựa chọn đường như cả hai mảnh thông thường riêng biệt và có cả trong số họ trong một hoạt động riêng biệt và có hoạt động này như một hộp thoại thông qua một chủ đề đơn giản: <activity android:theme="@android:style/Theme.Dialog" />
và có excludeFromRecents="true"
để không có điều này trong các ứng dụng đã sử dụng gần đây. Lựa chọn khu vực được tải trước, sau đó thêm lựa chọn đường phố qua addToBackStack(null)
do đó bạn sẽ có đoạn AreaSelection
bên dưới.
Nếu bạn không muốn có hoạt động riêng vì lý do này, bạn có thể thêm trình nghe thoại từ hộp thoại và trình cài đặt (hoạt động) sẽ mở AreaFragment
.Với một sự hiểu biết cơ bản về mã của bạn dự án đơn giản này nên làm điều đó:
Các hoạt động chủ sở hữu:
import com.example.adip.fragments.AreaSelectionFragment;
import com.example.adip.fragments.StreetSelectionFragment;
import com.example.adip.fragments.AreaSelectionFragment.AreaSelectionListener;
import com.example.adip.fragments.StreetSelectionFragment.StreetSelectionListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
public class DialogsActivity extends FragmentActivity implements OnClickListener,
AreaSelectionListener, StreetSelectionListener {
private static final String AREA_TAG = "AREA_TAG";
private static final String STREETS_TAG = "STREETS_TAG";
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.area_selections);
findViewById(R.id.btnStuff).setOnClickListener(this);
}
@Override
public void onClick(View v) {
showArea();
}
private void showArea() {
DialogFragment df = new AreaSelectionFragment();
df.show(getSupportFragmentManager(), AREA_TAG);
}
@Override
public void onStreetsUserCanceled() {
showArea();
}
@Override
public void showStreets() {
DialogFragment df = new StreetSelectionFragment();
df.show(getSupportFragmentManager(), STREETS_TAG);
}
}
AreaSelectionFragment (mở rộng nó yêu cầu của bạn):
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class AreaSelectionFragment extends DialogFragment {
public static interface AreaSelectionListener {
void showStreets();
}
private AreaSelectionListener areaSelectionListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof AreaSelectionListener) {
areaSelectionListener = (AreaSelectionListener) activity;
} else {
throw new ClassCastException("Parent Activity must implement AreaSelectionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
areaSelectionListener = null;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle("Area Selection")
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
areaSelectionListener.showStreets();
}
}).setNegativeButton("Cancel", null).create();
}
}
Và StreetSelectionFragment
(một lần nữa: mở rộng nó theo nhu cầu của bạn):
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class StreetSelectionFragment extends DialogFragment {
public static interface StreetSelectionListener {
void onStreetsUserCanceled();
}
private StreetSelectionListener selectionListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof StreetSelectionListener) {
selectionListener = (StreetSelectionListener) activity;
} else {
throw new ClassCastException("Parent activity must implement StreetSelectionListener");
}
}
@Override
public void onDetach() {
selectionListener = null;
super.onDetach();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Street Selection")
.setPositiveButton("OK", null).setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectionListener.onStreetsUserCanceled();
}
}).create();
return dialog;
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
selectionListener.onStreetsUserCanceled();
}
}
Cả hai giải pháp đều không đẹp ... 1. Không hoạt động nếu bạn đang làm việc trong các đoạn lồng nhau, 2. Sẽ khởi chạy hộp thoại mới để bạn không thể thực hiện bất kỳ chuyển đổi/hoạt ảnh nào đẹp khi nội dung thay đổi. – Ixx