banner2.0自定义轮播布局

news2024/10/5 15:22:30

说明:最近碰到一个需求,让新闻列表实现轮播图的效果,也就是轮播新闻,然后样式必须按照ui设计的样式来弄,之前传统的banner,都是只轮播图片,没想到,这次居然要轮播新闻,
网上找了点资料,发现banner2.0可以实现自定义的样式内容轮播,然后就实现了这个功能,
可以实现点击监听,跳转新闻详情页面
效果图:
在这里插入图片描述

step1:依赖包 ~\app\build.gradle

    implementation 'io.github.youth5201314:banner:2.2.2'
    implementation "com.github.bumptech.glide:glide:4.6.1"
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.github.bumptech.glide:glide:4.8.0'

step2:清单文件,网络权限 ~\app\src\main\AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />

step3: 主界面ui布局 ~\app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/news"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECECEC"
    android:orientation="vertical">

    <com.youth.banner.Banner
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:background="#8DC8F6" />

</LinearLayout>

step4:item界面ui布局 ~\app\src\main\res\layout\view_message.xml

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

    <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="100dp"
        android:layout_height="230dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tx_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_logo"
        android:text="@string/app_name"
        android:textSize="20sp"
        android:textColor="@android:color/holo_red_dark"
        />
    <TextView
        android:id="@+id/tx_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tx_title"
        android:text="@string/app_name"
        />
    <TextView
        android:id="@+id/tx_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tx_content"
        android:text="@string/app_name"
        android:textColor="@android:color/holo_orange_light"

        />

</RelativeLayout>

step5: 主界面功能 ~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java

package com.example.iosdialogdemo;


import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import java.util.ArrayList;
import java.util.List;
import com.youth.banner.listener.OnBannerListener;

public class MainActivity extends AppCompatActivity {
    private Banner banner;
    private List<Notice> notices;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        banner = findViewById(R.id.banner);
        notices = new ArrayList<Notice>();
        notices.add(new Notice(1, "红楼梦", "曹雪芹", "http://img.netbian.com/file/2024/0516/small0017044jQKI1715789824.jpg", "2022-05-22"));
        notices.add(new Notice(2, "西游记", "吴承恩", "http://img.netbian.com/file/2024/0516/small001855bTfrr1715789935.jpg", "2022-05-22"));
        notices.add(new Notice(3, "三国演义", "罗贯中", "http://img.netbian.com/file/2024/0517/small150904ck1C21715929744.jpg", "2022-05-22"));
        notices.add(new Notice(4, "水浒传", "施耐庵", "http://img.netbian.com/file/2024/0516/small002209I5p4J1715790129.jpg", "2022-05-22"));
        notices.add(new Notice(5, "桃花源记", "陶渊明", "http://img.netbian.com/file/2024/0519/small190636mQVqM1716116796.jpg", "2022-05-22"));
        banner.setAdapter(new MsgBannerAdapter(notices, MainActivity.this));
        banner.addBannerLifecycleObserver(this);
        banner.setIndicator(new CircleIndicator(MainActivity.this));
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object o, int i) {
                Log.e("TAG", "点击了" + notices.get(i).getTitle());
            }
        });
    }
}

step6:轮播适配器 ~\app\src\main\java\com\example\iosdialogdemo\MsgBannerAdapter.java

package com.example.iosdialogdemo;


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.youth.banner.adapter.BannerAdapter;
import java.util.List;

public class MsgBannerAdapter extends BannerAdapter<Notice, MsgBannerAdapter.BannerViewHolder> {
    Context mainContext;

    public MsgBannerAdapter(List<Notice> datas, Context context) {
        super(datas);
        mainContext = context;
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_message, parent, false);
        return new BannerViewHolder(view);
    }

    @Override
    public void onBindView(BannerViewHolder holder, Notice data, final int position, int size) {
        holder.tx_title.setText(mDatas.get(position).getTitle());
        holder.tx_content.setText(mDatas.get(position).getContent());
        holder.tx_time.setText(mDatas.get(position).getUpdatedTimeStr());
        //加载图片
        Glide.with(mainContext)
                .load(mDatas.get(position).getCreateTimeStr())
                .into(holder.iv_logo);
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_logo;
        TextView tx_title, tx_content, tx_time;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            tx_time = itemView.findViewById(R.id.tx_time);
            iv_logo = itemView.findViewById(R.id.iv_logo);
            tx_title = itemView.findViewById(R.id.tx_title);
            tx_content = itemView.findViewById(R.id.tx_content);
        }
    }
}

