专高二 day7_RecyclerView 新闻模块

 

RecyclerView.Adapter适配器
package com.bw.day7_recyclerview.adapter;

import android.content.Context;
import android.content.pm.LabeledIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bw.day7_recyclerview.R;
import com.bw.day7_recyclerview.entity.Bean;

import java.util.List;

public class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.RvViewHolder>{

    Context context;
    List<Bean> beanList;

    public MyRVAdapter(Context context, List<Bean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    @NonNull
    @Override
    public RvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RvViewHolder(LayoutInflater.from(context).inflate(R.layout.item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RvViewHolder holder, int position) {
        holder.ImageView.setImageResource(beanList.get(position).getSrcid());
        holder.textView.setText(beanList.get(position).getName());
    }

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

    public class RvViewHolder extends RecyclerView.ViewHolder{
        ImageView ImageView;
        TextView textView;
        public RvViewHolder(@NonNull View itemView) {
            super(itemView);
            this.ImageView=itemView.findViewById(R.id.iv);
            this.textView=itemView.findViewById(R.id.tv);


        }
    }
}

Activity
package com.bw.day7_recyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.bw.day7_recyclerview.adapter.MyRVAdapter;
import com.bw.day7_recyclerview.entity.Bean;
import com.bw.day7_recyclerview.entity.FoodEntity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView re;
    List<Bean> beanList;
    MyRVAdapter myRVAdapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        re = (RecyclerView) findViewById(R.id.re);
        //准备数据源
        beanList=new ArrayList<>();
        beanList.add(new Bean(R.drawable.shou1,"首页"));
        beanList.add(new Bean(R.drawable.fen,"分类"));
        beanList.add(new Bean(R.drawable.gou,"购物车"));
        beanList.add(new Bean(R.drawable.wo,"我的"));
        beanList.add(new Bean(R.drawable.guang,"逛逛"));

        //定义适配器
        myRVAdapter = new MyRVAdapter(this, beanList);
        //设置适配器
        re.setAdapter(myRVAdapter);
        re.setLayoutManager(new LinearLayoutManager(this));









    }
}

瀑布流

package com.bw.day7_recyclerview.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bw.day7_recyclerview.R;
import com.bw.day7_recyclerview.entity.FoodEntity;

import java.util.List;

public class FoodRvAdapter extends RecyclerView.Adapter<FoodRvAdapter.ViewHolder>{

    Context context;
    List<FoodEntity.DataDTO> list;
    int type=1;

    public FoodRvAdapter(Context context, List<FoodEntity.DataDTO> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(type==0){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item, null);
            ViewHolder viewHolder = new ViewHolder(inflate);
            viewHolder.textView=inflate.findViewById(R.id.tv);
            viewHolder.imageView=inflate.findViewById(R.id.iv);
            return viewHolder;
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item2, null);
            ViewHolder viewHolder = new ViewHolder(inflate);
            viewHolder.textView=inflate.findViewById(R.id.tv2);
            viewHolder.imageView=inflate.findViewById(R.id.iv2);
            return viewHolder;
        }


    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        type=list.get(position).getType();
        holder.textView.setText(list.get(position).getTitle());
        Glide.with(context).load(list.get(position).getPic())
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_launcher_background)
                .placeholder(R.drawable.ic_launcher_foreground)
                .thumbnail(0.1f)//缩略图
                .transform(new RoundedCorners(20))//圆角
                .into(holder.imageView);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
//            this.imageView = itemView.findViewById(R.id.iv2);
//            this.textView = itemView.findViewById(R.id.tv2);
        }
    }




}
package com.bw.day7_recyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.bw.day7_recyclerview.adapter.FoodRvAdapter;
import com.bw.day7_recyclerview.entity.FoodEntity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

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

public class MainActivity2 extends AppCompatActivity {

    private RecyclerView rv2;
    List<FoodEntity.DataDTO> foofList=new ArrayList<>();
    FoodRvAdapter foodRvAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        rv2 = (RecyclerView) findViewById(R.id.rv2);
        //准备数据源
        OkGo.<String>get("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=10").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i("------json------",response.body());
                List<FoodEntity.DataDTO> data = new Gson().fromJson(response.body(), FoodEntity.class).getData();
                if(data.size()==0&&data==null){
                    Toast.makeText(MainActivity2.this, "没有东西了", Toast.LENGTH_SHORT).show();
                }else {
                    foofList.addAll(data);
                    for (int i = 0; i <foofList.size(); i++) {
                        int type=(i%2==0)?0:1;
                        foofList.get(i).setType(type);
                    }




                    foodRvAdapter.notifyDataSetChanged();
                }

            }
        });
        //准备适配器
        foodRvAdapter=new FoodRvAdapter(MainActivity2.this,foofList);
        //设置设配器
        rv2.setAdapter(foodRvAdapter);
        rv2.setLayoutManager(new LinearLayoutManager(MainActivity2.this));
    }
}

点击事件  接口回调

第一步定义接口

适配器里

public interface OnItemClickListener{
        void onItemClick(int position,View view);
    }

第二步 声明接口变量

 设置set  get方法

适配器里

//声明接口变量
    OnItemClickListener onItemClickListener;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public FoodRvAdapter(Context context, List<FoodEntity.DataDTO> list) {
        this.context = context;
        this.list = list;
    }

     

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(position, view);

            }
        });

activity中

foodRvAdapter.setOnItemClickListener(new FoodRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                Toast.makeText(MainActivity2.this, "aaa"+foofList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            }
        });

刷新加载更多

package com.bw.day7_recyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bw.day7_recyclerview.adapter.FoodRvAdapter;
import com.bw.day7_recyclerview.entity.FoodEntity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

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

public class MainActivity2 extends AppCompatActivity {

    private RecyclerView rv2;
    List<FoodEntity.DataDTO> foofList=new ArrayList<>();
    FoodRvAdapter foodRvAdapter;

    SmartRefreshLayout sw;

    int page=1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        rv2 = (RecyclerView) findViewById(R.id.rv2);
        sw=findViewById(R.id.sw);

        //加载更多
        sw.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                add();
                refreshLayout.finishLoadMore();

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                add();
                refreshLayout.finishRefresh();
            }
        });



        //准备数据源
        add();
        //准备适配器
        foodRvAdapter=new FoodRvAdapter(MainActivity2.this,foofList);
        //设置设配器
        rv2.setAdapter(foodRvAdapter);
        rv2.setLayoutManager(new LinearLayoutManager(MainActivity2.this));

        foodRvAdapter.setOnItemClickListener(new FoodRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                Toast.makeText(MainActivity2.this, "aaa"+foofList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            }
        });


    }


    public void add(){
        OkGo.<String>get("http://43.143.146.165:7777/foods/getFoods?currentPage="+page+"&pageSize=10").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i("------json------",response.body());
                List<FoodEntity.DataDTO> data = new Gson().fromJson(response.body(), FoodEntity.class).getData();
                if(data.size()==0&&data==null){
                    Toast.makeText(MainActivity2.this, "没有东西了", Toast.LENGTH_SHORT).show();
                }else {
                    foofList.addAll(data);
                    for (int i = 0; i <foofList.size(); i++) {
                        int type=(i%2==0)?0:1;
                        foofList.get(i).setType(type);
                    }
                    foodRvAdapter.notifyDataSetChanged();
                }

            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值