引入的包:

demo结构:

测试代码:

News.java:

package com.zzw.testcardview;

import java.io.Serializable;

public class News implements Serializable {

    // 新闻标题,内容,图片
private String title;
private String desc;
private int photoId; public News(String title, String desc, int photoId) {
super();
this.title = title;
this.desc = desc;
this.photoId = photoId;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public int getPhotoId() {
return photoId;
} public void setPhotoId(int photoId) {
this.photoId = photoId;
}
}

MainActivity.java:

package com.zzw.testcardview;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; public class MainActivity extends Activity { private RecyclerView recyclerView;
private List<News> newsList;
private RecyclerViewAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); initPersonData();
adapter = new RecyclerViewAdapter(newsList, MainActivity.this); recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter); } private void initPersonData() {
newsList = new ArrayList<>();
// 添加新闻
newsList.add(new News(getString(R.string.news_one_title),
getString(R.string.news_one_desc), R.drawable.news_one));
newsList.add(new News(getString(R.string.news_two_title),
getString(R.string.news_two_desc), R.drawable.news_two));
newsList.add(new News(getString(R.string.news_three_title),
getString(R.string.news_three_desc), R.drawable.news_three));
newsList.add(new News(getString(R.string.news_four_title),
getString(R.string.news_four_desc), R.drawable.news_four));
} }

RecyclerViewAdapter.java:

package com.zzw.testcardview;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; public class RecyclerViewAdapter extends
Adapter<RecyclerViewAdapter.NewsViewHolder> { private List<News> newses;
private Context context; public RecyclerViewAdapter(List<News> newses, Context context) {
super();
this.newses = newses;
this.context = context;
} // 自定义ViewHolder类
static class NewsViewHolder extends RecyclerView.ViewHolder { CardView cardView;
ImageView news_photo;
TextView news_title;
TextView news_desc;
Button share;
Button readMore; public NewsViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.card_view);
news_photo = (ImageView) itemView.findViewById(R.id.news_photo);
news_title = (TextView) itemView.findViewById(R.id.news_title);
news_desc = (TextView) itemView.findViewById(R.id.news_desc);
share = (Button) itemView.findViewById(R.id.btn_share);
readMore = (Button) itemView.findViewById(R.id.btn_more); // 设置TextView背景为半透明
news_title.setBackgroundColor(Color.argb(20, 0, 0, 0));
}
} @Override
public int getItemCount() { return newses == null ? 0 : newses.size();
} @Override
public void onBindViewHolder(
RecyclerViewAdapter.NewsViewHolder personViewHolder, int position) { final int j = position; personViewHolder.news_photo.setImageResource(newses.get(position)
.getPhotoId());
personViewHolder.news_title.setText(newses.get(position).getTitle());
personViewHolder.news_desc.setText(newses.get(position).getDesc()); // 为btn_share btn_readMore cardView设置点击事件
// 为btn_share btn_readMore cardView设置点击事件
personViewHolder.cardView
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NewsActivity.class);
intent.putExtra("News", newses.get(j));
context.startActivity(intent);
}
}); personViewHolder.share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, newses.get(j).getDesc());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, newses
.get(j).getTitle()));
}
}); personViewHolder.readMore
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NewsActivity.class);
intent.putExtra("News", newses.get(j));
context.startActivity(intent);
}
});
} @Override
public RecyclerViewAdapter.NewsViewHolder onCreateViewHolder(
ViewGroup viewGroup, int arg1) { View view = LayoutInflater.from(context).inflate(R.layout.news_item,
null);
NewsViewHolder nvh = new NewsViewHolder(view); return nvh;
} }

NewsActivity.java:

package com.zzw.testcardview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView; public class NewsActivity extends Activity{ private ImageView newsPhoto;
private TextView newsTitle;
private TextView newsDesc; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news); newsPhoto= (ImageView) findViewById(R.id.news_info_photo);
newsTitle= (TextView) findViewById(R.id.news_info_title);
newsDesc= (TextView) findViewById(R.id.news_info_desc); Intent intent=getIntent(); News item= (News) intent.getSerializableExtra("News");
newsPhoto.setImageResource(item.getPhotoId());
newsTitle.setText(item.getTitle());
newsDesc.setText(item.getDesc()); }
}

布局代码:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

news_item.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"
android:padding="5dp" > <!-- android:clickable="true" //cardView是否可点击,默认是不可点击的 -->
<!-- app:cardCornerRadius="3dp" //圆角 -->
<!-- app:cardElevation="8dp" //阴影 --> <android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="3dp"
app:cardElevation="8dp" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <RelativeLayout
android:id="@+id/news_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <ImageView
android:id="@+id/news_photo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop" /> <TextView
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout> <TextView
android:id="@+id/news_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news_header"
android:layout_margin="15dp"
android:maxLines="2" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/news_desc"
android:orientation="horizontal" > <Button
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:background="#00000000"
android:text="SHARE" /> <Button
android:id="@+id/btn_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="READ MORE"
android:textColor="#7AD3E0" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView> </RelativeLayout>

