我正在尝试使用 CardViews 编写一个 RecyclerView 并使用 CAB 尝试在选择时删除多张卡片。我如何为所选卡片提供背景颜色。我正在尝试使用 statelistdrawable 如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/primary_dark" />
<item android:drawable="@android:color/transparent" />
</selector>
并将其应用于 CardView 布局:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/statelist_item_background"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageView"
android:tag="image_tag"
android:layout_width="72dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/one"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/textViewEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
下面是我的适配器的代码
public class modeladapter extends RecyclerView.Adapter<modeladapter.myholder> {
ArrayList<MyModel> arraylist;
SparseBooleanArray selecteditems;
public modeladapter(ArrayList<MyModel> ar) {
arraylist=ar;
selecteditems=new SparseBooleanArray();
}
public void removeData(int position) {
arraylist.remove(position);
notifyItemRemoved(position);
}
public MyModel getItem(int position) {
return arraylist.get(position);
}
public void addData(MyModel newModelData, int position) {
arraylist.add(position, newModelData);
notifyItemInserted(position);
}
public void toggleSelection(int pos) {
if (selecteditems.get(pos, false)) {
selecteditems.delete(pos);
}
else {
selecteditems.put(pos, true);
}
notifyItemChanged(pos);
}
public void clearSelections() {
selecteditems.clear();
notifyDataSetChanged();
}
public int getSelectedItemCount() {
return selecteditems.size();
}
public List<Integer> getSelectedItems() {
List<Integer> items = new ArrayList<Integer>(selecteditems.size());
for (int i = 0; i < selecteditems.size(); i++) {
items.add(selecteditems.keyAt(i));
}
return items;
}
@Override
public modeladapter.myholder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater lf=LayoutInflater.from(viewGroup.getContext());
View v = lf.inflate(R.layout.card_lay, viewGroup, false);
// v.setOnClickListener(Activity_Main.listener);
myholder m=new myholder(v);
return m;
}
@Override
public void onBindViewHolder(modeladapter.myholder m, int i) {
m.cimage.setImageResource(arraylist.get(i).url);
m.email.setText(arraylist.get(i).email);
m.name.setText(arraylist.get(i).name);
m.itemView.setActivated(selecteditems.get(i,false));
}
@Override
public int getItemCount() {return (arraylist.size());}
public static class myholder extends RecyclerView.ViewHolder
{
ImageView cimage;
TextView name,email;
public myholder(View itemView) {
super(itemView);
cimage= (ImageView) itemView.findViewById(R.id.imageView);
name= (TextView) itemView.findViewById(R.id.textViewName);
email= (TextView) itemView.findViewById(R.id.textViewEmail);
if(itemView.isActivated())
itemView.setBackgroundColor(itemView.getContext().getResources().getColor(R.color.primary_dark));
else
itemView.setBackgroundColor(Color.parseColor("#ffffff"));
}
}
请更新我如何更改所选项目的背景颜色。 谢谢
最佳答案
你不一定需要有一个StateListDrawable
您需要在适配器中有一个方法来检查选择:
sparseArray.valueAt(i).isSelected()
然后在您的 RecylcerView.Adapter 中,更改:
if (itemView.isActivated())
itemView.setBackgroundColor(itemView.getContext().getResources().getColor(R.color.primary_dark));
else
itemView.setBackgroundColor(Color.parseColor("#ffffff"));
To(您需要更改 cardView,而不是 itemView):
viewHolder.cardView.setCardBackgroundColor(sparseArray.valueAt(i).isSelected() ? Color.LTGRAY : Color.WHITE);
关于android - CardView 在 RecyclerView 中选择背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28177250/
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我有一个ids和他们的分数的散列,它是这样的:@objects={1=>57,4=>12,3=>9,5=>3,55=>47,32=>39,17=>27,29=>97,39=>58}我怎样才能选出前五名并放弃其余的?我这样做:@orderedObject=@objects.sort_by{|k,v|v}.reverse=>[[29,97],[39,58],[1,57],[55,47],[32,39],[17,27],[4,12],[3,9],[5,3]]然后我这样做:只有@orderedObjects的键:@keys=@orderedObject.map{|key,value|key}这
我的任务是从数组中选择最高和最低的数字。我想我很清楚我想做什么,但只是努力以正确的格式访问信息以满足通过标准。defhigh_and_low(numbers)array=numbers.split("").map!{|x|x.to_i}array.sort!{|a,b|ba}putsarray[0,-1]end数字可能看起来像"80917234100",要通过,我需要输出"9234"。我正在尝试putsarray.first.last,但一直无法弄明白。 最佳答案 有Array#minmax完全满足您需要的方法:array=[80,
我在View中有这段代码prawn_document(:page_size=>"A4",:top_margin=>80,:bottom_margin=>40,:background=>"public/uploads/1.png")do|pdf|creation_date=Time.now.strftime('%d-%m-%Y')posts=@posts.eachdo|post|pdf.pad(10)dopdf.textpost.titlepdf.textpost.textendendpdf.page_count.timesdo|i|pdf.go_to_page(i+1)pdf.draw
这是两个脚本的故事,与previousquestion有关.这两个脚本位于http://gist.github.com/50692.ansi.rb脚本在所有256种背景颜色上显示所有256种颜色。ncurses.rb脚本显示所有256种前景颜色,但背景显示基本的16种颜色,然后似乎循环显示各种属性,如闪烁和反向视频。那么是什么给了?这是ncurses中的错误,它使用带符号的整数来表示颜色对吗?(即'tputcolors'表示256但'tputpairs'表示32767而不是65536)似乎如果是这种情况,颜色对的前半部分会正确显示但后半部分会重复或进入属性作为int包裹。
这是我的下拉菜单,我想在它的值选项上选择它01-Liveanimals我知道如何选择下拉内容,即ie.select_list(:id,"DropDownList_Product").select("01-Liveanimals")实际上我想选择其值01的下拉菜单,我应该为此做什么? 最佳答案 像这样的东西应该可以工作:ie.select_list(:id,"DropDownList_Product").select_value("01")更多信息请访问http://rdoc.info/gems/watir-webdriver/Wat
我正在尝试使用“电子表格”解析Excel电子表格。如何获取每一行的背景颜色? 最佳答案 book=Spreadsheet::Workbook.newsheet=book.create_worksheet:name=>'Name'format=Spreadsheet::Format.new:color=>:blue,:pattern_fg_color=>:yellow,:pattern=>1sheet.row(0).set_format(0,format)#forfirstcellinfirstrow或sheet.row(0).def
我有一张背景图片,我无法只停留在一页上。我制作了一个带有一个主视图的欢迎Controller来显示它。我也在预编译我的Assets。背景显示得很好,但我的目标是只在我的home.html.erbView中显示背景图像。欢迎/home.html.erb:"lang="">title欢迎Controller:classWelcomeController样式表/welcome.css.scss:body{background:{image:asset-url("image.jpg");}}我的应用程序布局中有以下内容:在config/initializers/assets.rb中:Rails