RecyclerView(多种样式+网络图片加载)

1.样式文件

activity_main(主页面文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/refreshLayout">
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recy2"
            >
        </androidx.recyclerview.widget.RecyclerView>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>

news_item_one文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="9dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="212dp"
            android:layout_height="63dp"
            android:text="《天外世界:果根险境》9月9日上架 游戏将支持Xbox Series X专属强化"
            android:id="@+id/title"
            android:layout_weight="1"
            android:textColor="@color/black"
            ></TextView>
        <ImageView
            android:layout_width="108dp"
            android:layout_height="53dp"
            android:id="@+id/thumb"
            android:scaleType="fitXY"
            android:src="@mipmap/news_bg"
            ></ImageView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        >
        <ImageView
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:id="@+id/header"
            ></ImageView>
        <TextView
            android:layout_width="71dp"
            android:layout_height="17dp"
            android:text="游戏早知道 ."
            android:textColor="#777777"
            android:textSize="13sp"
            android:gravity="left"
            android:id="@+id/author"
            />
        <TextView
            android:layout_width="39dp"
            android:layout_height="17dp"
            android:text="1评论 ."
            android:textColor="#777777"
            android:textSize="13sp"
            android:gravity="left"
            android:id="@+id/comment"
            />
        <TextView
            android:layout_width="46dp"
            android:layout_height="17dp"
            android:text="1小时前"
            android:textColor="#777777"
            android:textSize="13sp"
            android:gravity="left"
            android:id="@+id/time"
            />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f5f5f5"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        ></View>
</LinearLayout>

news_item_two文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CS+吃鸡+守望先锋,真正的大杂烩!"
        android:textColor="#222222"
        android:textSize="16sp"
        android:gravity="left"
        android:id="@+id/title"
        android:layout_margin="10dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <ImageView
            android:layout_width="116dp"
            android:layout_height="78dp"
            android:layout_weight="1"
            android:id="@+id/pic1"
            android:src="@mipmap/news_bg"
            ></ImageView>
        <ImageView
            android:layout_width="116dp"
            android:layout_height="78dp"
            android:layout_weight="1"
            android:id="@+id/pic2"
            android:src="@mipmap/news_bg"
            ></ImageView>
        <ImageView
            android:layout_width="116dp"
            android:layout_height="78dp"
            android:layout_weight="1"
            android:id="@+id/pic3"
            android:src="@mipmap/news_bg"
            ></ImageView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        >
        <ImageView
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:id="@+id/header"
            ></ImageView>
        <TextView
            android:layout_width="71dp"
            android:layout_height="17dp"
            android:text="游戏早知道 ."
            android:textColor="#777777"
            android:textSize="13sp"
            android:id="@+id/author"
            android:gravity="left"
            />
        <TextView
            android:layout_width="39dp"
            android:layout_height="17dp"
            android:text="1评论 ."
            android:textColor="#777777"
            android:textSize="13sp"
            android:gravity="left"
            android:id="@+id/comment"
            />
        <TextView
            android:layout_width="46dp"
            android:layout_height="17dp"
            android:text="1小时前"
            android:textColor="#777777"
            android:textSize="13sp"
            android:id="@+id/time"
            android:gravity="left"
            />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f5f5f5"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="10dp"
        ></View>
</LinearLayout>

news_item_three

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CS+吃鸡+守望先锋,真正的大杂烩!"
            android:textColor="#222222"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/thumb"
            android:layout_width="match_parent"
            android:layout_height="190dp"
            android:layout_marginTop="7dp"
            android:scaleType="fitXY"
            android:src="@mipmap/news_bg"
            />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="9dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/header"
                android:layout_width="16dp"
                android:layout_height="16dp"
                android:scaleType="fitXY"
                />

            <TextView
                android:id="@+id/author"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="9dp"
                android:text="游戏早知道 ."
                android:textColor="#777777"
                android:textSize="13sp" />

            <TextView
                android:id="@+id/comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="9dp"
                android:text="1评论 ."
                android:textColor="#777777"
                android:textSize="13sp" />

            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="9dp"
                android:text="1小时前"
                android:textColor="#777777"
                android:textSize="13sp" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="#f5f5f5" />
</RelativeLayout>

2.适配器

package com.example.recymorestyleapplication.adapter;
import android.content.Context;
import android.util.Log;
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.example.recymorestyleapplication.R;
import com.example.recymorestyleapplication.view.CircleTransform;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *首页视频适配器
 **/
public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<String> list;
    private String TAG="";
    public void setDatas(List<String> datas){
        this.list=datas;
    }

    //构造器只需要传入界面上下文就行
    public NewsAdapter(Context context){
        this.context=context;
        this.list=list;
    }

    public NewsAdapter(Context context, List<String> list){
        this.context=context;
        this.list=list;
    }

    //可以实现多种不同布局的
//    position  数据下标
    @Override
    public int getItemViewType(int position) {
        int a =position%3+1;
        return a;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==1){
            ViewHolderOne viewHolderOne;
            View view= LayoutInflater.from(context).inflate(R.layout.news_item_one,parent,false);
            viewHolderOne=new ViewHolderOne(view);
             return viewHolderOne;
        }else if(viewType==2){
            ViewHolderTwo viewHolderTwo;
            View view= LayoutInflater.from(context).inflate(R.layout.news_item_two,parent,false);
            viewHolderTwo=new ViewHolderTwo(view);
            return viewHolderTwo;
        }else {
                ViewHolderThree viewHolderThree;
                View view= LayoutInflater.from(context).inflate(R.layout.news_item_three,parent,false);
             viewHolderThree=new ViewHolderThree(view);
            return viewHolderThree;
        }
    }

    //绑定数据用的 给布局渲染数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = position%3+1;
        Log.d(TAG, "onBindViewHolder: 下标----"+type);
        if (type==1){
            ViewHolderOne vho=(ViewHolderOne) holder;
            String author="作者1";
            String title="标题1";
            String comment="新闻内容";
            String time="2020-08-08";
            String headerUrl="http://crawl.ws.126.net/img/4ea8924e432bc4067a1d2f8bb90e8793.jpg";
            vho.author.setText(author);
            vho.comment.setText(comment);
            vho.time.setText(time);
            vho.title.setText(title);
            Picasso.get()
                    .load(headerUrl)
                    .transform( new CircleTransform())//圆头像
                    .into( vho.header);
            Picasso.get()
                    .load(headerUrl)
                    .into( vho.thumb);

        }else if(type==2){
            ViewHolderTwo vho=(ViewHolderTwo) holder;
            String author="作者1";
            String title="标题1";
            String comment="新闻内容";
            String time="2020-08-08";
            String headerUrl="http://crawl.ws.126.net/img/4ea8924e432bc4067a1d2f8bb90e8793.jpg";
            vho.author.setText(author);
            vho.comment.setText(comment);
            vho.time.setText(time);
            vho.title.setText(title);
            Picasso.get()
                    .load(headerUrl)
                    .transform(new CircleTransform())//圆头像
                    .into( vho.header);
            Picasso.get()
                    .load(headerUrl)
                    .into( vho.pic1);
            Picasso.get()
                    .load(headerUrl)
                    .into( vho.pic2);
            Picasso.get()
                    .load(headerUrl)
                    .into( vho.pic3);
        }else {
            ViewHolderThree vho=(ViewHolderThree) holder;
            String author="作者1";
            String title="标题1";
            String comment="新闻内容";
            String time="2020-08-08";
            String headerUrl="http://crawl.ws.126.net/img/4ea8924e432bc4067a1d2f8bb90e8793.jpg";
            vho.author.setText(author);
            vho.comment.setText(comment);
            vho.time.setText(time);
            vho.title.setText(title);
            Picasso.get()
                    .load(headerUrl)
                    .transform(new CircleTransform())//圆头像
                    .into( vho.header);
            Picasso.get()
                    .load(headerUrl)
                    .into( vho.thumb);
        }
    }


    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }



    //    当前多少个数据
    @Override
    public int getItemCount() {
        if(list!=null&&list.size()>0){
            Log.d(TAG, "getItemCount: 数量"+list.size());
            return list.size();
        }else{
            return 0;
        }
    }
    public class ViewHolderOne extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView author;
        private TextView comment;
        private TextView time;
        private ImageView header;
        private ImageView thumb;
        private Map<String,Object> newsEntity;
        public ViewHolderOne(@NonNull View view) {
            super(view);
            title = view.findViewById(R.id.title);
            author = view.findViewById(R.id.author);
            comment = view.findViewById(R.id.comment);
            time = view.findViewById(R.id.time);
            header = view.findViewById(R.id.header);
            thumb = view.findViewById(R.id.thumb);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: 点击数还记3");
                }
            });
        }
    }

    public class ViewHolderTwo extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView author;
        private TextView comment;
        private TextView time;
        private ImageView header;
        private ImageView pic1, pic2, pic3;
        private Map<String,Object> newsEntity;

        public ViewHolderTwo(@NonNull View view) {
            super(view);
            title = view.findViewById(R.id.title);
            author = view.findViewById(R.id.author);
            comment = view.findViewById(R.id.comment);
            time = view.findViewById(R.id.time);
            header = view.findViewById(R.id.header);
            pic1 = view.findViewById(R.id.pic1);
            pic2 = view.findViewById(R.id.pic2);
            pic3 = view.findViewById(R.id.pic3);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: 点击了");
                }
            });
        }
    }

    public class ViewHolderThree extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView author;
        private TextView comment;
        private TextView time;
        private ImageView header;
        private ImageView thumb;
        private Map<String,Object> newsEntity;

        public ViewHolderThree(@NonNull View view) {
            super(view);
            title = view.findViewById(R.id.title);
            author = view.findViewById(R.id.author);
            comment = view.findViewById(R.id.comment);
            time = view.findViewById(R.id.time);
            header = view.findViewById(R.id.header);
            thumb = view.findViewById(R.id.thumb);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: 点击数还记2");
                }
            });
        }
    }


}

