利用XrecycleView写多条目展示+流式布局

首先我们先引入我们所需要的依赖

//流失布局需要的依赖
implementation 'com.hyman:flowlayout-lib:1.1.2'
    implementation 'com.android.support:design:28.0.0'
    //recycler的依赖
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//okgo
    implementation 'com.lzy.net:okgo:3.0.4'
//Glide加载图片
    implementation 'com.github.bumptech.glide:glide:3.7.0'
	//解析数据
    implementation 'org.kie.modules:com-google-code-gson:6.5.0.Final'
    //xrecycle
    implementation 'com.jcodecraeer:xrecyclerview:1.5.9'

如果build报错在最外层gradle加入

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }    //////加入
    }
}

其次是清单文件写权限

//联网权限
 
    //写
    
   //读
    

首页显示的流式布局 xml布局

 
    

需要写一个圆角的text条目



下方是显示多条目的布局

 

下方就是recyleView的多条目的三种布局
第一种布局


       
       
   
    

第二种布局



    

    
        
        
        

    
    


第三种布局



    
    


主页activity显示

public class MainActivity extends AppCompatActivity {
    //设置标题
    private String[] mVals={"三星旗舰店","欧莱雅男士洁面乳","乐高积木","雀氏纸尿裤","惠玲奶粉","苹果手机","美团外卖","饿了么"};
    private TagFlowLayout flowlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取id
        flowlayout = findViewById(R.id.id_flowlayout);
            //设置引入流式布局
        flowlayout.setAdapter(new TagAdapter(mVals) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                //加入你的布局
                TextView tv = (TextView) View.inflate(MainActivity.this, R.layout.tv, null);
                tv.setText(s);
                return tv;
            }
        });
        //点击流式布局跳转到下个界面
        flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                return true;
            }
        });
    }
}

recyleView的多条目展示avtivity

public class Main2Activity extends AppCompatActivity implements ShowConstant.ShowView {

    private ShowConstant.ShowPresenter presenterImpi;
    private XRecyclerView recycle1;
    private List list;
    private int page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        recycle1 = (XRecyclerView) findViewById(R.id.recycle1);
        presenterImpi = new ShowPresenterImpi();
        presenterImpi.attachView(this);
        presenterImpi.requestData(page);
        //设置加载
        recycle1.setLoadingMoreEnabled(true);
        //设置刷新
        recycle1.setPullRefreshEnabled(true);
        //监听
        recycle1.setLoadingListener(new XRecyclerView.LoadingListener() {
            //设置刷新
            @Override
            public void onRefresh() {
                list.clear();
                presenterImpi.requestData(page);
            }
            //设置加载更多
            @Override
            public void onLoadMore() {
                page++;
                presenterImpi.requestData(page);
            }
        });
    }

    @Override
    public void showData(String responseData) {
        //gson解析
        Gson gson=new Gson();
        JsonBean jsonBean = gson.fromJson(responseData, JsonBean.class);
        List data = jsonBean.getData();
        //集合
        list = new ArrayList<>();
        list.addAll(data);
        //布局管理器
        LinearLayoutManager manager= new LinearLayoutManager(Main2Activity.this, LinearLayoutManager.VERTICAL, false);
        recycle1.setLayoutManager(manager);
        //设置适配器
        final ShowAdapter showAdapter = new ShowAdapter(Main2Activity.this, list);
        recycle1.setAdapter(showAdapter);
        //设置刷新加载完成
        recycle1.refreshComplete();
        recycle1.loadMoreComplete();
        //长安弹出框 确定就删除  取消就吐司
        showAdapter.setOnItemLongClickListener(new ShowAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClickListener(final View v, final int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Main2Activity.this);
                builder.setTitle("不再关注");
                builder.setMessage("确定取消关注此类新闻吗?");
				/////确定删除
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    //删除的动画可加可不加
                        final ObjectAnimator alpha = ObjectAnimator.ofFloat(v, "alpha", 1f, 0f);
                        ObjectAnimator translationX = ObjectAnimator.ofFloat(v, "translationX", 5, -500);
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(2000);
                        animatorSet.playTogether(alpha,translationX);
                        //
                        animatorSet.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                            }
                            @Override
                            public void onAnimationEnd(Animator animation) {
                            /////这行是删除 的 加刷新
                                list.remove(position);
                                showAdapter.notifyItemRemoved(position);
                            }
                            @Override
                            public void onAnimationCancel(Animator animation) {
                            }
                            @Override
                            public void onAnimationRepeat(Animator animation) {
                            }
                        });
                        animatorSet.start();

                    }
                });
                /////到这里都是动画的代码 不加的话可以不要, 可以直接删除
                //取消吐司提示
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(Main2Activity.this, "您已取消", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                builder.show();

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenterImpi.detachView(this);
    }
}

最后在写入设配器

