Pada kali ini, saya akan membuat tambahan slide dengan android pada bagian bawah :
buat xml di res/layout dengan nama "bottom_sheet.xml"
buat xml di res/layout dengan nama "bottom_sheet.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" android:id="@+id/bottom_sheet" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="445dp" android:background="@color/colorGrey" app:behavior_hideable="false" app:behavior_peekHeight="?actionBarSize" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="?actionBarSize" android:background="@color/colorGrey" android:paddingStart="@dimen/activity_horizontal_margin" android:paddingEnd="@dimen/activity_horizontal_margin"> <TextView android:id="@+id/bottom_sheet_heading_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Keterangan Pekerjaan" android:textStyle="bold" android:textColor="@color/colorPrimaryDark" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" app:layout_constraintEnd_toStartOf="@+id/bottom_sheet_right_arrow" android:layout_marginEnd="8dp" app:layout_constraintStart_toEndOf="@+id/bottom_sheet_left_arrow" android:layout_marginStart="8dp"/> <ImageView android:id="@+id/bottom_sheet_left_arrow" android:layout_width="wrap_content" android:layout_height="0dp" app:srcCompat="@drawable/ic_arrow_up" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintStart_toStartOf="parent"/> <ImageView android:id="@+id/bottom_sheet_right_arrow" android:layout_width="wrap_content" android:layout_height="0dp" app:srcCompat="@drawable/ic_arrow_up" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"/> </android.support.constraint.ConstraintLayout> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="8dp" android:paddingRight="8dp"> <TextView android:id="@+id/daftar_muat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Daftar Muat" android:textSize="18dp" android:textColor="@color/colorPrimaryDark"/> <TextView android:id="@+id/loading_memo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/daftar_muat" android:text="Loading_memo" android:textSize="18dp" android:textColor="@color/colorPrimaryDark"/> </RelativeLayout> </LinearLayout>
pada main_activity.xml , tambahkan :
<FrameLayout android:id="@+id/main_fragment_container" android:layout_width="match_parent"
android:layout_height="match_parent"/>
tambahkan layout fragment dengan nama "fragment_main.xml" :
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".button.HalamanPertama"> <android.support.design.widget.CoordinatorLayout android:id="@+id/coordinator_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- include main content --> <!--<include layout="@layout/trip_stats_main_content"/>--> <!-- include bottom sheet --> <include layout="@layout/bottom_sheet"/> </android.support.design.widget.CoordinatorLayout>
</FrameLayout>
Buat MainFragmen.java :
public class MainFragment extends Fragment { private LinearLayout mBottomSheet; private ImageView mLeftArrow; private ImageView mRightArrow; public MainFragment() { // Required empty public constructor } public static MainFragment newInstance() { return new MainFragment(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_main, container, false); // find container view mBottomSheet = view.findViewById(R.id.bottom_sheet); // find arrows mLeftArrow = view.findViewById(R.id.bottom_sheet_left_arrow); mRightArrow = view.findViewById(R.id.bottom_sheet_right_arrow); initializeBottomSheet(); return view; } private void initializeBottomSheet() { // init the bottom sheet behavior BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(mBottomSheet); // change the state of the bottom sheet bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); // change the state of the bottom sheet bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); // set callback for changes bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { if (isAdded()) { transitionBottomSheetBackgroundColor(slideOffset); animateBottomSheetArrows(slideOffset); } } }); } private void transitionBottomSheetBackgroundColor(float slideOffset) { //int colorFrom = getResources().getColor(R.color.colorAccent); //int colorTo = getResources().getColor(R.color.colorAccentAlpha60); int colorFrom = getResources().getColor(R.color.timestamp); int colorTo = getResources().getColor(R.color.colorGrey); mBottomSheet.setBackgroundColor(interpolateColor(slideOffset, colorFrom, colorTo)); } private void animateBottomSheetArrows(float slideOffset) { mLeftArrow.setRotation(slideOffset * -180); mRightArrow.setRotation(slideOffset * 180); } // Helper method to interpolate colors private int interpolateColor(float fraction, int startValue, int endValue) { int startA = (startValue >> 24) & 0xff; int startR = (startValue >> 16) & 0xff; int startG = (startValue >> 8) & 0xff; int startB = startValue & 0xff; int endA = (endValue >> 24) & 0xff; int endR = (endValue >> 16) & 0xff; int endG = (endValue >> 8) & 0xff; int endB = endValue & 0xff; return ((startA + (int) (fraction * (endA - startA))) << 24) | ((startR + (int) (fraction * (endR - startR))) << 16) | ((startG + (int) (fraction * (endG - startG))) << 8) | ((startB + (int) (fraction * (endB - startB)))); }}
tambahkan di MainActivity.java :
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity);
MainFragment fragment = MainFragment.newInstance(); getSupportFragmentManager() .beginTransaction() .replace(R.id.main_fragment_container, fragment) .commit();}Berikut contoh keluaran program :Demikian penjelasan kali ini, komentar dan masukkan sangan di terima. Terima kasih
Comments
Post a Comment