便签信息显示ViewHolder
职责
- 显示便签内容
- 显示便签添加日期
组件
- TextView 显示便签内容
- TextView 显示便签添加日期,但是不显示全部日期,当天显示时分,不在同一天显示日期,不在同一年显示年月日【可以参考日期工具】
- RelativeLayout relativeLayout;
- ItemonClick itemonClick;
- ItemonLongClick itemonLongClick;
布局
- 根容器CardView
- 项容器RelativeLayout
事件
- 项容器RelativeLayout单击事件
- 项容器RelativeLayout长按事件
兼容性
v21版本点击事件需要 Ripple Effect【水波纹】效果
需要layout-v21在项容器上添加android:background="?android:attr/selectableItemBackground"属性
res/layout/itme.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
>
<RelativeLayout
android:clickable="true"
android:longClickable="true"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/st"
android:singleLine="true"
android:text="xxxxxxxxxxxxxxxxx"
android:textColor="#000"
android:textSize="14sp"
/>
<TextView
android:id="@+id/st"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="00:00"
android:textColor="#000"
android:textSize="12sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
android5.0以上的布局 res/layout-v21/itme.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
>
<RelativeLayout
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:longClickable="true"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/st"
android:singleLine="true"
android:text="xxxxxxxxxxxxxxxxx"
android:textColor="#000"
android:textSize="14sp"
/>
<TextView
android:id="@+id/st"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="00:00"
android:textColor="#000"
android:textSize="12sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
com.hzj163.mysqlitedb.viewholder.NoteViewHolder
package com.hzj163.mysqlitedb.viewholder;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hzj163.mysqlitedb.R;
/**
* 作者: 黄志江老师 on 2015/9/21.
* 网址: www.hzj163.com
* 网书: https://www.gitbook.com/@hzj163
* 邮箱: [email protected]
*/
//ViewHolder只负责有什么,用什么组件显示,并不负责具体显示什么内容
//ViewHolder还要负责事件的处理,但是具体怎么处理,ViewHolder并不直接处理
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
//便签内容
public TextView info;
//便签添加日期
public TextView st;
//项布局
RelativeLayout relativeLayout;
//声明接口对象
ItemonClick itemonClick;
//创建一个单击事件接口
public interface ItemonClick
{
//定义接口的时候考虑清楚参数,具体到那些参数,必须本类可以给出
//因为这个接口的参数是给调用者使用
void onClick(View v,int position);
}
//声明接口对象
ItemonLongClick itemonLongClick;
//创建一个长按事件接口
public interface ItemonLongClick
{
void onLongClick(View v,int position);
}
//构造【利用构造传递两个事件接口对象】
public NoteViewHolder(View itemView,ItemonClick itemonClick,ItemonLongClick itemonLongClick) {
super(itemView);
this.itemonClick=itemonClick;
this.itemonLongClick=itemonLongClick;
info = (TextView) itemView.findViewById(R.id.info);
st = (TextView) itemView.findViewById(R.id.st);
relativeLayout=(RelativeLayout) itemView.findViewById(R.id.relativeLayout);
//点击事件
relativeLayout.setOnClickListener(this);
//长按事件
relativeLayout.setOnLongClickListener(this);
}
//View.OnClickListener单击事件
@Override
public void onClick(View v) {
//调用我们自己定义的接口,这样就可以不用在这里处理事件
itemonClick.onClick(v,getAdapterPosition());
}
//View.OnLongClickListener长按事件
//返回false表示会触发单击事件,返回true表示不会触发单击事件
@Override
public boolean onLongClick(View v) {
//调用我们自己定义的接口
itemonLongClick.onLongClick(v,getAdapterPosition());
return true;
}
}