准备工作:导入

1.activity_mian.xml

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

2.acitivy_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>

3.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.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">
<ImageView
android:id="@+id/news_photo"
android:scaleType="centerCrop"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="150dp"/>
<TextView
android:id="@+id/news_title"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:maxLines="1"
android:textSize="20sp"
android:padding="5dp"
android:textColor="#ffffff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout> <TextView
android:id="@+id/news_desc"
android:maxLines="2"
android:layout_below="@+id/news_header"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_below="@+id/news_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_share" android:text="SHARE" android:background="#00000000" android:layout_marginLeft="10dp" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_more" android:background="#00000000" android:textColor="#7AD3E0" android:text="READ MORE" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout> </android.support.v7.widget.CardView></RelativeLayout>

5.MainActivity

package com.zps.recyclerviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; import static android.support.v7.widget.RecyclerView.*; public class MainActivity extends AppCompatActivity {
/* private RecyclerView recyclerView;
LayoutManager layoutManager;
Adapter adapter;
private List<String> mDatas;
*/
private RecyclerView recyclerView;
private List<News> newsList;
private RecyclerViewAdapter adapter; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /* initData();
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new MyAdapter(mDatas);
recyclerView.setAdapter(adapter);*/ 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.mipmap.ic_launcher));
newsList.add(new News(getString(R.string.news_two_title),getString(R.string.news_two_desc),R.mipmap.ic_launcher));
newsList.add(new News(getString(R.string.news_three_title),getString(R.string.news_three_desc),R.mipmap.ic_launcher));
newsList.add(new News(getString(R.string.news_four_title),getString(R.string.news_four_desc),R.mipmap.ic_launcher));
} /* protected void initData()
{
mDatas = new ArrayList<String>();
for (int i = 'A'; i < 'z'; i++)
{
mDatas.add("" + (char) i);
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mDataset;
public MyAdapter(List<String> myDataset){
mDataset = myDataset;
}
class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.tv_item);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
} @Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset.get(position));
} @Override
public int getItemCount() {
return mDataset.size();
}
}*/
}

6.News

package com.zps.recyclerviewdemo;

import java.io.Serializable;

/**
* Created by Administrator on 2016/1/24 0024.
*/
public class News implements Serializable {
//新闻标题,内容,图片
private String title;
private String desc;
private int photoId; /**
* Constructs a new instance of {@code Object}.
*/
public News(String name, String age, int photoId) {
this.title=name;
this.desc=age;
this.photoId=photoId;
} public void setDesc(String desc) {
this.desc = desc;
} public void setTitle(String title) {
this.title = title;
} public void setPhotoId(int photoId) {
this.photoId = photoId;
} public String getDesc() {
return desc;
} public int getPhotoId() {
return photoId;
} public String getTitle() {
return title;
}
}

7.NewsActiviy

package com.zps.recyclerviewdemo;

/**
* Created by Administrator on 2016/1/24 0024.
*/ import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView; public class NewsActivity extends AppCompatActivity {
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()); }
}

8.RecyclerViewAdapter

package com.zps.recyclerviewdemo;

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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import java.util.List; /**
* Created by Administrator on 2016/1/24 0024.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.NewsViewHolder>{ private List<News> newses;
private Context context;
public RecyclerViewAdapter(List<News> newses,Context context) {
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(final 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 RecyclerViewAdapter.NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v= LayoutInflater.from(context).inflate(R.layout.news_item,viewGroup,false);
NewsViewHolder nvh=new NewsViewHolder(v);
return nvh;
} @Override
public void onBindViewHolder(RecyclerViewAdapter.NewsViewHolder personViewHolder, int i) {
final int j=i; personViewHolder.news_photo.setImageResource(newses.get(i).getPhotoId());
personViewHolder.news_title.setText(newses.get(i).getTitle());
personViewHolder.news_desc.setText(newses.get(i).getDesc()); //为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 int getItemCount() { return newses.size(); }}

RecyclerView 结合 CardView 使用的更多相关文章

  1. Android L 之 RecyclerView 、CardView 、Palette

    转: http://blog.csdn.net/xyz_lmn/article/details/38735117 <Material Design>提到,Android L版本中新增了Re ...

  2. Android L中间RecyclerView 、CardView 、Palette使用

    RecyclerView CardView Palette <Material Design>提到,Android L版本号中新增了RecyclerView.CardView .Palet ...

  3. Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载

    随着 Google 推出了全新的设计语言 Material Design,还迎来了新的 Android 支持库 v7,其中就包含了 Material Design 设计语言中关于 Card 卡片概念的 ...

  4. 【android】使用RecyclerView和CardView,实现知乎日报精致布局

    完整代码,请参考我的博客园客户端,git地址:http://git.oschina.net/yso/CNBlogs 在写博客园客户端的时候,突然想到,弄个知乎日报风格的简单清爽多好!不需要那么多繁杂的 ...

  5. Android L中的RecyclerView 、CardView 、Palette的使用

    <Material Design>提到,Android L版本中新增了RecyclerView.CardView .Palette.RecyclerView.CardView为用于显示复杂 ...

  6. Android RecyclerView And CardView

    Google I/O 2014大会公布Android L系统,还有Material Design全新的设计风格.而Material Design卡片式的设计.Google Play应用商店和G+ AP ...

  7. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

  8. RecyclerView 结合 CardView 使用(二)

    上一篇的基础上,修改了,CardView的布局和点击效果 总结: CardView的奇葩属性 :app:cardPreventCornerOverlap="false" 和园角边框 ...

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

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

随机推荐

  1. shell脚本积累

    统计当前目录下文件夹的大小 for d in $(ls) do du -sh ./$d done 获取之前日期date  +"%Y%m%d" -d  "-n days&q ...

  2. mac mysql安装

    一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...

  3. Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

  4. 使用maven多模块来构建系统时,spring初始化报错的问题

    最近在实验maven结构的maven工程时,碰到一个问题,springbean总是初始化失败: Related cause: org.springframework.beans.factory.Uns ...

  5. 【Ural】【1057】Amount of degrees

    数位DP 2009年刘聪<浅谈数位类统计问题> 例题一 从组合数 以及 数位DP的角度都可以做…… 首先转化成求1~n内K进制下只有0.1的数的个数: 考虑K进制下第一个为1的位,剩下的数 ...

  6. PHP读取xml方法讲解

    一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...

  7. springMVC 简单事例

    本帖最后由 悲观主义者一枚 于 2015-1-31 17:55 编辑 使用SpringMvc开发Android WebService入门教程1.首先大家先创建一个JavaWeb项目2.然后加入Spri ...

  8. GET和POST的区别,就是明信片和信封的区别

  9. C#实现身份证号码验证的方法

    本文实例讲述了C#实现身份证号码验证的方法.分享给大家供大家参考.具体实现方法如下: 随着现在互联网的发展,越来越多的注册用户的地方都用到了身份证,那么对于输入的身份证如何验证呢?看下面的代码,其实很 ...

  10. 安装SQL Server 2012遇到“需要更新的以前的Visual Studio 2010实例.”

    Microsoft Visual Studio 2010 Service Pack 1(exe) 下载链接:http://www.microsoft.com/zh-cn/download/confir ...