Skip to content

Commit

Permalink
优化了懒加载
Browse files Browse the repository at this point in the history
  • Loading branch information
liang02.wang committed Jan 8, 2020
1 parent c17ae00 commit 3a7e13b
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions core/src/main/java/me/liam/support/SupportFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</activity>
<activity android:name="wechat.WeChatActivity"/>
<activity android:name="zhihu.ZhiHuActivity"/>
<activity android:name="viewpager.ViewPagerActivity"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
7 changes: 7 additions & 0 deletions demo/src/main/java/me/liam/fragmentation/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
});
}
}
65 changes: 65 additions & 0 deletions demo/src/main/java/viewpager/ItemPageFragment.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
22 changes: 22 additions & 0 deletions demo/src/main/java/viewpager/ViewPagerActivity.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
71 changes: 71 additions & 0 deletions demo/src/main/java/viewpager/ViewPagerFragment.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
5 changes: 5 additions & 0 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ZHI HU App"/>
<Button
android:id="@+id/btn_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View pager"/>
</LinearLayout>
9 changes: 9 additions & 0 deletions demo/src/main/res/layout/activity_vp_root.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
11 changes: 11 additions & 0 deletions demo/src/main/res/layout/fragment_item_page.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:gravity="center"
android:textSize="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
13 changes: 13 additions & 0 deletions demo/src/main/res/layout/fragment_vp_root.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.viewpager.widget.ViewPager>
</LinearLayout>

0 comments on commit 3a7e13b

Please sign in to comment.