public class ShowAdapter extends RecyclerView.Adapter {
    private static final int TYPE_ONE=1;
    private static final int TYPE_TWO=2;
    private static final int TYPE_THREE=3;
    private Context context;
    private List list;

    //定义接口
    OnItemLongClickListener onItemLongClickListener;
    public interface OnItemLongClickListener{
        public void onItemLongClickListener(View v,int position);
    }
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
//构造方法
    public ShowAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List lists) {
        if (list!=null){
            this.list = lists;
        }
        notifyDataSetChanged();

    }
    public void addList(List lists){
        if (list!=null){
            list.addAll(lists);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    ///判断加载布局
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i==TYPE_ONE){
            View view = LayoutInflater.from(context).inflate(R.layout.recycle1, viewGroup, false);
            return new ViewHolderOne(view);
        }else if (i==TYPE_TWO){
            View view = LayoutInflater.from(context).inflate(R.layout.recycle2, viewGroup, false);
            return new ViewHolderTwo(view);
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.recycle3, viewGroup, false);
            return new ViewHolderThree(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        //获取第几条
        int itemViewType = getItemViewType(i);
        switch (itemViewType){
            //第一种条目设置
            case TYPE_ONE:
                ViewHolderOne viewHolderOne= (ViewHolderOne) viewHolder;
                viewHolderOne.text_name.setText(list.get(i).getTitle());
                viewHolderOne.text_time.setText(list.get(i).getDate());
                String imagesString = list.get(i).getThumbnail_pic_s();
                String[] imagesStr = imagesString.split("\\|");
                Glide.with(context).load(imagesStr[0]).into(viewHolderOne.image_icon);
                break;
                //第二种条目设置
            case TYPE_TWO:
                ViewHolderTwo viewHolderTwo= (ViewHolderTwo) viewHolder;
                viewHolderTwo.text_name.setText(list.get(i).getTitle());
                viewHolderTwo.text_time.setText(list.get(i).getDate());
                Glide.with(context).load(list.get(i).getThumbnail_pic_s()).into(viewHolderTwo.image_icon1);
                Glide.with(context).load(list.get(i).getThumbnail_pic_s02()).into(viewHolderTwo.image_icon2);
                Glide.with(context).load(list.get(i).getThumbnail_pic_s03()).into(viewHolderTwo.image_icon3);
                break;
                //第三种条目设置
            case TYPE_THREE:
                ViewHolderThree viewHolderThree= (ViewHolderThree) viewHolder;
                viewHolderThree.text_name.setText(list.get(i).getTitle());
                viewHolderThree.text_time.setText(list.get(i).getDate());
                break;
        }
        //点击触发的事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemLongClickListener.onItemLongClickListener(v,i);
            }
        });

    }
    //总数
    @Override
    public int getItemCount() {
        return list.size();
    }
    //判断等于几时加载那种布局
    @Override
    public int getItemViewType(int position) {
        if (position%3==1){
            return TYPE_ONE;
        }else if (position%3==2){
            return TYPE_TWO;
        }else {
            return TYPE_THREE;
        }
    }
    //设置第一种条目的设配器
    class ViewHolderOne extends RecyclerView.ViewHolder {
        //获取id
        private final TextView text_name;
        private final TextView text_time;
        private final ImageView image_icon;

        public ViewHolderOne(@NonNull View itemView) {
            super(itemView);
            text_name = itemView.findViewById(R.id.text_name);
            text_time = itemView.findViewById(R.id.text_time);
            image_icon = itemView.findViewById(R.id.image_icon);
        }
    }
    //第二种的条目
    class ViewHolderTwo extends RecyclerView.ViewHolder {

        private final TextView text_name;
        private final TextView text_time;
        private final ImageView image_icon1;
        private final ImageView image_icon2;
        private final ImageView image_icon3;

        public ViewHolderTwo(@NonNull View itemView) {
            super(itemView);
            ////获取id
            text_name = itemView.findViewById(R.id.text_name_recycle2);
            text_time = itemView.findViewById(R.id.text_time_recycle2);
            image_icon1 = itemView.findViewById(R.id.image_icon_recycle2_1);
            image_icon2 = itemView.findViewById(R.id.image_icon_recycle2_2);
            image_icon3 = itemView.findViewById(R.id.image_icon_recycle2_3);
        }
    }
    //第三种的条目数
    class ViewHolderThree extends RecyclerView.ViewHolder {

        private final TextView text_name;
        private final TextView text_time;

        public ViewHolderThree(@NonNull View itemView) {
            super(itemView);
            //获取id
            text_name = itemView.findViewById(R.id.text_name_recycle3);
            text_time = itemView.findViewById(R.id.text_time_recycle3);
        }
    }

}

接下来看一下效果图

利用XrecycleView写多条目展示+流式布局_第1张图片

你可能感兴趣的:(Android,APP开发)