activity_news.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"
android:padding="20dp">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
app:cardCornerRadius="3dp"
app:cardElevation="8dp"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/news_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/news_info_photo"
android:scaleType="centerCrop"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="180dp"/>
<TextView
android:id="@+id/news_info_title"
android:layout_alignParentLeft="true"
android:layout_below="@+id/news_info_photo"
android:textSize="20sp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout> <TextView
android:id="@+id/news_info_desc"
android:lineSpacingExtra="5dp"
android:layout_below="@+id/news_header"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>

利用RecyclerView CardView实现新闻卡片样式的更多相关文章

  1. RecyclerView,CardView导入和使用(Demo)

    简介: 这篇文章是ANDROID L——Material Design详解(UI控件)的一个补充或者说是应用实例,如果有时间建议大家稍微浏览一下上篇文章. 本文主要介绍Android L新增加的两个U ...

  2. 利用RecyclerView实现无限轮播广告条

    代码地址如下:http://www.demodashi.com/demo/14771.html 前言: 公司产品需要新增悬浮广告条的功能,要求是可以循环滚动,并且点击相应的浮条会跳转到相应的界面,在实 ...

  3. Material design之New Widgets(RecyclerView CardView)

    New Widgets:提供了两个新的控件 RecyclerView CardView 这两个控件包含在了Android L的support library中, 他们可以用于显示复杂的布局而且都默认采 ...

  4. ANDROID L——RecyclerView,CardView进口和使用(Demo)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控 ...

  5. RecyclerView+CardView简单使用

    RecyclerView取代Listview用来显示数据,除此之外还能实现瀑布流的布局.CardView让我们的界面更好看,此外还将使用官方的下拉刷新. 添加支持: compile 'com.andr ...

  6. RecyclerView+Cardview学习探索

    1.概述 在support-V7包中引入了很多新的M 控件,其中RccyclerView也是其中一员,它的名字来源于它的工作方式:当一个Item被隐藏起来时候并没有被销毁,当建立新的item时候,组件 ...

  7. 利用jquery实现百度新闻导航菜单滑动动画

    前言 前两天,群里有人问百度新闻导航是如何实现的,当时由于忙于工作,没有来得及细看,恰好今天有空闲时间,索性就实现一下这个效果吧: 思路与步骤 1.利用UL创建简单横向导航: <!DOCTYPE ...

  8. 【Web】利用jquery实现百度新闻导航菜单滑动动画

    前言 前两天,群里有人问百度新闻导航是如何实现的,当时由于忙于工作,没有来得及细看,恰好今天有空闲时间,索性就实现一下这个效果吧: 思路与步骤 1.利用UL创建简单横向导航: <!DOCTYPE ...

  9. Android Wear开发 - 卡片通知 - 第二节 : 自定义Wear卡片样式

    一.前言说明 在上一节添加Android Wear通知特性我们实现了简单的Android Wear卡片拓展,但是默认提供给我们的多张卡片只能实现简单的文字展示,如果想要自定义布局,添加图片等等,则需要 ...

随机推荐

  1. Intent Receiver

    使用Intent Receiver让自己的应用对一个外部事件做出响应,比如当电话呼入时,或者当数据网络可用时,或者时间到晚上了.Intent Receiver不能显示用户界面,它只能通过Notific ...

  2. How to Modify Public Network Information including VIP in Oracle Clusterware (文档 ID 276434.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 11.2.0.3 to 12.1.0.2 [Release 11.2 to 12. ...

  3. 关于mina框架EMFILE: open too many files exception的处理

    做项目的时候,用到了mina框架,与server进行交互.由于采用的是短连接+心跳包+断线重连的方式,因此网络不稳定的时候经常会出现断线重连. 那么有时候偶尔会出现EMFILE: open too m ...

  4. 怎样调整CODESOFT中条码线的宽度

       CODESOFT是一款功能强大.灵活便捷的标签条码设计打印软件.在使用CODESOFT设计并打印标签,有时会因为打印精度或扫面清晰度等原因,需要调整条形码中行的宽度,即调整条码线宽度.本文,小编 ...

  5. memcached学习(5). memcached的应用和兼容程序

    mixi在提供服务的初期阶段就使用了memcached. 随着网站访问量的急剧增加,单纯为数据库添加slave已无法满足需要,因此引入了memcached. 此外,我们也从增加可扩展性的方面进行了验证 ...

  6. MSP430F149学习之路——时钟2

    代码一: /************************** 功能:LED每隔1秒闪烁一次 ****************************/ #include <msp430x14 ...

  7. 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

    P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...

  8. C# 扩展

    “扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些 ...

  9. 使用UI Automation实现自动化测试 --微软提供的控件Pattern

    微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...

  10. ionic ngcordova camera

    拍照是經常用到的,所以記錄一下 拍照的代碼... 1. ionic start camera blankcd camera ionic platform add ios 2. 添加插件,這裏很熟悉.. ...