最近一段時間做項目,項目中學(xué)習(xí)到了很多姿(zhi)勢(shi),我把一些我感覺有必要記錄下來的東西 在之后會記錄到博客里,今天將分享其中一個頁面
11.png
22.png
看到UI之后第一反應(yīng)是我草,怎么把這個卡片從中間切開,腦海中浮現(xiàn)出了各種計算.雖然我不會,慢慢的細想,沒必要那么復(fù)雜,不就是隱藏顯示么~ 于是開擼,我們先來畫布局
主頁面 沒啥可說的 就一個RecycleView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_061358"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rcy_more_data"
android:layout_width="match_parent"
android:layout_height="1622px"
android:layout_centerHorizontal="true"
android:layout_marginTop="68px" />
</RelativeLayout>
再來看 item布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="746px"
android:layout_height="300px"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/iv_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/icon_yaotunbi" />
<RelativeLayout
android:id="@+id/rl_content"
android:layout_width="match_parent"
android:layout_height="80px"
android:layout_marginTop="120px">
<TextView
android:id="@+id/tv_chinese_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60px"
android:text=""
android:textColor="@color/color_fafafa"
android:textSize="36px" />
<TextView
android:id="@+id/tv_eg_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_chinese_name"
android:layout_marginLeft="60px"
android:text=""
android:textColor="@color/color_fafafa"
android:textSize="20px" />
<TextView
android:id="@+id/tv_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="90px"
android:text=""
android:textColor="@color/color_fafafa"
android:textSize="52px"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:id="@+id/iv_expand"
android:layout_width="32px"
android:layout_height="32px"
android:layout_alignParentRight="true"
android:layout_marginTop="24px"
android:layout_marginRight="24px"
android:background="@drawable/icon_expand" />
</RelativeLayout>
<include layout="@layout/item_jy_expand_layout" />
</RelativeLayout>
還有一個include的代碼 ,這個就是卡片展開時顯示的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_expand"
android:layout_width="746px"
android:layout_height="450px"
android:layout_centerHorizontal="true"
android:layout_marginTop="120px"
android:background="@drawable/jy_item_expand_bg"
android:visibility="gone">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="580px"
android:layout_above="@id/ll_pack_up">
<TextView
android:id="@+id/tv_introduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="60px"
android:layout_marginTop="34px"
android:layout_marginRight="60px"
android:text="不飽和脂肪酸是構(gòu)成體內(nèi)脂肪的一種脂肪酸,人體不可缺少的脂肪酸恬口。不飽和脂肪酸根據(jù)雙鍵個數(shù)的不同铐懊,分為單不飽和脂肪酸和多不飽和脂肪酸二種。"
android:textColor="@color/color_888888"
android:textSize="20px" />
<TextView
android:id="@+id/tv_expand_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_introduce"
android:layout_centerHorizontal="true"
android:layout_marginLeft="60px"
android:layout_marginTop="26px"
android:layout_marginRight="60px"
android:text="環(huán)境因素和遺傳因素都對肥胖的產(chǎn)生起到重要作用,遺傳因素尤其增加了人體對于肥胖的易感性杂瘸。FTO(fat-mass and obesity-associated gene)基因是一個在不同人群得到廣泛驗證的肥胖易感基因,該基因單核甘酸突變與肥胖密切相關(guān)。研究表明FTO基因在腦仪媒、脂肪組織、胰腺和下丘腦等組織中廣泛表達谢鹊。FTO基因編碼的蛋白在能量代謝過程中發(fā)揮作用算吩。近年來多項研究陸續(xù)報道了肥胖人群和FTO基因突變的關(guān)聯(lián)性分析,因而發(fā)現(xiàn)對于特定突變位點不同基因型個體在肥胖人群和正常人群中的分布存在明顯差異佃扼,攜帶某些特定的基因型的人群為先天性的易胖體質(zhì)偎巢。"
android:textColor="@color/color_888888"
android:textSize="20px" />
</RelativeLayout>
<LinearLayout
android:id="@+id/ll_pack_up"
android:layout_width="100px"
android:layout_height="80px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="收起"
android:textColor="@color/color_2283e2"
android:textSize="20px" />
<ImageView
android:layout_width="18px"
android:layout_height="10px"
android:layout_gravity="center"
android:layout_marginLeft="9px"
android:background="@drawable/icon_stroke" />
</LinearLayout>
</RelativeLayout>
以上 所有的布局都搞完了,下面我們先把adapter 寫完
public class ListDataAdapter extends RecyclerView.Adapter<ListDataAdapter.MyViewHolder> {
private Context context;
public itemOnExpandClickListener itemExpandOnClick;
public itemPackUpClickListener itemPackUpClickListener;
private List<ListDataBean.DataBean> dataBeans;
private AnimationUtils animationUtils;
public void setJyMoreDataBeans(List<ListDataBean.DataBean> dataBeans) {
this.dataBeans = dataBeans;
notifyDataSetChanged();
}
public void setItemPackUpClickListener(ListDataAdapter.itemPackUpClickListener itemPackUpClickListener) {
this.itemPackUpClickListener = itemPackUpClickListener;
}
public void setItemExpandOnClick(itemOnExpandClickListener itemExpandOnClick) {
this.itemExpandOnClick = itemExpandOnClick;
}
public ListDataAdapter(Context context) {
this.context = context;
animationUtils = new AnimationUtils();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_jy_more_layout, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// GlideOptions glideOptions = new GlideOptions.Builder()
// .radius(0)
//
// .cornerType(GlideOptions.CornerType.TOP)
// .build();
// ImageUtil.display(context, jyMoreDataBeans.get(position).getGp_bg_img(), holder.iv_bg, glideOptions);
holder.tv_chinese_name.setText(dataBeans.get(position).getGp_name());
holder.tv_eg_name.setText(dataBeans.get(position).getGp_name_en());
holder.tv_request.setText(dataBeans.get(position).getRisk());
holder.tv_introduce.setText(dataBeans.get(position).getGp_intro());
holder.tv_expand_content.setText(dataBeans.get(position).getGp_gene_intro());
holder.ll_pack_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemPackUpClickListener.itemPackUpOnClick(position);
}
});
holder.iv_bg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemExpandOnClick.itemExpandOnClick(position);
}
});
if (dataBeans.get(position).isExpand()) {//這里是判斷是否顯示
holder.iv_expand.setVisibility(View.GONE);
holder.iv_bg.setClickable(false);
} else {
holder.iv_bg.setClickable(true);
holder.iv_expand.setVisibility(View.VISIBLE);
}
}
@Override
public int getItemCount() {
return dataBeans == null ? 0 : dataBeans.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView iv_bg;
private TextView tv_chinese_name;
private TextView tv_eg_name;
private TextView tv_request;
private ImageView iv_expand;
private RelativeLayout rl_expand;
private LinearLayout ll_pack_up;
private RelativeLayout rl_content;
private TextView tv_introduce;
private TextView tv_expand_content;
public MyViewHolder(View itemView) {
super(itemView);
iv_bg = itemView.findViewById(R.id.iv_bg);
tv_chinese_name = itemView.findViewById(R.id.tv_chinese_name);
tv_eg_name = itemView.findViewById(R.id.tv_eg_name);
tv_request = itemView.findViewById(R.id.tv_request);
iv_expand = itemView.findViewById(R.id.iv_expand);
rl_expand = itemView.findViewById(R.id.rl_expand);
ll_pack_up = itemView.findViewById(R.id.ll_pack_up);
rl_content = itemView.findViewById(R.id.rl_content);
tv_introduce = itemView.findViewById(R.id.tv_introduce);
tv_expand_content = itemView.findViewById(R.id.tv_expand_content);
}
}
public interface itemOnExpandClickListener {
void itemExpandOnClick(int pos);
}
public interface itemPackUpClickListener {
void itemPackUpOnClick(int pos);
}
普通,非常普通的RecycleViewAdpter. Activity中這樣賦值就可以了
private void initData() {
ListDataBean.DataBean listDataBean0 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test0", "測試0", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean1 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test1", "測試1", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean2 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test2", "測試2", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean3 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test3", "測試3", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean4 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test4", "測試4", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean5 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test5", "測試5", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean6 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test6", "測試6", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean7 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test7", "測試7", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean8 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test8", "測試8", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
ListDataBean.DataBean listDataBean9 = new ListDataBean.DataBean(R.drawable.icon_yaotunbi, "test9", "測試9", "這里是測試這里是測試這里是測試這里是測試這里是測試這里是測試", "測試測試測試測試測試測試測試測試測試測試測試測試測試測試", "正常");
dataBeans.add(listDataBean0);
dataBeans.add(listDataBean1);
dataBeans.add(listDataBean2);
dataBeans.add(listDataBean3);
dataBeans.add(listDataBean4);
dataBeans.add(listDataBean5);
dataBeans.add(listDataBean6);
dataBeans.add(listDataBean7);
dataBeans.add(listDataBean8);
dataBeans.add(listDataBean9);
listDataAdapter.setJyMoreDataBeans(dataBeans);
rcy_more_data.setAdapter(listDataAdapter);
}
/**
* 列表展開點擊事件
*
* @param pos
*/
@Override
public void itemExpandOnClick(int pos) {
dataBeans.get(pos).setExpand(true);
listDataAdapter.notifyItemChanged(pos);
}
/**
* 列表收起點擊事件
*
* @param pos
*/
@Override
public void itemPackUpOnClick(int pos) {
dataBeans.get(pos).setExpand(false);
listDataAdapter.notifyItemChanged(pos);
}
到這里呢,已經(jīng)可以完全的控制 顯示和隱藏了.但是非常的生硬,效果圖就不貼了,之前說到這個效果無非是顯示和隱藏,但是怎么能讓它絲滑流暢呢?加動畫~加屬性動畫!
首先看UI 在展開卡片的同時,卡片中間的文字上移,在收起的時候還原文字位置
首先是文字上移的代碼
//將文字置頂
public void changeTop(View view) {
ObjectAnimator translationX = new ObjectAnimator().ofFloat(view, "translationX", 0, 0);
ObjectAnimator translationY = new ObjectAnimator().ofFloat(view, "translationY", 0, -100f);
AnimatorSet animatorSet = new AnimatorSet(); //組合動畫
animatorSet.playTogether(translationX, translationY); //設(shè)置動畫
animatorSet.setDuration(300); //設(shè)置動畫時間
animatorSet.start();
}
移動的距離這個需要根據(jù)實際情況來,這里我需要向上移動.所以只設(shè)置y軸坐標就行.
還原文字
public void changeCentent(View view) {
ObjectAnimator translationX = new ObjectAnimator().ofFloat(view, "translationX", 0, 0);
ObjectAnimator translationY = new ObjectAnimator().ofFloat(view, "translationY", 0, 0f);
AnimatorSet animatorSet = new AnimatorSet(); //組合動畫
animatorSet.playTogether(translationX, translationY); //設(shè)置動畫
animatorSet.setDuration(300); //設(shè)置動畫時間
animatorSet.start();
}
讓它恢復(fù)到默認位置就可以
卡片展開動畫
public void animateOpen(View v, int mHiddenViewMeasuredHeight) {
v.setVisibility(View.VISIBLE);
ValueAnimator animator = createDropAnimator(v, 0, mHiddenViewMeasuredHeight);
animator.start();
}
卡片收起動畫
public void animateClose(final View view) {
int origHeight = view.getHeight();
ValueAnimator animator = createDropAnimator(view, 620, 0);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});
animator.start();
}
這里需要根據(jù)需求來定義展開的高度,和你在布局中的高度統(tǒng)一.
查不到就這樣了…要說難不算難,但是需要你細心的去思考一些問題.
下載地址--------------->https://github.com/HelloSinger/ListDemo