アクティビティによって呼び出されたBottomSheetDialogレイアウトxmlファイルがあります。 BottomSheetDialogには2つのボタンがあります。 BottomSheetDialog内のボタンをクリックすると、別のアクティビティに移動します。
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="#fff"
Android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.ezeelearn.colorstonz.ezeelearn.PhysicsTestSelectorActivity"
tools:showIn="@layout/app_bar_physics_lesson">
<ScrollView
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<TableLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="horizontal"
Android:id="@+id/lesson_list">
</TableLayout>
</ScrollView>
<include layout="@layout/bottom_sheet_layout" />
</LinearLayout>
bottom_sheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
Android:id="@+id/RelativeLayoutSheet"
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
app:layout_behavior="@string/bottom_sheet_behavior"
Android:background="#ffffff"
app:behavior_hideable="true"
Android:padding="20dp"
>
<TableLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<TableRow
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:weightSum="2">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_weight="1"
Android:paddingRight="50dp"
Android:paddingLeft="50dp"
Android:paddingTop="20dp"
Android:paddingBottom="20dp"
Android:layout_height="wrap_content"
Android:orientation="horizontal">
<Button
Android:layout_width="100dp"
Android:layout_height="100dp"
Android:layout_weight="1"
Android:background="#fff"
Android:drawableTop="@drawable/google_cardboard"
Android:drawablePadding="6dp"
Android:gravity="left|center"
Android:height="60dp"
Android:padding="6dp"
Android:text="Video"
Android:id="@+id/bottom_sheet_video_btn"
Android:textAlignment="center"
Android:fontFamily="sans-serif-light"
Android:foreground="?android:attr/selectableItemBackground"
Android:textColor="#666" />
</LinearLayout>
<LinearLayout
Android:layout_width="match_parent"
Android:layout_weight="1"
Android:paddingRight="50dp"
Android:paddingLeft="50dp"
Android:paddingTop="20dp"
Android:paddingBottom="20dp"
Android:layout_height="wrap_content"
Android:orientation="horizontal">
<Button
Android:layout_width="100dp"
Android:layout_height="100dp"
Android:layout_weight="1"
Android:background="#fff"
Android:drawableTop="@drawable/bulletin_board"
Android:drawablePadding="6dp"
Android:gravity="left|center"
Android:height="60dp"
Android:padding="6dp"
Android:text="Lesson"
Android:id="@+id/bottom_sheet_lesson_btn"
Android:textAlignment="center"
Android:fontFamily="sans-serif-light"
Android:foreground="?android:attr/selectableItemBackground"
Android:textColor="#666" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.Java
public class MainActivity extends Activity{
BottomSheetDialog bottomSheetDialog;
BottomSheetBehavior bottomSheetBehavior ;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
List<String> lessonNames = new ArrayList<String>();
lessonNames.add("Photosynthesis in Higher Plants");
lessonNames.add("The Living World");
lessonNames.add("Biological Classification");
lessonNames.add("Plant Kingdom");
lessonNames.add("Animal Kingdom");
lessonNames.add("Morphology of Flowering Plants");
lessonNames.add("Anatomy of Flowering Plants");
lessonNames.add("Structural Organisation in Animals");
lessonNames.add("Cell-The Unit of Life");
lessonNames.add("Biomolecules");
lessonNames.add("Transport in Plants");
lessonNames.add("Mineral Nutrition");
lessonNames.add("Flowering Plants");
TableLayout lessonList = (TableLayout) findViewById(R.id.lesson_list);
List<TableLayout> lessonTableList = new ArrayList<TableLayout>();
for(int i = 0; i < lessonNames.size(); i++) {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(tableLayoutParams);
tableRow.setPadding(30, 25, 30, 25);
tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));
TextView textView = new TextView(this);
textView.setText(String.format("%02d", (i+1))+". "+lessonNames.get(i));
/*textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);*/
textView.setTextSize(17);
tableRow.addView(textView);
/*TextView textView = new TextView(this);
textView.setText(String.format("%02d", (i+1))+". "+lessonNames.get(i));
textView.setPadding(20, 25, 20, 25);
textView.setTextSize(17);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);
textView.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));
lessonList.addView(textView);*/
lessonList.addView(tableRow);
lessonTableList.add(lessonList);
}
for(final TableLayout tableLayout: lessonTableList) {
tableLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bottomSheetDialog = new BottomSheetDialog(PhysicsLessonActivity.this);
View view = getLayoutInflater().inflate(R.layout.bottom_sheet_layout, null);
bottomSheetDialog.setContentView(view);
bottomSheetDialog.show();
bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
Log.d("BottomSheetVideoBtn", "called");
return false;
}
});
}
});
}
Button bottomSheetVideoBtn = (Button) findViewById(R.id.bottom_sheet_video_btn);
Button bottomSheetLessonBtn = (Button) findViewById(R.id.bottom_sheet_lesson_btn);
bottomSheetVideoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
Log.d("BottomSheetVideoBtn", "called");
startActivity(intent);
}
});
bottomSheetLessonBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Lesson Button Clicked", Toast.LENGTH_LONG).show();
}
});
ImageButton backToPHome = (ImageButton) findViewById(R.id.back_to_home);
backToPHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
startActivity(intent);
}
});
}
}
これを使用
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
Button btn1 = (Button)v.findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),YourActivity.class));
}
});
return v;
}
}
次に、あなたの活動でこのbottomSheetを呼び出します
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");
新しいクラスを作成するのではなく、これはどうですか
final BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(R.layout.your_bottomsheet_layout);
dialog.setCanceledOnTouchOutside(false);
ImageButton btnClose = (ImageButton) dialog.findViewById(R.id.button_close);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();