我正在努力使用带有一些听众的布局的RecyclerView。
代码是这个
ViewHolder
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tickerSymbol;
public TextView companyName;
public View highlightBuy;
public LinearLayout categoryImage;
public LayoutParams categoryLayoutParams;
public LinearLayout arrowImage;
public ImageView categoryIconSeparator;
public ImageView arrowSeparator;
public ImageView arrowIcon;
public RelativeLayout stockRow;
public LinearLayout tickerInfo;
public Boolean isCategoryOpened = false;
public ViewHolder(View itemView) {
super(itemView);
this.tickerSymbol = (TextView) itemView.findViewById(R.id.ticker_symbol_textview);
this.companyName = (TextView) itemView.findViewById(R.id.company_name_textview);
this.highlightBuy = itemView.findViewById(R.id.highlight_buy_view);
this.categoryImage = (LinearLayout) itemView.findViewById(R.id.category_image);
this.categoryLayoutParams = (LayoutParams) categoryImage.getLayoutParams();
this.arrowImage = (LinearLayout) itemView.findViewById(R.id.arrow);
this.categoryIconSeparator = (ImageView) itemView.findViewById(R.id.category_icon_separator);
this.arrowSeparator = (ImageView) itemView.findViewById(R.id.arrow_separator);
this.arrowIcon = (ImageView) itemView.findViewById(R.id.arrow_icon_imageview);
this.stockRow = (RelativeLayout) itemView.findViewById(R.id.stock_row);
this.tickerInfo = (LinearLayout) itemView.findViewById(R.id.ticker_info);
}
}
onCreateViewHolder
@Override
public StockListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View stockView = inflater.inflate(R.layout.stock_list_row, parent, false);
return new StockListAdapter.ViewHolder(stockView);
}
onBindViewHolder
@Override
public void onBindViewHolder(final StockListAdapter.ViewHolder holder, final int position) {
final Stock stock = mStocks.get(position);
final int collapsedCategoryWidth = (int) mContext.getResources().getDimension(R.dimen.collapsed_category_icon_width);
final int stockRowHeight = (int) mContext.getResources().getDimension(R.dimen.stock_row_height);
//Reset holder status
if (holder.isCategoryOpened) {
holder.isCategoryOpened = false;
holder.categoryImage.getLayoutParams().width = collapsedCategoryWidth;
holder.categoryImage.setLayoutParams(holder.categoryImage.getLayoutParams());
holder.arrowIcon.setRotationY(0);
}
holder.tickerSymbol.setText(stock.ticker);
holder.companyName.setText(stock.name);
holder.companyName.setSelected(true);
holder.companyName.requestFocus();
holder.categoryImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int newWidth;
final int startWidth;
final int startArrowAngle;
final int newArrowAngle;
if (!holder.isCategoryOpened) {
holder.isCategoryOpened = true;
newWidth = holder.stockRow.getWidth() - holder.categoryLayoutParams.leftMargin - holder.arrowImage.getWidth() - ((LayoutParams) holder.arrowImage.getLayoutParams()).rightMargin - holder.categoryIconSeparator.getWidth();
startWidth = collapsedCategoryWidth;
startArrowAngle = 0;
newArrowAngle = 180;
} else {
holder.isCategoryOpened = false;
newWidth = collapsedCategoryWidth;
startWidth = holder.categoryImage.getWidth();
startArrowAngle = 180;
newArrowAngle = 0;
}
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
holder.categoryLayoutParams.width = startWidth + (int) ((newWidth - startWidth) * interpolatedTime);
//MAYBE PROBLEM HERE??
holder.categoryImage.setLayoutParams(holder.categoryLayoutParams);
holder.arrowIcon.setRotationY(startArrowAngle + (newArrowAngle - startArrowAngle) * interpolatedTime);
}
};
a.setDuration(500);
holder.categoryImage.startAnimation(a);
}
});
我会附上我的错误样本。
当我第一次点击一个视图时,onClick工作成功,但如果我滚动然后返回到该视图,只有箭头被翻转(这意味着onClick被调用)但是LinearLayout Width不会被修改。
但现在真正奇怪的是,如果我点击之前没有点击的其他视图,或者(我猜)还没有被回收,则会发生以下情况:
所以我想,Width已设置,但未显示。
我真的不知道如何解决这个问题。
感谢帮助或其他想法!
这是我已经尝试过的:
答案 0 :(得分:1)
如果您已点击该项目,则在商店类中将1个变量添加到存储值中。
像这样的ilustration,我在商店中设置变量isSelected: private boolean mSelected=false;
public boolean getIsSelected(){
return this.mSelected;
}
public void setIsSelected(boolean mSelected){
this.mSelected=mSelected;
}
然后在你的onClick方法中,只需添加stock.setIsSelected(!stock.getIsSelected())
if条件并将if条件设置为从stock.getIsSelected()
读取布尔值,然后从持有者中删除变量isCategoryOpened。
public void onClick(View v) {
final int newWidth;
final int startWidth;
final int startArrowAngle;
final int newArrowAngle;
if (stock.getIsSelected()) {
newWidth = holder.stockRow.getWidth() - holder.categoryLayoutParams.leftMargin - holder.arrowImage.getWidth() - ((LayoutParams) holder.arrowImage.getLayoutParams()).rightMargin - holder.categoryIconSeparator.getWidth();
startWidth = collapsedCategoryWidth;
startArrowAngle = 0;
newArrowAngle = 180;
} else {
newWidth = collapsedCategoryWidth;
startWidth = holder.categoryImage.getWidth();
startArrowAngle = 180;
newArrowAngle = 0;
}
stock.setIsSelected(!stock.getIsSelected())
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
holder.categoryLayoutParams.width = startWidth + (int) ((newWidth - startWidth) * interpolatedTime);
//MAYBE PROBLEM HERE??
holder.categoryImage.setLayoutParams(holder.categoryLayoutParams);
holder.arrowIcon.setRotationY(startArrowAngle + (newArrowAngle - startArrowAngle) * interpolatedTime);
}
};
a.setDuration(500);
holder.categoryImage.startAnimation(a);
}
如果我错了,请纠正我:)
答案 1 :(得分:0)
问题解决了,造成所有这些错误的原因是holder.companyName.requestFocus()
我正在调用以在TextView中使用选框。
它出来我不需要requestFocus()
才能让它正常工作,而且这是导致问题的原因。