CardView 设置 match_parent 无效 而且 动画效果无效

来源:互联网 发布:自动缝包机 知乎 编辑:程序博客网 时间:2024/06/10 08:51

在 recyclerView 中使用 cardView 作为 item ,发现match_parent无效,内容都挤到了左边,只有设置cardView 具体宽度才可以,这样肯定是不行的
我开始是这样写的

View view  = LayoutInflater.from(MainActivity.this).inflate(R.layout.content_item,null);

就造成了无法适配,接着改成

View view = View.inflate(MainActivity.this,R.layout.content_item,null);

依然也不可以,最后改成如下,

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.content_item,parent,false);

参数3表示是否需要将参数1的Layout资源依附于参数2的ViewGroup上,false表示不依附,系统已经默认将Layout插入至ViewGroup中,如果参数3是true的话,将添加一层冗余的视图。

好啦,成功 match_parent。
顺便附上 stackOverFlow 的文章
http://stackoverflow.com/questions/24503760/cardview-layout-width-match-parent-does-not-match-parent-recyclerview-width

同时,cardView 默认无法点击,需要设置如下:

 android:clickable="true" android:foreground="?android:attr/selectableItemBackground" android:stateListAnimator="@animator/touch"

即 需要设置 clickable 为 true,foreground 点击会产生涟漪效果,动画 touch 是改变 cardView 的
translationZ ,即每次点击 cardView 时 cardView 的 Z 轴会向上浮动,产生动画效果,如下:

  <item android:state_enabled="true" android:state_pressed="true">        <objectAnimator            android:duration="***"            android:propertyName="translationZ"            android:valueTo="50dp"            android:valueType="floatType" />  </item>
0 0
原创粉丝点击