diff --git a/.idea/misc.xml b/.idea/misc.xml index b0c7b20..e0d5b93 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/core/src/main/java/me/liam/support/SupportFragment.java b/core/src/main/java/me/liam/support/SupportFragment.java index e2ca66b..a423246 100644 --- a/core/src/main/java/me/liam/support/SupportFragment.java +++ b/core/src/main/java/me/liam/support/SupportFragment.java @@ -75,16 +75,16 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { if (isSavedInstance()){ resumeAnim(); } - if (!getArguments().getBoolean(SupportTransaction.FRAGMENTATION_INIT_LIST) - && !isHidden()){ - onLazyInit(savedInstanceState); - } } @Override public void onResume() { super.onResume(); getArguments().putBoolean(SupportTransaction.FRAGMENTATION_SAVED_INSTANCE,false); + if (!getArguments().getBoolean(SupportTransaction.FRAGMENTATION_INIT_LIST) + && !isHidden()){ + onLazyInit(null); + } } @Nullable diff --git a/demo/build.gradle b/demo/build.gradle index 015812e..e37b1c8 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -25,7 +25,7 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.3.0-alpha03' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha03' - // implementation 'com.github.Liam6666:FragmentationX:1.0.0-beta02' +// implementation 'com.github.Liam6666:FragmentationX:1.0.1' implementation 'com.eightbitlab:bottomnavigationbar:0.9.6' implementation project(':core') implementation 'com.google.android.material:material:1.0.0' diff --git a/demo/src/main/AndroidManifest.xml b/demo/src/main/AndroidManifest.xml index e95e4c0..b22e627 100644 --- a/demo/src/main/AndroidManifest.xml +++ b/demo/src/main/AndroidManifest.xml @@ -18,6 +18,7 @@ + \ No newline at end of file diff --git a/demo/src/main/java/me/liam/fragmentation/MainActivity.java b/demo/src/main/java/me/liam/fragmentation/MainActivity.java index 2c21be7..6316b16 100644 --- a/demo/src/main/java/me/liam/fragmentation/MainActivity.java +++ b/demo/src/main/java/me/liam/fragmentation/MainActivity.java @@ -7,6 +7,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import me.liam.support.SupportActivity; +import viewpager.ViewPagerActivity; import wechat.WeChatActivity; import zhihu.ZhiHuActivity; @@ -33,5 +34,11 @@ public void onClick(View view) { startActivity(new Intent(MainActivity.this, ZhiHuActivity.class)); } }); + findViewById(R.id.btn_vp).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + startActivity(new Intent(MainActivity.this, ViewPagerActivity.class)); + } + }); } } diff --git a/demo/src/main/java/viewpager/ItemPageFragment.java b/demo/src/main/java/viewpager/ItemPageFragment.java new file mode 100644 index 0000000..663fc3b --- /dev/null +++ b/demo/src/main/java/viewpager/ItemPageFragment.java @@ -0,0 +1,65 @@ +package viewpager; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import me.liam.fragmentation.R; +import me.liam.support.SupportFragment; + +/** + * Create on 2020/1/8. + */ +public class ItemPageFragment extends SupportFragment { + + public static ItemPageFragment newInstance(int count) { + + Bundle args = new Bundle(); + args.putInt("count",count); + ItemPageFragment fragment = new ItemPageFragment(); + fragment.setArguments(args); + return fragment; + } + + private View rootView; + private TextView textView; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + rootView = View.inflate(getContext(), R.layout.fragment_item_page,null); + textView = rootView.findViewById(R.id.tv); + textView.setText(""+getArguments().getInt("count")); + Log.e("ItemPageFragment",getArguments().getInt("count") + "onCreateView"); + return rootView; + } + + @Override + public void onLazyInit(Bundle savedInstanceState) { + super.onLazyInit(savedInstanceState); + Log.e("ItemPageFragment",getArguments().getInt("count") + "onLazyInit"); + } + + @Override + public void setUserVisibleHint(boolean isVisibleToUser) { + super.setUserVisibleHint(isVisibleToUser); + Log.e("ItemPageFragment",getArguments().getInt("count") + "setUserVisibleHint" + isVisibleToUser); + } + + @Override + public void onStart() { + super.onStart(); + Log.e("ItemPageFragment",getArguments().getInt("count") + "onStart"); + } + + @Override + public void onResume() { + super.onResume(); + Log.e("ItemPageFragment",getArguments().getInt("count") + "onResume"); + } +} diff --git a/demo/src/main/java/viewpager/ViewPagerActivity.java b/demo/src/main/java/viewpager/ViewPagerActivity.java new file mode 100644 index 0000000..138856c --- /dev/null +++ b/demo/src/main/java/viewpager/ViewPagerActivity.java @@ -0,0 +1,22 @@ +package viewpager; + +import android.os.Bundle; + +import androidx.annotation.Nullable; +import me.liam.fragmentation.R; +import me.liam.support.SupportActivity; + +/** + * Create on 2020/1/8. + */ +public class ViewPagerActivity extends SupportActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_vp_root); + if (findFragmentByClass(ViewPagerFragment.class) == null){ + loadRootFragment(R.id.container,ViewPagerFragment.newInstance()); + } + } +} diff --git a/demo/src/main/java/viewpager/ViewPagerFragment.java b/demo/src/main/java/viewpager/ViewPagerFragment.java new file mode 100644 index 0000000..2d8c251 --- /dev/null +++ b/demo/src/main/java/viewpager/ViewPagerFragment.java @@ -0,0 +1,71 @@ +package viewpager; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.google.android.material.tabs.TabLayout; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.ViewPager; +import me.liam.fragmentation.R; +import me.liam.support.SupportFragment; + +/** + * Create on 2020/1/8. + */ +public class ViewPagerFragment extends SupportFragment { + + public static ViewPagerFragment newInstance() { + + Bundle args = new Bundle(); + + ViewPagerFragment fragment = new ViewPagerFragment(); + fragment.setArguments(args); + return fragment; + } + + private View rootView; + private TabLayout tabLayout; + private ViewPager viewPager; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + rootView = View.inflate(getContext(), R.layout.fragment_vp_root,null); + tabLayout = rootView.findViewById(R.id.tabLayout); + viewPager = rootView.findViewById(R.id.viewPager); + tabLayout.addTab(tabLayout.newTab().setText("1")); + tabLayout.addTab(tabLayout.newTab().setText("2")); + tabLayout.addTab(tabLayout.newTab().setText("3")); + tabLayout.addTab(tabLayout.newTab().setText("4")); + tabLayout.addTab(tabLayout.newTab().setText("5")); + PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); + viewPager.setAdapter(adapter); + tabLayout.setupWithViewPager(viewPager); + return rootView; + } + + static class PagerAdapter extends FragmentPagerAdapter { + + public PagerAdapter(@NonNull FragmentManager fm, int behavior) { + super(fm, behavior); + } + + @NonNull + @Override + public Fragment getItem(int position) { + return ItemPageFragment.newInstance(position); + } + + @Override + public int getCount() { + return 5; + } + } +} diff --git a/demo/src/main/res/layout/activity_main.xml b/demo/src/main/res/layout/activity_main.xml index 472fa78..e973078 100644 --- a/demo/src/main/res/layout/activity_main.xml +++ b/demo/src/main/res/layout/activity_main.xml @@ -21,4 +21,9 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ZHI HU App"/> +