step7:data数据类 ~\app\src\main\java\com\example\iosdialogdemo\Notice.java

package com.example.iosdialogdemo;

import java.io.Serializable;
import java.util.Date;

public class Notice implements Serializable {
    private int id;

    private String title;

    private String content;


    private String createTimeStr;

    private String updatedTimeStr;

    private static final long serialVersionUID = 1L;

    public Notice(int id, String title, String content, String createTimeStr, String updatedTimeStr) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.createTimeStr = createTimeStr;
        this.updatedTimeStr = updatedTimeStr;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreateTimeStr() {
        return createTimeStr;
    }

    public void setCreateTimeStr(String createTimeStr) {
        this.createTimeStr = createTimeStr;
    }

    public String getUpdatedTimeStr() {
        return updatedTimeStr;
    }

    public void setUpdatedTimeStr(String updatedTimeStr) {
        this.updatedTimeStr = updatedTimeStr;
    }
}

end

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1699979.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

夏老师小课堂(7) 免费撸Harmony0S应用开发者高级认证

点击上方 “机械电气电机杂谈 ” → 点击右上角“...” → 点选“设为星标 ★”&#xff0c;为加上机械电气电机杂谈星标&#xff0c;以后找夏老师就方便啦&#xff01;你的星标就是我更新动力&#xff0c;星标越多&#xff0c;更新越快&#xff0c;干货越多&#xff01; 关注…

C++ | Leetcode C++题解之第113题路径总和II

题目&#xff1a; 题解&#xff1a; class Solution { public:vector<vector<int>> ret;unordered_map<TreeNode*, TreeNode*> parent;void getPath(TreeNode* node) {vector<int> tmp;while (node ! nullptr) {tmp.emplace_back(node->val);node …

高效的大型语言模型适应方法:提升基础性的解决方案

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

C++系列-static成员

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 概念 声明为static的类成员称为类的静态成员&#xff0c;用static修饰的成员变量&#xff0c;称之为静态成员变量&#xff0c;用static修饰的成员函数&#xff0c;称之为静态成…

Django框架css文件能正常加载,但是css样式不生效

最近运行一个Django项目&#xff0c;能正常启动运行&#xff0c;css文件也能够正常加载&#xff0c;但是css样式却没有正常渲染。 解决办法&#xff1a; 1、打开注册表&#xff1a;winR 2、找到&#xff1a;计算机\HKEY_CLASSES_ROOT\.css 修改&#xff1a;Content Type 值&…

【机器学习结合AI绘画工具】——开启艺术创作的新纪元

目录 一、AI绘画工具的发展历程 二、AI绘画工具的技术原理 实例说明 三、AI绘画工具在艺术创作中的应用 实例网站 四、AI绘画工具的影响与未来展望 结论 机器学习和人工智能&#xff08;AI&#xff09;在过去的十年里取得了显著的进展。特别是在艺术创作领域&#xff0c…

linux创建离线yum源给局域网机器使用

适用场景&#xff1a;在封闭的内网环境中&#xff0c;无法使用互联网进行安装各种rpm包的时候&#xff0c;离线yum源可以解决大部分问题&#xff0c;配置号后可直接使用yum进行安装包 1.准备好镜像源ISO&#xff1a; 例如以下示例&#xff0c;具体可参考自己的系统进行下载&a…

50-Qt控件详解:Input Display

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> //1.Combo Box控件 #include<QComboBox> //2.QFontComboBox控件 #include<QFontComboBox> #include<QLabel>//3.Line Edit控件 #include<QLineEdit> #include <QPushButton…

Installing Tinyproxy on CentOS 7 测试可用

Installing Tinyproxy on CentOS 7 For RHEL/CentOS 7 systems, Tinyproxy is part of EPEL (Extra Packages for Enterprise Linux). Install EPEL on CentOS 7 yum install epel-release -y yum update -y Install Tinyproxy on CentOS 7 yum install tinyproxy -y 编辑…

mysql中单表查询方法

大家好。我们知道&#xff0c;mysql有一个查询优化器的模块。当我们用sql语句查询表中记录时&#xff0c;会对这条查询语句进行语法解析&#xff0c;然后就会交给查询优化器来进行优化&#xff0c;优化后生成一个执行计划&#xff0c;这个执行计划表明了应该使用哪些索引进行查…

