-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[M]优化内存占用,解决资源回收问题 & update README.md
- Loading branch information
Showing
11 changed files
with
92 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,8 @@ | |
import androidx.annotation.NonNull; | ||
import androidx.viewpager.widget.PagerAdapter; | ||
|
||
import com.hitomi.tilibrary.view.video.ExoVideoView; | ||
import com.hitomi.tilibrary.view.image.TransferImage; | ||
import com.hitomi.tilibrary.view.video.ExoVideoView; | ||
|
||
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; | ||
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; | ||
|
@@ -25,7 +25,6 @@ | |
* email: [email protected] | ||
*/ | ||
class TransferAdapter extends PagerAdapter { | ||
|
||
private TransferLayout transfer; | ||
private int showIndex; | ||
private int imageSize; | ||
|
@@ -38,8 +37,7 @@ class TransferAdapter extends PagerAdapter { | |
this.imageSize = imageSize; | ||
this.showIndex = nowThumbnailIndex + 1 == imageSize | ||
? nowThumbnailIndex - 1 : nowThumbnailIndex + 1; | ||
this.showIndex = showIndex < 0 ? 0 : showIndex; | ||
|
||
this.showIndex = Math.max(showIndex, 0); | ||
containLayoutArray = new SparseArray<>(); | ||
} | ||
|
||
|
@@ -56,63 +54,24 @@ public boolean isViewFromObject(View view, Object object) { | |
@Override | ||
public void destroyItem(ViewGroup container, int position, Object object) { | ||
container.removeView((View) object); | ||
} | ||
|
||
/** | ||
* 获取指定索引页面中的 TransferImage | ||
* | ||
* @param position | ||
* @return | ||
*/ | ||
TransferImage getImageItem(int position) { | ||
FrameLayout parentLayout = containLayoutArray.get(position); | ||
if (parentLayout != null && parentLayout.getChildAt(0) instanceof TransferImage) { | ||
return ((TransferImage) parentLayout.getChildAt(0)); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 获取指定页面中的 ExoVideoView | ||
* | ||
* @param position | ||
* @return | ||
*/ | ||
ExoVideoView getVideoItem(int position) { | ||
FrameLayout parentLayout = containLayoutArray.get(position); | ||
if (parentLayout != null && parentLayout.getChildAt(0) instanceof ExoVideoView) { | ||
return ((ExoVideoView) parentLayout.getChildAt(0)); | ||
} | ||
return null; | ||
} | ||
|
||
SparseArray<FrameLayout> getCacheItems() { | ||
return containLayoutArray; | ||
} | ||
|
||
FrameLayout getParentItem(int position) { | ||
return containLayoutArray.get(position); | ||
} | ||
|
||
void setOnInstantListener(OnInstantiateItemListener listener) { | ||
this.onInstantListener = listener; | ||
containLayoutArray.remove(position); | ||
transfer.loadedIndexSet.remove(position); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Object instantiateItem(@NonNull ViewGroup container, int position) { | ||
// ViewPager instantiateItem 顺序:按 position 递减 OffscreenPageLimit, | ||
// 再从 position 递增 OffscreenPageLimit 的次序创建页面 | ||
FrameLayout parentLayout = containLayoutArray.get(position); | ||
|
||
FrameLayout parentLayout = containLayoutArray.get(position); | ||
if (parentLayout == null) { | ||
parentLayout = newParentLayout(container, position); | ||
containLayoutArray.put(position, parentLayout); | ||
containLayoutArray.append(position, parentLayout); | ||
|
||
if (position == showIndex && onInstantListener != null) | ||
onInstantListener.onComplete(); | ||
} | ||
|
||
container.addView(parentLayout); | ||
return parentLayout; | ||
} | ||
|
@@ -147,6 +106,46 @@ private FrameLayout newParentLayout(ViewGroup container, final int pos) { | |
return parentLayout; | ||
} | ||
|
||
/** | ||
* 获取指定索引页面中的 TransferImage | ||
* | ||
* @param position | ||
* @return | ||
*/ | ||
TransferImage getImageItem(int position) { | ||
FrameLayout parentLayout = containLayoutArray.get(position); | ||
if (parentLayout != null && parentLayout.getChildAt(0) instanceof TransferImage) { | ||
return ((TransferImage) parentLayout.getChildAt(0)); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 获取指定页面中的 ExoVideoView | ||
* | ||
* @param position | ||
* @return | ||
*/ | ||
ExoVideoView getVideoItem(int position) { | ||
FrameLayout parentLayout = containLayoutArray.get(position); | ||
if (parentLayout != null && parentLayout.getChildAt(0) instanceof ExoVideoView) { | ||
return ((ExoVideoView) parentLayout.getChildAt(0)); | ||
} | ||
return null; | ||
} | ||
|
||
SparseArray<FrameLayout> getCacheItems() { | ||
return containLayoutArray; | ||
} | ||
|
||
FrameLayout getParentItem(int position) { | ||
return containLayoutArray.get(position); | ||
} | ||
|
||
void setOnInstantListener(OnInstantiateItemListener listener) { | ||
this.onInstantListener = listener; | ||
} | ||
|
||
interface OnInstantiateItemListener { | ||
void onComplete(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.