2017-09-08 1 views
0

私は、特定のイベントが発生したときにスクロールしたい項目のリストを持つrecyclerViewを持っています。私はsmoothScrollToPositionを使用していますが、驚いたことに、滑らかではないだけでなく、実際にスクロールする前に元の位置に戻す必要があるように、ちらつき効果があります。効果はここで見ることができます:smoothScrollToPositionがRecyclerViewで点滅しています

http://i.imgur.com/rrpLr7N.mp4

はこの正常な動作ですか?

アダプターコード:

@ActivityScope 
class HighlightListAdapter @Inject constructor() : RecyclerView.Adapter<HighlightListAdapter.ViewHolder>() { 

private var highlights: List<Highlight> = emptyList() 
private var itemClick: ((Highlight, Int) -> Unit)? = null 

private var selectedRow: MutableList<Int> = mutableListOf() 

override fun onBindViewHolder(holder: ViewHolder, position: Int) { 

    val binding = holder.binding 
    val highlight = highlights[position] 
    var viewModel = binding.viewModel 

    viewModel?.unbind() 

    viewModel = HighlightViewModel(highlight) 
    binding.viewModel = viewModel 
    viewModel.bind() 

    if(selectedRow.contains(position)) { 
     binding.rootItemView.alpha = 1.0f 
    } 
    else { 
     binding.rootItemView.alpha = 0.5f 
    } 

    holder.setClickListener(itemClick) 
} 

override fun getItemCount(): Int = highlights.size 

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { 
    val binding = DataBindingUtil.inflate<ItemHighlightListBinding>(
      LayoutInflater.from(parent.context), 
      R.layout.item_highlight_list, 
      parent, 
      false 
    ) 
    return ViewHolder(binding) 
} 

fun updateEvents(highlights: List<Highlight>) { 
    this.highlights = highlights 
    notifyDataSetChanged() 
} 


fun setClickListener(itemClick: ((Highlight, Int) -> Unit)?) { 
    this.itemClick = itemClick 
} 


fun enableRow(index: Int) { 
    //Clear previous selection (only if we want single selection) 
    selectedRow.clear() 
    //Select specified row 
    selectedRow.add(index) 
    //Let the adapter redraw 
    notifyDataSetChanged() 
} 

class ViewHolder(val binding: ItemHighlightListBinding) : RecyclerView.ViewHolder(binding.root) { 

    fun setClickListener(callback: ((Highlight, Int) -> Unit)?) { 
     binding.viewModel.clicks().subscribe { 
      callback?.invoke(binding.viewModel.highlight, adapterPosition) 
     } 
    } 

} 

fun getSelected() = selectedRow 

} 

レイアウトファイル:

<data> 
    <variable 
     name="viewModel" 
     type="tv.mycujoo.mycujooplayer.ui.video.highlights.HighlightViewModel" /> 
</data> 



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:id="@+id/root_item_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical" 
        android:alpha="0.5" 
        android:orientation="horizontal" 
        android:clickable="true" 
        android:onClick="@{() -> viewModel.onClick()}" 
        android:background="@color/dark_black" 
        android:padding="@dimen/single_padding" 
        > 


     <TextView 
      android:layout_width="48dp" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:gravity="right" 
      android:text="@{viewModel.time}" 
      android:textColor="@color/light_gray" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
      android:layout_marginRight="@dimen/single_padding" 
      style="@style/Base.TextAppearance.AppCompat.Title" 
      tools:text="122''"/> 

     <ImageView 
      android:background="@drawable/bg_circle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/single_padding" 
      android:padding="@dimen/single_padding" 
      tools:src="@mipmap/ic_launcher_round" 
      app:imageResource="@{viewModel.image}"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 

       android:textColor="@color/light_gray" 
       tools:text="@{viewModel.name}"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="@color/light_gray" 
       tools:text="@{viewModel.team}"/> 
     </LinearLayout> 


     <ImageView 
      android:visibility="gone" 
      android:layout_width="48dp" 
      android:layout_height="48dp" 
      android:src="@drawable/tv_avatar_default" 
      /> 

    </LinearLayout> 
</layout> 
+0

投稿コード... –

+0

アダプターコード – Anonymous

+0

を投稿してください。 –

答えて

1

I)は、(notifyDataSetChanged問題は内の行を有効にあると思うそれはフリックで結果データセット全体をリフレッシュします。リストに追加してください。これをお試しください。

fun enableRow(index: Int) { 
//Clear previous selection (only if we want single selection) 
selectedRow.clear() 
//Select specified row 
selectedRow.add(index) 
//Let the adapter redraw 
notifyItemRangeChanged(index,highlights.size()) 
} 
+0

これは何とか修正されているようですが、問題はすべて1行しか選択しないようにするためです選択手段のUIが選択された行と異なる) –

+0

私はあなたをはっきりと見分けることができますが、これが役に立ったと感じたら、回答をupvoteしてください。notifyDataSetChanged()とnotifyItemRangeChanged()はデータリストを更新するものとほとんど同じです – Anonymous

+0

は、リストから選択した項目が別の色で表示されます。したがって、新しいイベントが選択されるたびに(そしてスクロールされるたびに)、他のアイテムはリフレッシュされる必要があるため、選択されていないものとして表示されます。私があなたの言うことをするならば、私は選択されたものから始まる範囲のアイテムだけをリフレッシュするので、以前に選択されたアイテムが選択されたままになります。 –

関連する問題