<分区> 分区>
我在 fragment 底部有一个 RecyclerView 水平图像 slider 。 fragment 的顶部显示了一些细节。一旦用户单击底部的图像,我们的想法是从图像 slider 中删除该图像并在 fragment 中显示其信息。现在显示了信息,但图像没有从 RecyclerView 中删除。这是我在最外层布局的 Onclick 中编写的代码。我已经尝试了所有我能找到的相关答案,但没有任何效果。它们都在代码中。请让我知道我做错了什么或缺少什么。
holder.itemRowRelativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFiltering) {
mItemList.clear();
mItemList.addAll(mOriginalItemList);
mItemList.remove(position);// At this point mItemList holds the correct. That is all the images but not the one that was clicked.
notifyItemRemoved(position); //solution 1
notifyItemRangeRemoved(position, getItemCount()); // solution 2
notifyItemRangeRemoved(0, getItemCount()); // solution 3
notifyDataSetChanged();//solution 4
}
}
});
适配器的完整代码
public class ImageGallery16X9Adapter<T extends GalleryItem> extends RecyclerView.Adapter<ImageGallery16X9Adapter.GalleryItemViewHolder> {
public enum GalleryMode {
All_SAME,
FIRST_DIFFERENT
}
private Context mContext;
private BasePresenter mPresenter;
private List<T> mItemList;
private List<T> mOriginalItemList;
private GalleryItem mFirstItem;
private GalleryMode mGalleryMode;
private int deviceWidth, itemWidth, marginSingle, marginDouble;
private boolean isFiltering;
public ImageGallery16X9Adapter(Context context, BasePresenter presenter, GalleryMode galleryMode, List<T> itemList, GalleryItem firstItem, boolean isFiltering) {
mContext = context;
mPresenter = presenter;
mGalleryMode = galleryMode;
mItemList = new ArrayList<>(itemList);
mOriginalItemList = new ArrayList<>(itemList);
mFirstItem = firstItem;
deviceWidth = CommonUtils.getDeviceWidth((Activity) mContext);
itemWidth = (int) (deviceWidth * 0.9);
marginDouble = (int) (deviceWidth * 0.05);
marginSingle = (int) (deviceWidth * 0.025);
this.isFiltering = isFiltering;
}
@Override
public GalleryItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new GalleryItemViewHolder(LayoutInflater.from(parent.getContext()).
inflate(R.layout.row_image_gallery_16x9_item, parent, false));
}
@Override
public void onBindViewHolder(GalleryItemViewHolder holder, final int position) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemRowRelativeLayout.getLayoutParams();
RelativeLayout.LayoutParams rlParams = (RelativeLayout.LayoutParams) holder.itemImageView.getLayoutParams();
layoutParams.width = itemWidth;
rlParams.height = (int) (layoutParams.width * Constant.HEIGHT_FACTOR_16X9);
if (position == 0) {
layoutParams.leftMargin = marginDouble;
layoutParams.rightMargin = 0;
if (mGalleryMode == GalleryMode.FIRST_DIFFERENT) {
holder.itemTitle.setVisibility(View.VISIBLE);
holder.itemTitle.setText(mFirstItem.getItemTitle());
if (mFirstItem.getItemImage() != null) {
Picasso.with(MyApplication.getAppContext()).load(mFirstItem.getItemImage()).fit().placeholder(R.drawable.error_image).error(R.drawable.error_image).into(holder.itemImageView);
} else {
Picasso.with(MyApplication.getAppContext()).load(R.drawable.error_image).placeholder(R.drawable.error_image).error(R.drawable.error_image).fit().into(holder.itemImageView);
}
holder.itemDescription.setText(mFirstItem.getItemDescription());
}
} else {
if (mGalleryMode == GalleryMode.FIRST_DIFFERENT) {
if (position == mItemList.size()) {
layoutParams.rightMargin = marginDouble;
} else {
layoutParams.rightMargin = 0;
}
} else {
if (position == mItemList.size() - 1) {
layoutParams.rightMargin = marginDouble;
} else {
layoutParams.rightMargin = 0;
}
}
layoutParams.leftMargin = marginSingle;
}
int itemPosition = position;
if (mGalleryMode == GalleryMode.FIRST_DIFFERENT && position > 0) {
itemPosition = position - 1;
T item = mItemList.get(itemPosition);
holder.itemTitle.setVisibility(View.GONE);
holder.itemDescription.setText(item.getItemDescription());
Picasso.with(mContext).load(item.getItemImage()).fit().placeholder(R.drawable.error_image).error(R.drawable.error_image).into(holder.itemImageView);
} else if (mGalleryMode == GalleryMode.All_SAME) {
T item = mItemList.get(itemPosition);
holder.itemTitle.setVisibility(View.GONE);
holder.itemDescription.setText(item.getItemDescription());
Picasso.with(mContext).load(item.getItemImage()).fit().placeholder(R.drawable.error_image).error(R.drawable.error_image).into(holder.itemImageView);
}
holder.itemRowRelativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mGalleryMode == GalleryMode.FIRST_DIFFERENT) {
if (position == 0) {
mPresenter.onItemClicked(mFirstItem);
} else {
mPresenter.onItemClicked(mItemList.get(position - 1));
}
} else {
mPresenter.onItemClicked(mItemList.get(position));
if (isFiltering) {
mItemList.clear();
mItemList.addAll(mOriginalItemList);
mItemList.remove(position);
notifyItemRemoved(position); //solution 1
notifyItemRangeRemoved(position, getItemCount()); // solution 2
notifyItemRangeRemoved(0, getItemCount()); // solution 3
notifyDataSetChanged();//solution 4
}
}
}
});
}
@Override
public int getItemCount() {
if (mGalleryMode == GalleryMode.FIRST_DIFFERENT)
return mItemList.size() + 1;
else
return mItemList.size();
}
static class GalleryItemViewHolder extends RecyclerView.ViewHolder {
private final TextView itemDescription, itemTitle;
private final ImageView itemImageView, itemFavoriteImageView;
private final RelativeLayout itemRowRelativeLayout;
public GalleryItemViewHolder(View itemView) {
super(itemView);
itemRowRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.rl_gallery_item_row);
itemImageView = (ImageView) itemView.findViewById(R.id.img_gallery_item);
itemFavoriteImageView = (ImageView) itemView.findViewById(R.id.img_gallery_item_favorite);
itemTitle = (TextView) itemView.findViewById(R.id.txt_gallery_item_name);
itemDescription = (TextView) itemView.findViewById(R.id.txt_gallery_item_description);
}
}
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?