Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
created new code architecture for navigation fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
smukov committed Jun 19, 2016
1 parent 82a04f0 commit 51d5e98
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
import android.view.Menu;
import android.view.MenuItem;

import com.thesis.smukov.anative.interfaces.INavigationFragment;
import com.thesis.smukov.anative.NavigationFragment.ContactFragment;
import com.thesis.smukov.anative.NavigationFragment.INavigationFragment;
import com.thesis.smukov.anative.NavigationFragment.ProfileFragment;
import com.thesis.smukov.anative.NavigationFragment.SettingsFragment;

public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

FragmentManager fragmentManager;
INavigationFragment currentFragment;
FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -30,7 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -49,9 +53,11 @@ public void onClick(View view) {
navigationView.setNavigationItemSelectedListener(this);

//instantiate the fragmentManager and set the default view to profile
currentFragment = new ProfileFragment();
currentFragment.setFloatingActionButton(fab);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame ,new ProfileFragment())
.replace(R.id.content_frame ,(Fragment) currentFragment)
.commit();
}

Expand Down Expand Up @@ -106,6 +112,7 @@ public boolean onNavigationItemSelected(MenuItem item) {
currentFragment = new ProfileFragment();
}

currentFragment.setFloatingActionButton(fab);
fragmentManager.beginTransaction()
.replace(R.id.content_frame, (Fragment) currentFragment)
.commit();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.thesis.smukov.anative.NavigationFragment;

import android.app.Fragment;
import android.support.design.widget.FloatingActionButton;
import android.view.View;


public abstract class BaseNavigationFragment extends Fragment
implements INavigationFragment{

protected View myView;
protected FloatingActionButton fab;

@Override
public void setFloatingActionButton(FloatingActionButton fab) {
this.fab = fab;
}

protected abstract void prepareFloatingActionButton();
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.thesis.smukov.anative;
package com.thesis.smukov.anative.NavigationFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.thesis.smukov.anative.interfaces.INavigationFragment;
import com.thesis.smukov.anative.R;

/**
* Created by smukov on 18-Jun-16.
*/
public class ContactFragment extends Fragment
implements INavigationFragment {
public class ContactFragment extends BaseNavigationFragment {

View myView;
TextView profileName;

@Nullable
Expand All @@ -30,12 +29,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

prepareFloatingActionButton();

profileName = (TextView) view.findViewById(R.id.profile_name);
profileName.setText("Dr. Gregory House");
}

@Override
public void handleFabAction(int actionId) {

protected void prepareFloatingActionButton(){
fab.show();
fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_forum_black_24dp));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Called from Contact Fragment", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thesis.smukov.anative.NavigationFragment;

import android.support.design.widget.FloatingActionButton;

/**
* Created by smuko on 18-Jun-16.
*/
public interface INavigationFragment {
void setFloatingActionButton(FloatingActionButton fab);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.thesis.smukov.anative;
package com.thesis.smukov.anative.NavigationFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.thesis.smukov.anative.interfaces.INavigationFragment;
import com.thesis.smukov.anative.R;

/**
* Created by smukov on 15-Jun-16.
*/
public class ProfileFragment extends Fragment
implements INavigationFragment {
public class ProfileFragment extends BaseNavigationFragment {

View myView;
TextView profileName;

@Nullable
Expand All @@ -30,12 +29,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

prepareFloatingActionButton();

profileName = (TextView) view.findViewById(R.id.profile_name);
profileName.setText("Dr. Gregory House");
}

@Override
public void handleFabAction(int actionId) {

protected void prepareFloatingActionButton(){
fab.show();
fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_create_black_24dp));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Called from Profile Fragment", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.thesis.smukov.anative;
package com.thesis.smukov.anative.NavigationFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.thesis.smukov.anative.interfaces.INavigationFragment;
import com.thesis.smukov.anative.R;

/**
* Created by smuko on 15-Jun-16.
*/
public class SettingsFragment extends Fragment
implements INavigationFragment {

View myView;
public class SettingsFragment extends BaseNavigationFragment {

@Nullable
@Override
Expand All @@ -25,7 +21,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
}

@Override
public void handleFabAction(int actionId) {
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

prepareFloatingActionButton();
}

@Override
protected void prepareFloatingActionButton(){
fab.hide();
}
}

This file was deleted.

0 comments on commit 51d5e98

Please sign in to comment.