代码之家  ›  专栏  ›  技术社区  ›  Ahmed S. Durrani

如何使不同的视图成为循环视图?

  •  0
  • Ahmed S. Durrani  · 技术社区  · 8 年前

    我一直在寻找 recyclerview 轮转,以便第一个项目再次填充时,最后一个项目是可见的,它继续显示一次又一次。现在我知道这个答案已经被回答了 here here 但这两个答案并没有解决我的问题。虽然项目数量增加了,但是在recyclerview中作为项目的片段没有显示任何内容。

    我在膨胀 fragments 作为项目 RecyclerView 嵌套在 ViewPager . 公认的答案并不能解决我的问题。

    RecycleServiceAdapterClass类

    fun initialize(scrollableData: ArrayList<ScrollableData>, activity: AppCompatActivity) {
            this.scrollableData = scrollableData
            this.activity = activity
        }
    
        override fun getItemCount() = scrollableData.size
    
        override fun getItemViewType(position: Int): Int {
            return scrollableData[position].index
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder {
    
            when (viewType) {
                ScrollableItems.ABC -> {
                    val view = parent.inflate(R.layout.view_module_abc, false)
                    return ABCViewHolder(view, abcPresenterImpl, activity)
                }
    
                ScrollableItems.DEF -> {
                    val view = parent.inflate(R.layout.view_module_def, false)
                    return DEFViewHolder(view, defControlPresenter, activity)
                }
    
                else -> {
                    val view = parent.inflate(R.layout.view_module_ghi, false)
                    return GHIViewHolder(view, GHIModulePresenter, activity)
                }
            }
        }
    
        override fun onBindViewHolder(holder: BaseHolder, position: Int) {
            holder.bindView()
        }
    

    private fun setupRecyclerView(scrollableDataList: ArrayList<ScrollableData>) {
            linearLayoutManager = CustomLinearLayoutManager(applicationContext, LinearLayout.HORIZONTAL, !DeviceConfig.instance!!.isRegularDevice)
            recycler_view.layoutManager = linearLayoutManager
            recycler_view.adapter = mainScreenAdapter
    
            mainScreenAdapter.initialize(scrollableDataList, this)
            recycler_view.setHasFixedSize(true);
            GravityPagerSnapHelper(if (DeviceConfig.instance!!.isRegularDevice) Gravity.START else Gravity.END, true, this).attachToRecyclerView(recycler_view)
            recyclerViewScrollListener()
        }
    

    和 CustomLinearLayoutManager公司

    class CustomLinearLayoutManager(context: Context, orientation: Int, reverseLayout: Boolean)
        : LinearLayoutManager(context, orientation, reverseLayout) {
    
        var isScrollEnabled = true
    
        override fun canScrollHorizontally(): Boolean {
            return isScrollEnabled && super.canScrollHorizontally()
        }
    
        override fun canScrollVertically(): Boolean {
            return isScrollEnabled && super.canScrollVertically()
        }
    }
    

    请帮我解决这个问题。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Ahmed S. Durrani    8 年前

    this 解决方案。

    所以我就是这么做的

    RecycleServiceAdapterClass类

      fun initialize(scrollableData: ArrayList<ScrollableData>, activity: AppCompatActivity) {
                this.scrollableData = scrollableData
                this.activity = activity
                list = /*listOf(scrollableData.last()) +*/ scrollableData + listOf(scrollableData.first()) //this is very important
                }
        ...
          override fun getItemViewType(position: Int): Int {
            return list[position %  list.size].index
        }
    

    mainScreenAdapter.itemCount.takeIf { it > 1 }
                    ?.apply {
                        onScrollListener = OnScrollListener(
                                mainScreenAdapter.itemCount,
                                linearLayoutManager
                        ) {
                           //some code here
                        }
                        recycler_view.addOnScrollListener(onScrollListener)
                        recycler_view.scrollToPosition(0)
                    }
    

    internal class OnScrollListener(val itemCount: Int, val layoutManager: LinearLayoutManager,
                                        val stateChanged: (Int) -> Unit) : RecyclerView.OnScrollListener() {
            var isLooping = false
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView!!, dx, dy)
                val firstItemVisible = layoutManager.findFirstCompletelyVisibleItemPosition()
    
                if (firstItemVisible > 0 && firstItemVisible % (itemCount - 1) == 0) {
                    // When position reaches end of the list, it sho    uld go back to the beginning
                    isLooping = true
                    recyclerView?.scrollToPosition(0)
                } /*else if (firstItemVisible == 0) {
                    // When position reaches beginning of the list, it should go back to the end
                    recyclerView?.scrollToPosition(itemCount-1)
                }*/
            }
    
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView!!, newState)
                var loop = newState
                if (isLooping) {
                    loop = 100
                    isLooping = false
                }
                stateChanged(loop)
            }
        }
    

    裁判: https://github.com/tomoima525/CirculerAutoScrollingRecyclerView