美文网首页
使用RecycleView自己实现Indicator

使用RecycleView自己实现Indicator

作者: Android百晓生 | 来源:发表于2020-03-11 16:48 被阅读0次

    以下代码为自己实现indicator切换tab效果,实现同一页面改变Lis显示(非page切换):

    package com.example.xp.ui.home.adapter;

    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;

    import com.example.xp.R;

    import java.util.ArrayList;
    import java.util.List;

    /**

    • 推荐页面视频分类tab
      */
      public class RecommendTypeAdapter extends RecyclerView.Adapter<RecommendTypeAdapter.TypeViewHolder> {
      private OnTypeSelectListener mOnTypeSelectListener;

      private List<String> mRecommendType = new ArrayList<>();
      private List<Boolean> isClickList = new ArrayList<>();

      public void refreshData(List<String> recommendType){
      this.mRecommendType.clear();
      this.mRecommendType.addAll(recommendType);
      notifyDataSetChanged();
      }

      public RecommendTypeAdapter(List<String> mRecommendType) {
      this.mRecommendType = mRecommendType;
      for (int i = 0; i < mRecommendType.size(); i++) {
      isClickList.add(false);
      }
      }

      @NonNull
      @Override
      public TypeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
      View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recommend_type, viewGroup, false);
      return new TypeViewHolder(view);
      }

      @Override
      public void onBindViewHolder(@NonNull TypeViewHolder holder, int position) {
      holder.tvType.setText(mRecommendType.get(position));
      if (isClickList.get(position)){
      holder.viewBg.setVisibility(View.VISIBLE);
      }else {
      holder.viewBg.setVisibility(View.INVISIBLE);
      }
      holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      for (int i = 0; i < isClickList.size(); i++) {
      isClickList.set(i, false);
      }
      isClickList.set(position, true);
      notifyDataSetChanged();
      if (mOnTypeSelectListener != null){
      mOnTypeSelectListener.typeSelect(mRecommendType.get(position));
      }
      }
      });
      }

      @Override
      public int getItemCount() {
      return mRecommendType.size();
      }

      class TypeViewHolder extends RecyclerView.ViewHolder{
      TextView tvType;
      View viewBg;
      public TypeViewHolder(@NonNull View itemView) {
      super(itemView);
      tvType = itemView.findViewById(R.id.tv_item_type);
      viewBg = itemView.findViewById(R.id.view_bottom_mark);
      }
      }

      public interface OnTypeSelectListener{
      void typeSelect(String videoType);
      }

      public void setOnTypeSelectListener(OnTypeSelectListener onTypeSelectListener){
      this.mOnTypeSelectListener = onTypeSelectListener;
      }
      }

    相关文章

      网友评论

          本文标题:使用RecycleView自己实现Indicator

          本文链接:https://www.haomeiwen.com/subject/foqhjhtx.html