Search in sources :

Example 1 with SectionProvider

use of com.xxf.view.recyclerview.itemdecorations.section.SectionProvider in project xxf_android by NBXXF.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Throwable {
        }
    });
    Observable.defer(new Supplier<ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> get() throws Throwable {
            Log.d("========>测试 开始执行重试:", "" + pageIndex);
            return Observable.just(pageIndex).map(new Function<Integer, Integer>() {

                @Override
                public Integer apply(Integer integer) throws Throwable {
                    if (integer < 3) {
                        Log.d("========>测试 异常:", "" + integer);
                        throw new RuntimeException("");
                    }
                    return integer;
                }
            });
        }
    }).retry(3, new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable throwable) throws Throwable {
            if (throwable instanceof RuntimeException) {
                Log.d("========>测试  重试:", "" + System.currentTimeMillis());
                pageIndex += 1;
                return true;
            }
            return false;
        }
    }).subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer integer) throws Throwable {
            Log.d("========>测试 结果:", "" + integer);
        }
    });
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    binding.change.setText("normal");
    binding.change.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(v.getContext(), NormalRecyclerViewActivity.class).putExtra("xx", "77463"));
        }
    });
    setContentView(binding.getRoot());
    System.out.println("==========>onChildViewAttachedToWindow  init");
    // adapter.setHasStableIds(true);
    binding.recyclerView.setAdapter(adapter);
    // 删除-> 改变焦点(上一个)  后删除当前
    // binding.recyclerView.find
    // 创建第五条
    binding.recyclerView.setEdgeEffectFactory(new EdgeSpringEffectFactory(0.5f, 0.5f));
    binding.recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {

        @Override
        public void onChildViewAttachedToWindow(@NonNull View view) {
            RecyclerView.ViewHolder containingViewHolder = binding.recyclerView.findContainingViewHolder(view);
            if (containingViewHolder.getAdapterPosition() == 5) {
                // requestfoucs
                binding.recyclerView.removeOnChildAttachStateChangeListener(this);
            }
            System.out.println("==========>onChildViewAttachedToWindow:AdapterPosition:" + containingViewHolder.getAdapterPosition() + "  LayoutPosition:" + containingViewHolder.getLayoutPosition() + "  hash:" + containingViewHolder.itemView);
        }

        @Override
        public void onChildViewDetachedFromWindow(@NonNull View view) {
        }
    });
    sectionMap.clear();
    sectionMap.put(0, "第一个分组");
    sectionMap.put(3, "第2个分组");
    Paint paint = new Paint();
    paint.setTextSize(DensityUtil.sp2px(13));
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    Paint background = new Paint();
    background.setColor(Color.WHITE);
    binding.recyclerView.addItemDecoration(new SectionItemDecoration(new SectionProvider() {

        @NotNull
        @Override
        public TreeMap<Integer, String> onProvideSection() {
            return sectionMap;
        }
    }, paint, background, DensityUtil.sp2px(30), DensityUtil.sp2px(16)));
    List<String> list = new ArrayList<>();
    int count = new Random().nextInt(50);
    for (int i = 0; i < count; i++) {
        // list.add("i" + new Random().nextInt(100));
        list.add("i" + i);
    }
    adapter.bindData(true, list);
    binding.refresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<String> list = new ArrayList<>();
            int count = new Random().nextInt(50);
            for (int i = 0; i < count; i++) {
                // list.add("i" + new Random().nextInt(100));
                list.add("i" + i);
            }
            adapter.bindData(true, list);
            Log.d("=======>list:", "" + list);
        }
    });
    binding.insert.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            adapter.addItem(0, "hello");
        }
    });
    binding.insertLast.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            adapter.addItem("hello foo");
        }
    });
    binding.delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            adapter.removeItem(0);
        }
    });
    binding.loadMore.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<String> list = new ArrayList<>();
            int count = new Random().nextInt(50);
            for (int i = 0; i < count; i++) {
                // list.add("i" + new Random().nextInt(100));
                list.add("i" + i);
            }
            adapter.addItems(list);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Predicate(io.reactivex.rxjava3.functions.Predicate) SectionProvider(com.xxf.view.recyclerview.itemdecorations.section.SectionProvider) Function(io.reactivex.rxjava3.functions.Function) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) Intent(android.content.Intent) Paint(android.graphics.Paint) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) EdgeSpringEffectFactory(com.xxf.view.recyclerview.adapter.EdgeSpringEffectFactory) Paint(android.graphics.Paint) ObservableSource(io.reactivex.rxjava3.core.ObservableSource) RecyclerView(androidx.recyclerview.widget.RecyclerView) SectionItemDecoration(com.xxf.view.recyclerview.itemdecorations.section.SectionItemDecoration)

Aggregations

Intent (android.content.Intent)1 Paint (android.graphics.Paint)1 View (android.view.View)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 EdgeSpringEffectFactory (com.xxf.view.recyclerview.adapter.EdgeSpringEffectFactory)1 SectionItemDecoration (com.xxf.view.recyclerview.itemdecorations.section.SectionItemDecoration)1 SectionProvider (com.xxf.view.recyclerview.itemdecorations.section.SectionProvider)1 ObservableSource (io.reactivex.rxjava3.core.ObservableSource)1 Function (io.reactivex.rxjava3.functions.Function)1 Predicate (io.reactivex.rxjava3.functions.Predicate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1