Android recycleview+cardview
来源:互联网 发布:linux udp socket编程 编辑:程序博客网 时间:2024/06/06 07:14
第一步:导入依赖:recycleview和cardview
compile 'com.android.support:recyclerview-v7:24.2.1'compile 'com.android.support:cardview-v7:24.2.1'compile 'com.android.support:appcompat-v7:24.2.1'第二部:xml中recycleview和cardview(item)
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/title" android:id="@+id/recycleView"></android.support.v7.widget.RecyclerView>
cardview:
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" app:cardBackgroundColor="#ffffff" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"><TextView android:textColor="#9b9b9b" android:textSize="15.0sp"android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cisco" android:id="@+id/text"/> <ImageView android:id="@+id/wifiLeve" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_width="22dp" android:layout_height="15dp"android:background="@drawable/one" /> </RelativeLayout></android.support.v7.widget.CardView>
第三部:cardview的属性
android:cardCornerRadius在xml文件中设置card圆角的大小CardView.setRadius在代码中设置card圆角的大小android:cardBackgroundColor在xml文件中设置card背景颜色android:elevation在xml文件中设置阴影的大小card_view:cardElevation在xml文件中设置阴影的大小card_view:cardMaxElevation在xml文件中设置阴影最大高度card_view:cardCornerRadius在xml文件中设置卡片的圆角大小card_view:contentPadding在xml文件中设置卡片内容于边距的间隔 card_view:contentPaddingBottom在xml文件中设置卡片内容于下边距的间隔 card_view:contentPaddingTop在xml文件中设置卡片内容于上边距的间隔 card_view:contentPaddingLeft在xml文件中设置卡片内容于左边距的间隔 card_view:contentPaddingRight在xml文件中设置卡片内容于右边距的间隔 card_view:contentPaddingStart在xml文件中设置卡片内容于边距的间隔起始 card_view:contentPaddingEnd在xml文件中设置卡片内容于边距的间隔终止 card_view:cardUseCompatPadding在xml文件中设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式card_view:cardPreventConrerOverlap 在xml文件中设置内边距,在V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠
package com.jdhr.crowdfunds.activitygovern;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.TextView;import com.jdhr.crowdfunds.R;import java.util.List;/** * Created by Administrator on 2017/4/13. */public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.MyViewholder>implements View.OnClickListener{ List<String> mList; Context mContext; //定义一个点击事件的接口 public static interface onRecycleviewItemClickListener { void onItemClick(View view , String data); } private onRecycleviewItemClickListener mOnItemClickListener = null; public void setOnItemClickListener(onRecycleviewItemClickListener listener) { this.mOnItemClickListener = listener; } public RecycleviewAdapter(List<String> allWifi, Context mContext) { this.mList = allWifi; this.mContext = mContext; } //item的点击事件 @Override public void onClick(View view) { if (mOnItemClickListener != null) { //注意这里使用getTag方法获取数据 mOnItemClickListener.onItemClick(view,(String) view.getTag()); } } class MyViewholder extends RecyclerView.ViewHolder{ TextView actionName; public MyViewholder(View itemView) { super(itemView); actionName= (TextView) itemView.findViewById(R.id.text); } } @Override public RecycleviewAdapter.MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(mContext).inflate(R.layout.item_recycleview,null); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); WindowManager wm = (WindowManager)mContext .getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); lp.width=width/2; view.setLayoutParams(lp); //为item设置点击事件 view.setOnClickListener(this); MyViewholder viewholder=new MyViewholder(view); return viewholder; } @Override public void onBindViewHolder(MyViewholder holder, int position) { View view=holder.itemView; if(view==null){ Log.e("获取到的view为空","------"); }else { Log.e("获取到的不view为空","------"); } ViewGroup.LayoutParams myLayoutParams=holder.itemView.getLayoutParams(); if(myLayoutParams==null){ Log.e("layoutparams为空","layoutparams为空"); }else { Log.e("layoutparams不为空","layoutparams为空"); } //将数据保存在itemview的tag中 holder.itemView.setTag(mList.get(position)); holder.actionName.setText(mList.get(position)); } @Override public int getItemCount() { return mList.size(); }}
第五部分:recycleview的item间距
public class RecycleviewItem extends RecyclerView.ItemDecoration{private int space; public RecycleviewItem(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left = space; outRect.right = space; outRect.bottom = space; if(parent.getChildPosition(view) != 0){ outRect.top = space; }}}
调用: myRecycleview= (RecyclerView) findViewById(R.id.recycleView); WifiManager mWifiManager= (WifiManager) getSystemService(Context.WIFI_SERVICE); allWifi= mWifiManager.getScanResults();
myRecycleview.setLayoutManager(new LinearLayoutManager(this)); myRecycleview.addItemDecoration(new RecycleviewItem(5)); RecycleviewAdapter adapter=new RecycleviewAdapter(allWifi,getApplicationContext()); myRecycleview.setAdapter(adapter); adapter.setOnItemClickListener(new RecycleviewAdapter.onRecycleviewItemClickListener() { @Override public void onItemClick(View view, ScanResult data) { Toast.makeText(getApplicationContext(),data.SSID,Toast.LENGTH_LONG).show(); } });
//默认纵向,这样设置横向//LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);//linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
附加:cardview的操作
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
cardBackgroundColor属性指定CardView的背景色;
cardCornerRadius属性指定作为边框的圆角矩形的半径;
cardElevation是阴影大小,MAX~~是最大阴影大小;
cardUseCompatPadding compat一般是为了兼容设备,保持一样的外观,所以这里就是保持内边距的计算方式一样;
cardPreventCornerOverlap属性是为了防止卡片内容和边角的重叠;
0 0
- Android recycleview+cardview
- Android 5.0+(RecycleView、CardView、Palette)
- Android 使用RecycleView和CardView
- Android之RecycleView和CardView
- RecycleView和CardView笔记(一)
- 瀑布流效果RecycleView+CardView
- Android recycleview和cardview结合使用出现的边角阴影问题
- android cardview
- Android CardView
- eclipse 上使用recycleview,cardview的办法
- 5.0 recycleview 和cardview基本使用
- Recycleview 多布局添加和cardview使用
- android recycleView
- Android RecycleView
- Android-RecycleView
- 使用cardview和recycleview时碰到的一些问题
- 如果5.0环境,不能自动引入RecycleView CardView
- 关于eclipse中recycleview/cardview的引入(环境搭建)
- android关于dispatchTouchEvent和onTouchEvent的源码实验分析
- for(;;)和while(true)的区别
- zlib 1.2.8 编译
- nodejs搭建http服务器(单一响应)
- log4j与log4j2的区别
- Android recycleview+cardview
- strongloop 修改系统默认返回错误信息
- NavigationController & NavigationBar常见用法以及设置
- Word文字的三种隐藏方法
- LOL坑人极限
- (十二)命令模式详解(故事版)
- 【Unity Shader学习笔记】(三)绘制点、直线、网格等基本图形
- 我也不知道有什么用
- Hessian源码分析(java)