Skip to content

Commit

Permalink
修复搜索详情页点击崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
cannon-lz committed Sep 3, 2016
1 parent e37a011 commit a76f155
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ dependencies {
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'io.reactivex:rxandroid:1.2.1'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/zhangly/zmenu/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.app.Application;
import android.content.Context;

import com.squareup.leakcanary.LeakCanary;

/**
* Created by zhangluya on 16/8/15.
*/
Expand All @@ -14,7 +16,7 @@ public class App extends Application {
@Override
public void onCreate() {
super.onCreate();

LeakCanary.install(this);
sApp = this;
sContext = getApplicationContext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getChildFragmentManager().popBackStackImmediate();
getFragmentManager().popBackStackImmediate();
}
});
return view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
final InputMethodManager inputMethodManager = (InputMethodManager) textView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
String keyword = textView.getText().toString();
final MenuListFragment menuListFragment = MenuListFragment.newFragment(keyword, true);
final MenuListFragment menuListFragment = MenuListFragment.newFragment(keyword, true, true);
new MenusPresenter(menuListFragment, keyword);
startFragment(menuListFragment, null, null);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.SharedElementCallback;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.transition.ChangeBounds;
import android.transition.ChangeImageTransform;
import android.transition.ChangeTransform;
Expand All @@ -30,6 +32,7 @@
import com.zhangly.zmenu.adapter.MenusAdapter;
import com.zhangly.zmenu.api.bean.Menus;
import com.zhangly.zmenu.module.menudetails.MenuDetailsFragment;
import com.zhangly.zmenu.utils.StatusBarUtil;
import com.zhangly.zmenu.widget.BaseRecyclerView;

import java.util.List;
Expand All @@ -49,20 +52,40 @@ public class MenuListFragment extends BaseFragment implements MenusContract.View

private MenusContract.Presenter mPresenter;

public static MenuListFragment newFragment(String menu, boolean isHome) {
public static MenuListFragment newFragment(String menu, boolean isHome, boolean isAddToolbar) {
Bundle bundle = new Bundle();
bundle.putString("menu", menu);
bundle.putBoolean("flag", isHome);
bundle.putBoolean("isAddToolbar", isAddToolbar);
MenuListFragment menuListFragment = new MenuListFragment();
menuListFragment.setArguments(bundle);

return menuListFragment;
}

public static MenuListFragment newFragment(String menu, boolean isHome) {
return newFragment(menu, isHome, false);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_menu_list, container, false);
final boolean isAddToolbar = getArguments().getBoolean("isAddToolbar");
int targetLayout = isAddToolbar ? R.layout.fragment_menu_list_toolbar : R.layout.fragment_menu_list;
final View view = inflater.inflate(targetLayout, container, false);
if (isAddToolbar) {
StatusBarUtil.fixStatusBar(view);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.id_tl_toolbar);
getHostActivity().setSupportActionBar(toolbar);
getHostActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getHostActivity().getSupportActionBar().setTitle(getArguments().getString("menu"));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStackImmediate();
}
});
}
return view;
}

private int[] getSomeVisibleItemPosition(RecyclerView recyclerView) {
Expand Down Expand Up @@ -109,13 +132,14 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
@Override
public void onItemClick(int position, BaseRecyclerView.BaseViewHolder holder, Menus.Menu item) {
final MenuDetailsFragment fragment = MenuDetailsFragment.newFragment(item);
Fragment thisFragment = getParentFragment() == null ? MenuListFragment.this : getParentFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getParentFragment().setSharedElementReturnTransition(new FragmentHelper.DetailTransition());
thisFragment.setExitTransition(new Slide());
fragment.setEnterTransition(new Slide());
fragment.setSharedElementEnterTransition(new FragmentHelper.DetailTransition());
startFragment(getParentFragment() == null ? MenuListFragment.this : getParentFragment(), fragment, holder.getImageView(R.id.id_iv_menu_pic), "MenuPic");
startFragment(thisFragment, fragment, holder.getImageView(R.id.id_iv_menu_pic), "MenuPic");
} else {
startFragment(getParentFragment() == null ? MenuListFragment.this : getParentFragment(), fragment, null ,null);
startFragment(thisFragment, fragment, null ,null);
}
}
});
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/fragment_menu_list_toolbar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/id_tl_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize" />

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/id_sr_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.zhangly.zmenu.widget.BaseRecyclerView
android:id="@+id/id_rcl_menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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


</LinearLayout>
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
Expand All @@ -16,6 +17,7 @@ buildscript {

allprojects {
repositories {
mavenCentral()
jcenter()
}
}
Expand Down

0 comments on commit a76f155

Please sign in to comment.