资源简介
仿抖音流的图片浏览app,可以不断下滑加载高兴图。预加载屏幕下方的图片。点击可看大图,可保存到本地。
@BindContentView(R.layout.fragment_recycler) | |
class UnsplashPhotosFragment : BaseFragment(), UnsplashPhotosItemFactory.UnsplashPhotosItemEventListener, OnLoadMoreListener, androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener { | |
private var adapter: AssemblyRecyclerAdapter? = null | |
private var pageIndex = 1 | |
private var backgroundImageUri: String? = null | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
recycler_recyclerFragment_content.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context) | |
refresh_recyclerFragment.setOnRefreshListener(this) | |
if (adapter != null) { | |
recycler_recyclerFragment_content.adapter = adapter | |
} else { | |
refresh_recyclerFragment.post { onRefresh() } | |
} | |
} | |
override fun onUserVisibleChanged(isVisibleToUser: Boolean) { | |
if (isVisibleToUser) { | |
changeBackground(backgroundImageUri) | |
} | |
} | |
private fun changeBackground(imageUri: String?) { | |
this.backgroundImageUri = imageUri | |
backgroundImageUri?.let { EventBus.getDefault().post(ChangeMainPageBgEvent(it)) } | |
} | |
private fun loadData(pageIndex: Int) { | |
this.pageIndex = pageIndex | |
NetServices.unsplash().listPhotos(pageIndex).enqueue(LoadDataCallback(this, pageIndex)) | |
} | |
override fun onClickImage(position: Int, image: UnsplashImage, optionsKey: String) { | |
val activity = activity ?: return | |
var finalOptionsKey: String? = optionsKey | |
// 含有这些信息时,说明这张图片不仅仅是缩小,而是会被改变,因此不能用作loading图了 | |
if (finalOptionsKey!!.contains("Resize") | |
|| finalOptionsKey.contains("ImageProcessor") | |
|| finalOptionsKey.contains("thumbnailMode")) { | |
finalOptionsKey = null | |
} | |
@Suppress("UNCHECKED_CAST") | |
val images = adapter!!.dataList as List<UnsplashImage> | |
val imageArrayList = ArrayList<Image>(images.size) | |
images.mapTo(imageArrayList) { Image(it.urls!!.regular!!, it.urls!!.raw!!) } | |
ImageDetailActivity.launch(activity, dataTransferHelper.put("urlList", imageArrayList), finalOptionsKey!!, position) | |
} | |
override fun onClickUser(position: Int, user: UnsplashImage.User) { | |
val uri = Uri.parse(user.links!!.html) | |
.buildUpon() | |
.appendQueryParameter("utm_source", "SketchSample") | |
.appendQueryParameter("utm_medium", "referral") | |
.appendQueryParameter("utm_campaign", "api-credit") | |
.build() | |
val intent = Intent(Intent.ACTION_VIEW) | |
intent.data = uri | |
startActivity(intent) | |
} | |
override fun onRefresh() { | |
if (adapter != null) { | |
adapter!!.loadMoreFinished(false) | |
} | |
if (!refresh_recyclerFragment.isRefreshing) { | |
refresh_recyclerFragment.isRefreshing = true | |
} | |
loadData(1) | |
} | |
override fun onLoadMore(adapter1: AssemblyAdapter) { | |
loadData(pageIndex 1) | |
} | |
private class LoadDataCallback internal constructor(fragment: UnsplashPhotosFragment, private val pageIndex: Int) : Callback<List<UnsplashImage>> { | |
private val reference: WeakReference<UnsplashPhotosFragment> = WeakReference(fragment) | |
init { | |
if (pageIndex == 1) { | |
fragment.hint_recyclerFragment.hidden() | |
} | |
} | |
override fun onResponse(call: Call<List<UnsplashImage>>, response: Response<List<UnsplashImage>>) { | |
val fragment = reference.get() ?: return | |
if (!fragment.isViewCreated) { | |
return | |
} | |
if (pageIndex == 1) { | |
create(fragment, response) | |
} else { | |
loadMore(fragment, response) | |
} | |
fragment.refresh_recyclerFragment.isRefreshing = false | |
} | |
override fun onFailure(call: Call<List<UnsplashImage>>, t: Throwable) { | |
val fragment = reference.get() ?: return | |
val activity = fragment.activity ?: return | |
if (!fragment.isViewCreated) { | |
return | |
} | |
if (pageIndex == 1) { | |
fragment.hint_recyclerFragment.failed(t, View.OnClickListener { fragment.onRefresh() }) | |
fragment.refresh_recyclerFragment.isRefreshing = false | |
} else { | |
fragment.adapter!!.loadMoreFailed() | |
Toast.makeText(fragment.activity, HintView.getCauseByException(activity, t), Toast.LENGTH_LONG).show() | |
} | |
} | |
private fun create(fragment: UnsplashPhotosFragment, response: Response<List<UnsplashImage>>) { | |
val activity = fragment.activity ?: return | |
val images = response.body() | |
if (images == null || images.isEmpty()) { | |
fragment.hint_recyclerFragment.empty(
代码片段和文件信息
属性 大小 日期 时间 名称 ----------- --------- ---------- ----- ---- 目录 0 2019-01-17 05:43 sketch-master\ 文件 334 2019-01-17 05:43 sketch-master\.gitignore 文件 67561 2019-01-17 05:43 sketch-master\CHANGELOG.md 文件 37713 2019-01-17 05:43 sketch-master\CHANGELOG_P_B.md 文件 13327 2019-01-17 05:43 sketch-master\LICENSE.txt 文件 10483 2019-01-17 05:43 sketch-master\README.md 文件 11127 2019-01-17 05:43 sketch-master\README_EN.md 文件 903 2019-01-17 05:43 sketch-master\build.gradle 文件 276 2019-01-17 05:43 sketch-master\build_kotlin.gradle 目录 0 2019-01-17 05:43 sketch-master\docs\ 目录 0 2019-01-17 05:43 sketch-master\docs\res\ 文件 493524 2019-01-17 05:43 sketch-master\docs\res\apps.png 文件 135400 2019-01-17 05:43 sketch-master\docs\res\gif_flag.png 文件 4623 2019-01-17 05:43 sketch-master\docs\res\logo.png 文件 48594 2019-01-17 05:43 sketch-master\docs\res\logo_source.jpg 文件 48793 2019-01-17 05:43 sketch-master\docs\res\sample_debug_mode.jpeg 文件 1896495 2019-01-17 05:43 sketch-master\docs\sketch-sample.apk 文件 2334 2019-01-17 05:43 sketch-master\docs\sketch-sample.png 目录 0 2019-01-17 05:43 sketch-master\docs\wiki\ 文件 4164 2019-01-17 05:43 sketch-master\docs\wiki\bitmap_pool.md 文件 5260 2019-01-17 05:43 sketch-master\docs\wiki\block_display.md 文件 1581 2019-01-17 05:43 sketch-master\docs\wiki\cache_processed_image_in_disk.md 文件 1747 2019-01-17 05:43 sketch-master\docs\wiki\cancel_request.md 文件 2426 2019-01-17 05:43 sketch-master\docs\wiki\correct_image_orientation.md 文件 2207 2019-01-17 05:43 sketch-master\docs\wiki\disk_cache.md 文件 1669 2019-01-17 05:43 sketch-master\docs\wiki\display_apk_or_app_icon.md 文件 2293 2019-01-17 05:43 sketch-master\docs\wiki\display_video_thumbnail.md 文件 1296 2019-01-17 05:43 sketch-master\docs\wiki\error_tracker.md 文件 1130 2019-01-17 05:43 sketch-master\docs\wiki\http_stack.md 文件 3585 2019-01-17 05:43 sketch-master\docs\wiki\image_displa 文件 3700 2019-01-17 05:43 sketch-master\docs\wiki\image_processor.md ............此处省略624个文件信息 相关资源
|
评论
共有 条评论