3.图片圆  实体类


import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.squareup.picasso.Transformation;


/**
 * @author: 源头像
 * @date: 2020-07-07
 **/
public class CircleTransform implements Transformation {

    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;
        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        squaredBitmap.recycle();
        return bitmap;
    }

    public String key() {
        return "circle";
    }
}

4.主activity

package com.example.recymorestyleapplication;

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.os.Handler;
import android.os.Message;

import com.example.recymorestyleapplication.adapter.NewsAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private RefreshLayout refreshLayout;
    private LinearLayoutManager linearLayoutManager;
    private NewsAdapter newsAdapter;
    private List<String> list=new ArrayList<>();//视频列表
    private String TAG="新闻列表";


    private Handler mhandler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //放在这里面就在UI朱线程里面执行
            switch (msg.what){
                case 0:
                    newsAdapter.setDatas(list);
                    newsAdapter.notifyDataSetChanged();//通知recyview刷新页面
                    break;
                case 1:

                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        recyclerView =findViewById(R.id.recy2);
        refreshLayout=findViewById(R.id.refreshLayout);//上拉刷新
    }

    @Override
    protected void initData() {
        linearLayoutManager=new LinearLayoutManager(MainActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//垂直排列
        recyclerView.setLayoutManager(linearLayoutManager);
        newsAdapter=new NewsAdapter(MainActivity.this);
        recyclerView.setAdapter(newsAdapter);//绑定适配器
        //下拉刷新第三方插件)
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getNewsList(true);
            }
        });
        // 上拉加载
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                getNewsList(false);
            }
        });
        //获取新闻数据
        getNewsList(true);
        getNewsList(true);
    }

    public void getNewsList(boolean isRefresh){
        if (isRefresh) {
            refreshLayout.finishRefresh(true);
        } else {
            refreshLayout.finishLoadMore(true);
        }
        if(isRefresh)
        {
            list=new ArrayList<>();
            for (int a=0;a<3;a++){
                list.add(a+"");
            }
        }else {
            for (int a=0;a<3;a++){
                list.add(a+"");
            }
        }
        mhandler.sendEmptyMessage(0);
    }

}

公共activity

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

/**
 *基类    公众的方法
 *
 * abstract  抽象类
 **/
public abstract class BaseActivity extends AppCompatActivity {
    private Context mcontext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mcontext=this;
        setContentView(initLayout());
        initView();
        initData();
    }

    protected abstract int initLayout();
    protected abstract void initView();
    protected  abstract void initData();

    public  void ShowToast(String msg){
        Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show();
    }
    public  void ShowToastSync(String msg){
        Looper.prepare();//消息队列自学
        Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show();
        Looper.loop();
    }
    public void navigateTo(Class cls){
        Intent i=new Intent(this, cls);
        startActivity(i);
    }
    protected void saveStringToSp(String key,String val){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(key,val);
        editor.commit();
    }
}

RecyMoreStyleApplication(加载网络图片,RecyclerView多种样式).rar-互联网文档类资源-CSDN下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值