andriangungon
11/19/2017 - 5:46 AM

collapsing action bar with floating button

package com.kapuso.teleserye.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.dailymotion.websdk.DMWebVideoView;
import com.kapuso.teleserye.R;
import com.kapuso.teleserye.adapter.RelatedVideosAdapter;
import com.kapuso.teleserye.data.Constant;
import com.kapuso.teleserye.library.PrefManager;
import com.kapuso.teleserye.model.Video;
import com.kapuso.teleserye.networking.general.APIClient;
import com.kapuso.teleserye.networking.videos.related.RelatedAPI;
import com.kapuso.teleserye.networking.videos.related.RelatedInterface;
import com.kapuso.teleserye.networking.videos.related.RelatedResponse;
import com.kapuso.teleserye.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.content.ContentValues.TAG;

public class  VideosByCategoryActivity extends AppCompatActivity {

    private static int VIDEO_CATEGORY_ID;
    private RecyclerView recyclerView;
    private RelatedVideosAdapter relatedVideosAdapter;
    private Context context;
    private ActionBar actionBar;
    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videos_by_category);

        Bundle extras = getIntent().getExtras();
        VIDEO_CATEGORY_ID = extras.getInt("category_id");

        initToolbar();
        initialization();
        relatedVideos();
    }

    private void initToolbar() {
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle("Categories");
    }

    private void initialization() {

        context = this;

        recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

    }

    public void relatedVideos() {

        RelatedInterface apiService = APIClient.getClient().create(RelatedInterface.class);
        Call<RelatedResponse> call = apiService.getResults(VIDEO_CATEGORY_ID);
        call.enqueue(new Callback<RelatedResponse>() {

            @Override
            public void onResponse(Call<RelatedResponse> call, Response<RelatedResponse> response) {

                if (response.isSuccessful()) {

                    try {

                        Constant.showLoading(context, "Videos are being loaded...");

                        List<RelatedAPI> videos = response.body().getData();

                        Log.v(TAG, "Videos Size: " + videos.size());

                        ArrayList<Video> videoItem = new ArrayList<>();

                        for (int i = 0; i < videos.size(); i++) {
                            videoItem.add(new Video(
                                    videos.get(i).getVid_category_title(),
                                    videos.get(i).getVid_created_at(),
                                    videos.get(i).getVid_url(),
                                    videos.get(i).getVid_category_id(),
                                    videos.get(i).getVid_start(),
                                    videos.get(i).getVid_end())
                            );

                            relatedVideosAdapter = new RelatedVideosAdapter(context, videoItem);

                        }

                        recyclerView.setAdapter(relatedVideosAdapter);
                        Constant.hideLoading();

                    } catch (Throwable e) {
                        Constant.hideLoading();
                        //Constant.showGeneralWarning(context, "Error (Throwable): " + e.getMessage());
                        Log.d(TAG, "Error (Throwable): " + e.getMessage());
                    }

                    // stopping swipe refresh
                    //swipeRefreshLayout.setRefreshing(false);

                } else {

                    Constant.hideLoading();

                    if(response.code() == 401){


                    }

                    Log.d(TAG, "Retrofit Failed: " + response.message());
                }

            }

            @Override
            public void onFailure(Call<RelatedResponse> call, Throwable t) {

                // stopping swipe refresh
                //swipeRefreshLayout.setRefreshing(false);

                Log.d(TAG, "Kapuso Error: " + t.getMessage());
            }

        });

    }
}

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:layout_marginBottom="50dp"
                android:layout_marginTop="8dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_refresh_white_48dp"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>


</android.support.design.widget.CoordinatorLayout>