Android开发记录(九)-Flexbox和RecyclerView实现流式布局

通过Flexbox实现流式布局,效果:

Android开发记录(九)-Flexbox和RecyclerView实现流式布局_第1张图片
  1. RecyclerView结合FlexboxLayoutManager实现流式布局

        FlexboxLayoutManager manager=new FlexboxLayoutManager(getContext());
        //设置主轴排列形式
        manager.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        manager.setFlexWrap(FlexWrap.WRAP);
        binding.navRv.setLayoutManager(manager);
        navListAdapter = new NavListAdapter(navResponseList,getContext());
        binding.navRv.setAdapter(navListAdapter);
  1. 通过getItemViewType重写,实现RecyclerView二级列表展示,根据type区分不同View展示数据

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.jetpack.demo.R;
import com.jetpack.demo.databinding.NavItemBinding;
import com.jetpack.demo.databinding.NavSuperItemBinding;
import com.jetpack.demo.databinding.TopItemBinding;
import com.jetpack.demo.model.home.TopResponse;
import com.jetpack.demo.model.nav.NavResponse;

public class NavListAdapter extends RecyclerView.Adapter {
    private List navResponses;
    private Context mContext;
    private int TYPE_SUPER = 0;
    private int TYPE_CHILD = 1;
    public NavListAdapter(List navResponses, Context mContext) {
        this.navResponses = navResponses;
        this.mContext = mContext;
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == TYPE_CHILD){
            NavItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.nav_item,parent,false);
            return new MyViewHolder(binding.getRoot());
        }else {
            NavSuperItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.nav_super_item,parent,false);
            return new MyViewHolder(binding.getRoot());
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
        NavResponse.Articles articles = navResponses.get(position);
        if (articles.chapterName == null){
            NavSuperItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
            binding.tvNav.setText(navResponses.get(position).title);
            binding.executePendingBindings();
        }else {
            NavItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
            binding.tvNav.setText(navResponses.get(position).title);
            binding.executePendingBindings();
        }
    }

    @Override
    public int getItemViewType(int position) {
        NavResponse.Articles articles = navResponses.get(position);
        return articles.chapterName == null?TYPE_SUPER:TYPE_CHILD;
    }

    @Override
    public int getItemCount() {
        return navResponses.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }
    }
}

你可能感兴趣的:(retrofit)