用这8种方法在海外媒体推广发稿平台上获得突破-华媒舍

在今天的数字时代&#xff0c;海外媒体推广发稿平台已经成为了许多机构和个人宣传和推广的有效途径。如何在这些平台上获得突破并吸引更多的关注是一个关键问题。本文将介绍8种方法&#xff0c;帮助您在海外媒体推广发稿平台上实现突破。 1. 确定目标受众 在开始使用海外媒体推…

[论文阅读笔记31]Mamba (Selective Structured State Space Model) 及其应用

最近想学一下Mamba模型&#xff0c;奈何看了很多视频还是感觉一知半解&#xff0c;因此做一篇笔记&#xff0c;顺便介绍一下Mamba结构作为CV backbone和时间序列预测领域的应用。 论文1. Mamba: Linear-Time Sequence Modeling with Selective State Spaces 0. Abstract 现有…

ModernC++(一)C++11

都在说Modern C和C不是一个东西&#xff0c;到底MordenC好在哪里&#xff0c;学了才有发言权。 why Morden C C 98的目标&#xff1a; 支持面向对象编程&#xff0c;支持泛型编程支持数据抽象 C 11的目标&#xff1a;使得C成为更好的适用于系统开发以及库开发的语言使得C语法…

宁夏银川、山东济南、中国最厉害的改名大师的老师颜廷利教授的前沿思想观点

在当代社会&#xff0c;一个响亮的声音穿越了传统的迷雾&#xff0c;它来自东方哲学的殿堂&#xff0c;由一位现代学者颜廷利教授所发出。他的话语&#xff0c;如同一股清泉&#xff0c;在混沌的世界里激荡着思考的波澜&#xff1a;"有‘智’不在年高&#xff0c;无‘智’…

第五节: 带你全面理解 vue3 中 computed, watch, watchEffect 组合式API的使用

前言: 上一章, 带大家分析了vue3核心响应式API中的三个, 即reactive,ref, readonly. 本章将会带大家分另外几个工作中比较常用的组合式API. 1. computed 计算属性 在vue2中, 我们是通过computed选项添加计算属性的, 关于计算属性的本质, 这里就不过多阐述了, 如果还有不了…

C++:函数模版简介

如果我们想要实现一个通用的交换函数&#xff0c;在C语言中&#xff0c;我们大概要定义无数个参数不同的交换函数&#xff0c;并且它们的函数名需要各不相同&#xff0c;相信这样的调用便会非常困难&#xff0c;想要调哪个函数还要记得对应的函数名。在C中&#xff0c;有了重载…

Aya 23 是 Cohere For AI 推出的一款最先进的新型多语言开放重量模型

相信一些对LLM关注较高的同学们&#xff0c;应该对这家加拿大的Cohere不会太陌生。毕竟此前&#xff0c;它就开源过 Aya 101 和 Command R 这两款大模型。 Cohere 的非营利性研究实验室 Cohere for AI 发布了 Aya 23&#xff0c;这是其多语言大型语言模型 &#xff08;llm&…

如何使用Rust构建Python原生库?注意,不是动态链接库!!!

参考文档&#xff1a;https://github.com/PyO3/pyo3 创建python虚拟环境&#xff1a; conda create --name pyo3 python3.11.7激活虚拟环境&#xff1a; conda activate pyo3安装依赖&#xff1a; pip install maturin初始化项目&#xff1a; maturin init构建项目&#x…

企业如何做好 SQL 质量管理?

研发人员写 SQL 操作数据库想必一定是一类基础且常见的工作内容。如何避免 “问题” SQL 流转到生产环境&#xff0c;保证数据质量&#xff1f;这值得被研发/DBA/运维所重视。 什么是 SQL 问题&#xff1f; 对于研发人员来说&#xff0c;在日常工作中&#xff0c;大部分都需要…

全国首例!云南破获域名黑产大案,抓获630人

2021年5月以来&#xff0c;在公安部的组织指挥下&#xff0c;云南公安机关历时8个多月&#xff0c;成功破获全国首例域名黑产犯罪案件&#xff0c;经全国各地公安机关连续奋战&#xff0c;共侦破案件300起&#xff0c;抓获涉案人员630人&#xff0c;查封用于黄、赌、诈等违法网…