Skip to content

Commit

Permalink
Merge pull request #54 from Aspsine/dev
Browse files Browse the repository at this point in the history
Demo use FragmentNavigator
  • Loading branch information
Aspsine committed Apr 27, 2016
2 parents 3169b8a + 9a37c1d commit da30fcb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 72 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

repositories {
maven { url "https://jitpack.io" }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile project(':library')

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
Expand All @@ -31,4 +36,5 @@ dependencies {
compile 'com.mcxiaoke.volley:library-aar:1.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.lsjwzh:materialloadingprogressbar:0.5.8-RELEASE'
compile 'com.github.Aspsine:FragmentNavigator:1.0.2'
}
115 changes: 43 additions & 72 deletions app/src/main/java/com/aspsine/swipetoloadlayout/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,117 +3,88 @@

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.aspsine.swipetoloadlayout.demo.fragment.BaseNavigationFragment;
import com.aspsine.fragmentnavigator.FragmentNavigator;
import com.aspsine.swipetoloadlayout.demo.fragment.BaseToolbarFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavGoogleFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavJDFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavJavaCodeFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavTwitterFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavYalantisFragment;

public class MainActivity extends AppCompatActivity implements BaseToolbarFragment.ToggleDrawerCallBack
, NavigationView.OnNavigationItemSelectedListener {

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
, BaseToolbarFragment.ToggleDrawerCallBack {

private static final Integer ID_ARRAY[] = {
R.id.nav_Twitter_style,
R.id.nav_google_style,
R.id.nav_yalantis_style,
R.id.nav_jd_style,
R.id.nav_set_header_footer_via_code
};

private static final List<Integer> IDS = Arrays.asList(ID_ARRAY);

private static final int DEFAULT_POSITION = 0;

private DrawerLayout drawerLayout;
private Handler handler;

/**
* https://github.com/Aspsine/FragmentNavigator
*/
private FragmentNavigator mFragmentNavigator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mFragmentNavigator = new FragmentNavigator(getSupportFragmentManager(), new MainFragmentAdapter(), R.id.container);

mFragmentNavigator.setDefaultPosition(DEFAULT_POSITION);

mFragmentNavigator.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);

navigationView.setNavigationItemSelectedListener(this);

handler = new Handler(Looper.getMainLooper());
navigationView.setCheckedItem(IDS.get(DEFAULT_POSITION));

mFragmentNavigator.showFragment(mFragmentNavigator.getCurrentPosition());
}

if (savedInstanceState == null) {
navigationView.setCheckedItem(R.id.nav_Twitter_style);
showNavigationFragment(R.id.nav_Twitter_style);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mFragmentNavigator.onSaveInstanceState(outState);
}

@Override
public void openDrawer() {
drawerLayout.openDrawer(GravityCompat.START);
}

int lastNavItemId = -1;

@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
drawerLayout.closeDrawer(GravityCompat.START);
handler.postDelayed(new Runnable() {
drawerLayout.postDelayed(new Runnable() {
@Override
public void run() {
int itemId = menuItem.getItemId();
if (itemId == R.id.nav_about) {
startActivity(new Intent(MainActivity.this, AboutActivity.class));
} else {
showNavigationFragment(itemId);
mFragmentNavigator.showFragment(IDS.indexOf(itemId));
}
}
}, 200);
return true;
}

void showNavigationFragment(int itemId) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment lastFragment = fm.findFragmentByTag(getTag(lastNavItemId));
lastNavItemId = itemId;
if (lastFragment != null) {
ft.detach(lastFragment);
}
Fragment fragment = fm.findFragmentByTag(getTag(itemId));
if (fragment == null) {
fragment = getItem(itemId);
ft.add(R.id.container, fragment, getTag(itemId));
} else {
ft.attach(fragment);
}
ft.commit();
}

private BaseNavigationFragment getItem(int itemId) {
BaseNavigationFragment navigationFragment = new BaseNavigationFragment();

switch (itemId) {
case R.id.nav_Twitter_style:
navigationFragment = NavTwitterFragment.newInstance();
break;

case R.id.nav_google_style:
navigationFragment = NavGoogleFragment.newInstance();
break;

case R.id.nav_yalantis_style:
navigationFragment = NavYalantisFragment.newInstance();
break;

case R.id.nav_jd_style:
navigationFragment = NavJDFragment.newInstance();
break;
case R.id.nav_set_header_footer_via_code:
navigationFragment = NavJavaCodeFragment.newInstance();
break;
}
return navigationFragment;
}

private String getTag(int itemId) {
return String.valueOf(itemId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.aspsine.swipetoloadlayout.demo;

import android.support.v4.app.Fragment;

import com.aspsine.fragmentnavigator.FragmentNavigatorAdapter;
import com.aspsine.swipetoloadlayout.demo.fragment.NavGoogleFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavJDFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavJavaCodeFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavTwitterFragment;
import com.aspsine.swipetoloadlayout.demo.fragment.NavYalantisFragment;

/**
* Created by aspsine on 16/4/28.
*/
public class MainFragmentAdapter implements FragmentNavigatorAdapter {

@Override
public Fragment onCreateFragment(int position) {
switch (position) {
case 0:
return NavTwitterFragment.newInstance();

case 1:
return NavGoogleFragment.newInstance();

case 2:
return NavYalantisFragment.newInstance();

case 3:
return NavJDFragment.newInstance();

case 4:
return NavJavaCodeFragment.newInstance();
}
return null;
}

@Override
public String getTag(int position) {
return String.valueOf(position);
}

@Override
public int getCount() {
return 5;
}
}
Binary file modified art/demo.apk
Binary file not shown.

0 comments on commit da30fcb

Please sign in to comment.