How to add margin to 2x2 items view in grid view? - android

Want to add margin to gridview in the following manner.
Left margin for 2,4, 2,9, 12,14,..
Right margin for 3,5, 8,10, 13,15,..
Here is my code snippet which I tried to set margin to 2x2 items in grid view...
i.e positions - ..,2,3,4,5,..,7,8,9,10,..,12,13,14,15,..,17,18,19,20,.. like wise.
for 2,3,4,5 & 12,13,14,15,.. Getting the expected output. but my case failed for 7,8,9,10 & 17,18,19,20 series.
Help me to solve this. Confused to solve this mathematical puzzle.
Thanks in advance.
public class BoxFeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<BoxFeedResult> boxFeedResults;
private Context mContext;
private RecyclerViewItemClickListener viewItemClickListener;
private final int LARGE_TYPE = 0;
private final int SMALL_TYPE = 1;
private final int PROGRESS_TYPE = 2;
private int GRID_VIEW_MARGIN_LEFT = 0;
private int GRID_VIEW_MARGIN_RIGHT = 0;
private int GRID_VIEW_MARGIN_TOP = 0;
private int GRID_VIEW_MARGIN_BOTTOM = 0;
private int GRID_VIEW_ROW_MARGIN = 0;
public BoxFeedAdapter(Context context, RecyclerViewItemClickListener viewItemClickListener) {
this.mContext = context;
this.viewItemClickListener = viewItemClickListener;
boxFeedResults = new ArrayList<>();
GRID_VIEW_MARGIN_LEFT = context.getResources().getDimensionPixelOffset(R.dimen.empty_dimen);
GRID_VIEW_MARGIN_RIGHT = context.getResources().getDimensionPixelOffset(R.dimen.empty_dimen);
GRID_VIEW_MARGIN_TOP = context.getResources().getDimensionPixelOffset(R.dimen.channel_row_padding);
GRID_VIEW_MARGIN_BOTTOM = context.getResources().getDimensionPixelOffset(R.dimen.empty_dimen);
GRID_VIEW_ROW_MARGIN = context.getResources().getDimensionPixelOffset(R.dimen.channel_row_padding_left);
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == SMALL_TYPE) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_box_feed_series_banner, null);
return new BoxFeedSeriesListViewHolder(layoutView);
} else if (viewType == LARGE_TYPE) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_box_feed_episode_banner, null);
return new BoxFeedEpisodeViewHolder(layoutView);
} else {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_item, null);
return new ProgressViewHolder(layoutView);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof BoxFeedSeriesListViewHolder) {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(false);
if ((position - 1) % 2 == 0) {
layoutParams.setMargins(GRID_VIEW_ROW_MARGIN, GRID_VIEW_MARGIN_TOP, GRID_VIEW_MARGIN_RIGHT, GRID_VIEW_MARGIN_BOTTOM);
} else {
layoutParams.setMargins(GRID_VIEW_MARGIN_LEFT, GRID_VIEW_MARGIN_TOP, GRID_VIEW_ROW_MARGIN, GRID_VIEW_MARGIN_BOTTOM);
}
holder.itemView.setLayoutParams(layoutParams);
final BoxFeedResult singleFeed = boxFeedResults.get(position);
final BoxFeedSeriesListViewHolder viewHolder = (BoxFeedSeriesListViewHolder) holder;
Glide.with(mContext)
.load(singleFeed.getA4MediumUrl())
.placeholder(R.drawable.bg_black)
.into(viewHolder.seriesImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, singleFeed.getId(), Toast.LENGTH_SHORT).show();
viewItemClickListener.onItemClickListioner(singleFeed, viewHolder.seriesImage, position);
}
});
} else if (holder instanceof BoxFeedEpisodeViewHolder) {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
final BoxFeedResult singleFeed = boxFeedResults.get(position);
final BoxFeedEpisodeViewHolder viewHolder = (BoxFeedEpisodeViewHolder) holder;
Glide.with(mContext)
.load(singleFeed.getAspectMediumUrl())
.placeholder(R.drawable.bg_black)
.into(viewHolder.episodeImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, singleFeed.getId(), Toast.LENGTH_SHORT).show();
}
});
} else {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
}
}
#Override
public int getItemCount() {
return boxFeedResults.size();
}
#Override
public int getItemViewType(int position) {
switch (boxFeedResults.get(position).getTileType()) {
case “large":
return LARGE_TYPE;
case “small":
return SMALL_TYPE;
default:
return PROGRESS_TYPE;
}
}
public void addList(List<BoxFeedResult> feedResults) {
this.boxFeedResults = feedResults;
notifyDataSetChanged();
}
}
Expected output is

Related

RecyclerView use so much memory until OOM

"Memory" rose quickly after open the Fragment that contains RecyclerView.When scorll up and down several times,"memory" continue to rise,and then show OOM and Force Close.
The following is the RecyclerView screenshot:
The following is my Adapter code:
I just use one RecyclerView the full page and nested a GridView at the first item;
public class CategoryPagerItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//热门分类
public static final int VIEW_TYPE_HOT_SUB_CATE = 1;
//banner+商品横滑推荐
public static final int VIEW_TYPE_PROMOTION_LIST = 2;
public static final int VIEW_TYPE_PROMOTION_LIST_LABEL = 3;
//商品集合
public static final int VIEW_TYPE_GOODS_LIST = 4;
//商品集合标签
public static final int VIEW_TYPE_GOODS_LIST_LABEL = 5;
//加载更多
public static final int VIEW_TYPE_GOODS_LIST_MORE = 6;
public static final int COUNT_HOT_SUB_CATE=1;
public static final int COUNT_HOT_GOODS_LIST_LABEL=1;
public static final int COUNT_HOT_GOODS_LIST_MORE=1;
private String mTagId;//TabLayout的title对应的tagid
private String mTitleName;//TabLayout的title对应的name
private int mIndex = -1;//ViewPager的index
private int mLevel = 1;//级别分类
private Context mContext;
//相关的banner+横滑商品
private ArrayList<CategoryList.RelatedTagListEntity> mRelatedTagListEntityList;
//热门分类
private ArrayList<CategoryList.HotSubCategoryListEntity> mHotSubCategoryListEntityList;
//商品列表
private ArrayList<CategoryList.GoodListEntity> mGoodListEntityList;
private int mRelatedTagSize, mGoodsListSize;
private OnGoodsItemClickListener mOnGoodsItemClickListener;
public CategoryPagerItemAdapter(Context context, String tagId, String titleName, int index) {
mTagId = tagId;
mTitleName = titleName;
mIndex = index;
mContext = context;
}
public void resetData(ArrayList<CategoryList.HotSubCategoryListEntity> hotSubCategoryListEntityList,
ArrayList<CategoryList.RelatedTagListEntity> relatedTagListEntityList,
ArrayList<CategoryList.GoodListEntity> goodListEntityList, int level) {
mHotSubCategoryListEntityList = hotSubCategoryListEntityList;
mRelatedTagListEntityList = relatedTagListEntityList;
mGoodListEntityList = goodListEntityList;
mRelatedTagSize = mRelatedTagListEntityList == null ? 0 : mRelatedTagListEntityList.size();
mGoodsListSize = mGoodListEntityList == null ? 0 : mGoodListEntityList.size();
mLevel = level;
notifyDataSetChanged();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
//头部的十个分类
if (viewType == VIEW_TYPE_HOT_SUB_CATE) {
return new HotSubCateViewHolder(layoutInflater.inflate(R.layout.category_classify_item_hot_sub_cate, parent, false));
} else if (viewType == VIEW_TYPE_PROMOTION_LIST) {//banner+商品横滑推荐
return new PromotionViewHolder(layoutInflater.inflate(R.layout.category_classify_item_related, parent, false));
} else if (viewType == VIEW_TYPE_GOODS_LIST) {//商品列表
return new GoodListViewHolder(layoutInflater.inflate(R.layout.category_classify_item_goods_list, parent, false));
} else if (viewType == VIEW_TYPE_GOODS_LIST_LABEL) {
return new GoodListLabelViewHolder(layoutInflater.inflate(R.layout.category_classify_item_goods_list_label, parent, false));
} else if (viewType == VIEW_TYPE_GOODS_LIST_MORE) {//更多
return new MoreGoodsListViewHolder(layoutInflater.inflate(R.layout.category_classify_item_more, parent, false));
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder vh, final int position) {
switch (vh.getItemViewType()) {
//热门分类
case VIEW_TYPE_HOT_SUB_CATE:
final HotSubCateViewHolder hotSubCateHolder = (HotSubCateViewHolder) vh;
int horizontalSpacing = (UJiPin.SCREEN_WIDTH - UDensity.dip2px(mContext, 16) * 2 - UDensity.dip2px(mContext, 50) * 5) / 4;
hotSubCateHolder.mGridview.setHorizontalSpacing(horizontalSpacing);
hotSubCateHolder.mGridview.setVerticalSpacing(UDensity.dip2px(mContext, 20));
final CategoryHotSubCateAdapter categoryHotSubCateAdapter = new CategoryHotSubCateAdapter(mContext, mHotSubCategoryListEntityList);
hotSubCateHolder.mGridview.setAdapter(categoryHotSubCateAdapter);
hotSubCateHolder.mGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
int hotSubViewType = categoryHotSubCateAdapter.getItemViewType(pos);
CategoryList.HotSubCategoryListEntity hotSubCategoryListEntity = mHotSubCategoryListEntityList.get(pos);
if (hotSubViewType == CategoryHotSubCateAdapter.VIEW_TYPE_HOT_SUB_CATE) {//热门分类
JumpUtils.JumpClassifyList((BaseActivity) mContext, hotSubCategoryListEntity.getLevel(), hotSubCategoryListEntity.getTag_id(), hotSubCategoryListEntity.getName());
UJPTrack.getInstance().setTrack((BaseActivity) mContext, UJPTrack.PAGE_CLASS_ + UString.urlEncode(hotSubCategoryListEntity.getName()),
"class-1-" + UString.urlEncode(hotSubCategoryListEntity.getName()));
} else if (hotSubViewType == CategoryHotSubCateAdapter.VIEW_TYPE_HOT_SUB_CATE_MORE) {//点击more
Intent intent = new Intent(mContext, CategoryPagerMoreActivity.class);
intent.putExtra(CategoryPagerMoreActivity.EXTRA_STRING_CATEGORY_NAME, mTitleName);
intent.putExtra(CategoryPagerMoreActivity.EXTRA_INT_CATEGORY_INDEX, mIndex);
intent.putExtra(CategoryPagerMoreActivity.EXTRA_STRING_CATEGORY_TAG_ID, mTagId);
mContext.startActivity(intent);
UJPTrack.getInstance().setTrack((BaseActivity) mContext,
UJPTrack.PAGE_CLASS_ + UString.urlEncode(mTitleName),
"class-1-more");
}
}
});
break;
case VIEW_TYPE_PROMOTION_LIST://banner+横滑
PromotionViewHolder promotionViewHolder = (PromotionViewHolder) vh;
final int promotionPosition = position - COUNT_HOT_SUB_CATE;
//防止recyclerview复用出现错乱的办法是有if必须有else,并且处理相反
GridLayoutManager.LayoutParams promotionLP = (GridLayoutManager.LayoutParams) promotionViewHolder.mContainer.getLayoutParams();
if (promotionPosition == 0) {//如果是第一个,则显示“相关推荐“
promotionViewHolder.mLabel.setVisibility(View.VISIBLE);
promotionLP.setMargins(-UDensity.dip2px(mContext, 5), UDensity.dip2px(mContext, 16), -UDensity.dip2px(mContext, 5), 0);
} else {
promotionViewHolder.mLabel.setVisibility(View.GONE);
promotionLP.setMargins(-UDensity.dip2px(mContext, 5), UDensity.dip2px(mContext, 4), -UDensity.dip2px(mContext, 5), 0);
}
promotionViewHolder.mContainer.setLayoutParams(promotionLP);
final CategoryList.RelatedTagListEntity relatedTagListEntity = mRelatedTagListEntityList.get(promotionPosition);
FrescoImageLoader.getInstance().load(promotionViewHolder.mCover, relatedTagListEntity.getCover());
//点击banner
promotionViewHolder.mCover.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JumpUtils.JumpActivityTopic((BaseActivity) mContext, relatedTagListEntity.getName(),
relatedTagListEntity.getTag_id(), null, promotionPosition);
UJPTrack.getInstance().setTrack((BaseActivity) mContext, UJPTrack.PAGE_LABEL_ + UString.urlEncode(relatedTagListEntity.getName()),
"class-2-" + covertInt(promotionPosition + 1));
}
});
final CategoryChoiceTagAdapter adapter = new CategoryChoiceTagAdapter(mContext);
//点击横滑商品
adapter.setOnItemClickListener(new CategoryChoiceTagAdapter.OnItemClickListener() {
#Override
public void onTag4ItemClick(final CategoryList.GoodListEntity goodListEntity, int position, View view) {
Jumper.newJumper().putString(GoodsDetailActivity.EXTRA_STRING_GOODID, goodListEntity.getGoods_id() + "")
.putString(GoodsDetailActivity.EXTRA_STRING_SOURCENAME, "分类")
.putString(GoodsDetailActivity.EXTRA_STRING_SOURCEID, "活动横滑")
.jump((BaseActivity) mContext, GoodsDetailActivity.class);
UJPTrack.getInstance().setTrack((BaseActivity) mContext, UJPTrack.PAGE_GOOD + goodListEntity.getGoods_id(),
"class-2-" + covertInt(promotionPosition + 1));
}
});
promotionViewHolder.mSlideMoreRecycleView.setAdapter(adapter);
adapter.setDataRefresh(relatedTagListEntity.getGoods_list());
//横滑
promotionViewHolder.mSlideMoreRecycleView.setOnSlideTwiceLisner(new SlideMoreRecycleView.SlideTwiceLisner() {
#Override
public void onSliding(RecyclerView recyclerView) {
JumpUtils.JumpActivityTopic((BaseActivity) mContext, relatedTagListEntity.getName(),
relatedTagListEntity.getTag_id(), "", promotionPosition);
}
});
break;
case VIEW_TYPE_GOODS_LIST_LABEL://商品列表的label
break;
case VIEW_TYPE_GOODS_LIST://商品列表
int goodsListPosition = position - COUNT_HOT_SUB_CATE - mRelatedTagSize - COUNT_HOT_GOODS_LIST_LABEL;
final CategoryList.GoodListEntity goodListEntity = mGoodListEntityList.get(goodsListPosition);
final GoodListViewHolder goodsListHolder = (GoodListViewHolder) vh;
ViewCompat.setTransitionName(goodsListHolder.ivCover, mContext.getString(R.string.transition_name) + position);
goodsListHolder.rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mOnGoodsItemClickListener != null) {
mOnGoodsItemClickListener.onItemClick(goodsListHolder.rootView, goodListEntity, position, goodListEntity.getThumbnail());
}
}
});
goodsListHolder.tv_goods_selling_point.setText(goodListEntity.getSelling_point());
if (!TextUtils.isEmpty(goodListEntity.getThumbnail()))
FrescoImageLoader.getInstance().load(goodsListHolder.ivCover, goodListEntity.getThumbnail());
if (!TextUtils.isEmpty(goodListEntity.getMark())) {
goodsListHolder.ivGroupLogo.setVisibility(View.VISIBLE);
FrescoImageLoader.getInstance().load(goodsListHolder.ivGroupLogo, goodListEntity.getMark());
} else {
goodsListHolder.ivGroupLogo.setVisibility(View.GONE);
}
goodsListHolder.tvDescription.setText(goodListEntity.getGoods_name());
if (!TextUtils.isEmpty(goodListEntity.getGoods_price()))
goodsListHolder.tvPrice.setText(String.format(mContext.getString(R.string.rmb), goodListEntity.getGoods_price()));
//售罄
if (goodListEntity.getProduct_stock_total() <= 0)
goodsListHolder.ivSoldOut.setVisibility(View.VISIBLE);
else
goodsListHolder.ivSoldOut.setVisibility(View.GONE);
break;
case VIEW_TYPE_GOODS_LIST_MORE://更多推荐
MoreGoodsListViewHolder moreGoodsListViewHolder = (MoreGoodsListViewHolder) vh;
moreGoodsListViewHolder.mContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JumpUtils.JumpClassifyList((BaseActivity) mContext, mLevel, mTagId, mTitleName);
UJPTrack.getInstance().setTrack((BaseActivity) mContext, UJPTrack.PAGE_CLASS_ + UString.urlEncode(mTitleName), UJPTrack.PAGE_CLASS_CLASS + UString.urlEncode(mTitleName));
}
});
break;
}
}
#Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder);
}
#Override
public int getItemViewType(int position) {
if (position == 0) {
return VIEW_TYPE_HOT_SUB_CATE;
} else if (position < COUNT_HOT_SUB_CATE + mRelatedTagSize) {
return VIEW_TYPE_PROMOTION_LIST;
} else if (position < COUNT_HOT_SUB_CATE + mRelatedTagSize + COUNT_HOT_GOODS_LIST_LABEL) {
return VIEW_TYPE_GOODS_LIST_LABEL;
} else if (position < COUNT_HOT_SUB_CATE + mRelatedTagSize + COUNT_HOT_GOODS_LIST_LABEL + mGoodsListSize) {
return VIEW_TYPE_GOODS_LIST;
} else {
return VIEW_TYPE_GOODS_LIST_MORE;
}
}
#Override
public int getItemCount() {
return mGoodsListSize + mRelatedTagSize + COUNT_HOT_SUB_CATE +
COUNT_HOT_GOODS_LIST_LABEL + COUNT_HOT_GOODS_LIST_MORE;
}
//头部的热门分类
private static class HotSubCateViewHolder extends RecyclerView.ViewHolder {
private MyGridview mGridview;
HotSubCateViewHolder(View view) {
super(view);
mGridview = (MyGridview) view.findViewById(R.id.grid_view);
}
}
//banner+推荐横滑
private static class PromotionViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView mCover;
private LinearLayout mContainer;
private TextView mLabel;
private SlideMoreRecycleView mSlideMoreRecycleView;
PromotionViewHolder(View view) {
super(view);
mContainer = (LinearLayout) view.findViewById(R.id.ll);
mLabel = (TextView) view.findViewById(R.id.label);
mCover = (SimpleDraweeView) view.findViewById(R.id.iv_cover);
mSlideMoreRecycleView = (SlideMoreRecycleView) view.findViewById(rv_tag_list);
}
}
//商品列表
private static class GoodListViewHolder extends RecyclerView.ViewHolder {
View rootView;
SimpleDraweeView ivCover;
TextView tvDescription;
TextView tvPrice;
TextView tv_goods_selling_point;
ImageView ivSoldOut;
SimpleDraweeView ivGroupLogo;
GoodListViewHolder(View v) {
super(v);
rootView = v;
ivCover = (SimpleDraweeView) v.findViewById(R.id.iv_cover);
tvDescription = (TextView) v.findViewById(R.id.tv_descrption);
tvPrice = (TextView) v.findViewById(R.id.tv_price);
tv_goods_selling_point = (TextView) v.findViewById(R.id.tv_goods_selling_point);
ivSoldOut = (ImageView) v.findViewById(R.id.iv_sold_out);
ivGroupLogo = (SimpleDraweeView) v.findViewById(R.id.iv_group_tag);
}
}
private static class GoodListLabelViewHolder extends RecyclerView.ViewHolder {
TextView mLabel;
public GoodListLabelViewHolder(View itemView) {
super(itemView);
mLabel = (TextView) itemView.findViewById(R.id.label);
}
}
private static class MoreGoodsListViewHolder extends RecyclerView.ViewHolder {
private LinearLayout mContainer;
MoreGoodsListViewHolder(View itemView) {
super(itemView);
mContainer = (LinearLayout) itemView.findViewById(R.id.container);
}
}
//点击双栏列表接口
public interface OnGoodsItemClickListener {
void onItemClick(View rootView, CategoryList.GoodListEntity goodListEntity, int position, String imageUrl);
}
public void addOnGoodsItemClickListener(OnGoodsItemClickListener listener) {
mOnGoodsItemClickListener = listener;
}
public String covertInt(int value) {
return value <= 9 ? ("0" + value) : value + "";
}

RecyclerView with different layouts

when i use this
#Override
public int getItemViewType(int position) {
return position;
}
my RecyclerView work without problem
now i want to use multi layout on it i use this
#Override
public int getItemViewType(int position) {
int type = -1;
if(mMessages.get(position).getDir().equals("left")) type = 1;
else if(mMessages.get(position).getDir().equals("right")) type = 0;
else if(mMessages.get(position).getDir().equals("typing")) type = 2;
return type;
//return position;
}
the problem when i used it's and scroll to top and back to bottom or just scrolling
its reorder item in RecyclerView like make the first item on it the last one or on middle or every where not i the correct position
just if i back to the first code of getItemViewType its work without
problem but i cant use multi layout
my full code
public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> {
private List<MessageList> mMessages;
private int[] mUsernameColors;
private Context context;
public MessageAdapter1(Context context, List<MessageList> messages) {
mMessages = messages;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = -1;
switch (viewType) {
case 0:
layout = R.layout.right_message;
break;
case 1:
layout = R.layout.left_message;
break;
case 2:
layout = R.layout.message_left;
break;
}
View v = LayoutInflater.from(parent.getContext()).inflate(layout , parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
MessageList message = mMessages.get(position);
//viewHolder.setMessage(message.getMessage());
//viewHolder.setUsername(message.getUsername());
viewHolder.setGroupMessage(message);
}
#Override
public int getItemCount() {
return mMessages.size();
}
#Override
public int getItemViewType(int position) {
int type = -1;
if(mMessages.get(position).getDir().equals("left")) type = 1;
else if(mMessages.get(position).getDir().equals("right")) type = 0;
else if(mMessages.get(position).getDir().equals("typing")) type = 2;
return type;
//return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout groupMessage;
//private ImageView typing;
public ViewHolder(View itemView) {
super(itemView);
groupMessage = (LinearLayout)itemView.findViewById(R.id.messages);
}
public void setGroupMessage(MessageList m) {
if (null == groupMessage) return;
int i = 0;
if(m.getMessageStatus() == false){
m.setMessageStatus(true);
for (String message : m.getMessageList()) {
//TextView text = new TextView(activity);
TextView text = new MyTextView(context);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0, 0, 0, 2);
if (m.getDir().equals("left")) {
text.setTextColor(Color.BLACK);
p.gravity = Gravity.LEFT;
if(m.getMessageList().size() == 1){
text.setBackgroundResource(R.drawable.message_left_default);
}
else if (i == 0) {
text.setBackgroundResource(R.drawable.message_left_first);
} else if (i + 1 == m.getMessageList().size()) {
text.setBackgroundResource(R.drawable.message_left_last);
} else {
text.setBackgroundResource(R.drawable.message_left);
}
} else{
p.gravity = Gravity.RIGHT;
text.setTextColor(Color.WHITE);
if(m.getMessageList().size() == 1){
text.setBackgroundResource(R.drawable.message_right_default);
}
else if (i == 0) {
text.setBackgroundResource(R.drawable.message_right_first);
} else if (i + 1 == m.getMessageList().size()) {
text.setBackgroundResource(R.drawable.message_right_last);
} else {
text.setBackgroundResource(R.drawable.message_right);
}
}
text.setLayoutParams(p);
text.setText(message);
text.setPadding(8, 8, 8, 8);
text.setTextSize(18f);
//text.setTextAppearance(context, android.R.style.TextAppearance_Large);
groupMessage.addView(text);
i++;
}
}
}
}
}
I solve it by remove
int type = -1;
if(mMessages.get(position).getDir().equals("left")) type = 1;
else if(mMessages.get(position).getDir().equals("right")) type = 0;
else if(mMessages.get(position).getDir().equals("typing")) type = 2;
return type;
from
getItemViewType
and return position
and put the first code
int type = -1;
if(mMessages.get(position).getDir().equals("left")) type = 1;
else if(mMessages.get(position).getDir().equals("right")) type = 0;
else if(mMessages.get(position).getDir().equals("typing")) type = 2;
return type;
in onCreateViewHolder and use viewType as the current position

Android RecyclerView layout bug

I have a recyclerview when it displayed for the first time the child's layout are not displayed properly:
but when i scroll to bottom and come back to the top it's good :
I have the same problem when i change the layout for a larger.
This is my Adapter class (don't take in consideration the diffents type,this is not the source of the bug because the bug exist too when i have one ViewHolder type)
private class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_ALERTS = 0 ;
private static final int TYPE_MOST_VIEWED = 1 ;
#Override
public RecyclerView.ViewHolder onCreateViewHolder (final ViewGroup parent, int viewType) {
if (viewType == TYPE_ALERTS){
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_item_alerts_home_home, null);
ViewHolderAlerts viewHolderAlerts = new ViewHolderAlerts(itemLayoutView);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(activity, Activity_UpdateAlert.class);
intent.putExtra("alert", (AlertObj)listAllProducts.get(recyclerView.getChildAdapterPosition(v)));
startActivity(intent);
}
});
return viewHolderAlerts;
}else {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_item_product_grid__list_products_by_category, null);
ViewHolder_Product_CatSearch viewHolder_product_catSearch = new ViewHolder_Product_CatSearch(itemLayoutView);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(activity, Activity_ProductDetails.class);
intent.putExtra("product", (Product)listAllProducts.get(recyclerView.getChildAdapterPosition(v)));
startActivity(intent);
}
});
return viewHolder_product_catSearch ;
}
}
#Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_ALERTS){
((ViewHolderAlerts)holder).textViewTitle.setText(((AlertObj)listAllProducts.get(position)).product.name);
((ViewHolderAlerts)holder).textViewPrice.setText(new DecimalFormat("###0.00")
.format(Double.valueOf(((AlertObj)listAllProducts.get(position)).product.price)) +
((AlertObj)listAllProducts.get(position)).product.currency);
Glide.with(activity.getApplicationContext())
.load(((AlertObj)listAllProducts.get(position)).product.imgUrl)
.error(R.drawable.picture_not_available)
.into(((ViewHolderAlerts) holder).imgView);
}else {
((ViewHolder_Product_CatSearch)holder).textViewName.setText(((ProductMostViews)listAllProducts.get(position)).name);
((ViewHolder_Product_CatSearch)holder).textViewPrice.setText(new DecimalFormat("###0.00")
.format(Double.valueOf(((ProductMostViews)listAllProducts.get(position)).price)) +
((ProductMostViews)listAllProducts.get(position)).currency);
Glide.with(activity.getApplicationContext())
.load(((ProductMostViews)listAllProducts.get(position)).imgUrl)
.error(R.drawable.picture_not_available)
.into(((ViewHolder_Product_CatSearch) holder).imageViewProduct);
}
}
#Override
public int getItemViewType (int position) {
return listAllProducts.get(position) instanceof AlertObj ? TYPE_ALERTS : TYPE_MOST_VIEWED ;
}
#Override
public long getItemId (int position) {
return listAllProducts.get(position) instanceof AlertObj ? TYPE_ALERTS : TYPE_MOST_VIEWED ;
}
#Override
public int getItemCount () {
return listAllProducts.size();
}
}
Thank's

RecyclerView items positions change or removed when scrolling

I have a problem that I have been trying to solve for too long but I can't know what causes the problem.
I have a marks list that contains Test objects and subject strings that I use as sections between marks, I use getClass() method to determine if an object is a string or Test, if it is a string, I hide the test's RelativeLayout, if it is a test then I hide the RelativeLayout of the subject. But when scrolling, items get removed and and re-added randomly.
my adapter's class:
public class MarksAdapter extends RecyclerView.Adapter<MarksAdapter.MyHolder>{
public class MyHolder extends RecyclerView.ViewHolder{
RelativeLayout card;
TextView title;
TextView total;
View divider;
int posito;
RelativeLayout group;
TextView groupTitle;
TextView groupMark;
public MyHolder(View itemView) {
super(itemView);
this.card = (RelativeLayout)itemView.findViewById(R.id.card);
this.title = (TextView)itemView.findViewById(R.id.title);
this.total = (TextView)itemView.findViewById(R.id.mark);
this.divider = itemView.findViewById(R.id.item_divider);
this.group = (RelativeLayout)itemView.findViewById(R.id.group);
this.groupTitle = (TextView)itemView.findViewById(R.id.group_title);
this.groupMark = (TextView)itemView.findViewById(R.id.group_mark);
}
}
final ArrayList marks;
Context ctx;
String selectedSem;
TestDatabase db;
public MarksAdapter(Context c, ArrayList marks,String sem,TestDatabase db,ArrayList<Integer> sections){
this.marks = marks;
ctx = c;
selectedSem = sem;
this.db = db;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyHolder(LayoutInflater.from(ctx).inflate(R.layout.mark_child, parent, false));
}
int lastPosition = -1;
int offset = 0;
#Override
public void onBindViewHolder(final MyHolder holder, final int positioner) {
holder.posito = positioner;
if(marks.get(holder.posito).getClass() == Test.class) {
holder.group.setVisibility(View.GONE);
//mark
final Test test = (Test)marks.get(positioner);
holder.title.setText(test.getName());
holder.total.setText(String.format(ctx.getString(R.string.new_m_sum), test.getMarkGot(), test.getMarkOver()));
//add here
holder.card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ctx,addTest.class);
Test mark = (Test)marks.get(holder.posito);
i.putExtra("t_name",mark.getName());
i.putExtra("subject",mark.getSubject());
i.putExtra("mark_got",mark.getMarkGot());
i.putExtra("mark_over",mark.getMarkOver());
i.putExtra("mode","edit");
i.putExtra("oldId", mark.getId());
ctx.startActivity(i);
}
});
holder.card.setLongClickable(true);
holder.card.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
final Dialog dialo = new Dialog(ctx);
dialo.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialo.setContentView(R.layout.material_dialog);
TextView title = (TextView) dialo.findViewById(R.id.title);
TextView body = (TextView) dialo.findViewById(R.id.body);
Button negative = (Button) dialo.findViewById(R.id.negative);
negative.setText(ctx.getString(R.string.cancel));
Button positive = (Button) dialo.findViewById(R.id.positive);
positive.setText(ctx.getString(R.string.Delete));
title.setText(ctx.getString(R.string.d));
body.setText(ctx.getString(R.string.q_delete) + " " + test.getName() + " " + ctx.getString(R.string.de_comp));
negative.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialo.dismiss();
}
});
positive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View vir) {
TestDatabase tb = new TestDatabase(ctx);
SemesterDatabase semesterDatabase = new SemesterDatabase(ctx);
tb.deleteTest(holder.posito, semesterDatabase.getSelected().getName());
MarksAdapter.this.notifyItemRemoved(holder.posito);
marks.remove(holder.posito);
for (int i = 0; i < marks.size(); i++) {
if(!(marks.get(i) instanceof Test)) {
String sub = String.valueOf(marks.get(i));
if(db.isNoValue(sub,selectedSem)){
marks.remove(i);
notifyItemRemoved(i);
}
}
}
dialo.dismiss();
}
});
dialo.show();
return true;
}
});
}else {
holder.group.setVisibility(View.VISIBLE);
holder.card.setVisibility(View.GONE);
//group
if(holder.posito == 0) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)holder.group.getLayoutParams();
params.setMargins(0,0,0,0);
holder.group.setLayoutParams(params);
}
String sub = String.valueOf(marks.get(holder.posito));
holder.groupTitle.setText(sub);
holder.groupMark.setText(db.getTotalSubjectMarks(sub,selectedSem));
}
if(lastPosition < holder.posito) {
if(holder.card.getVisibility() == View.VISIBLE) {
Animation slide = AnimationUtils.loadAnimation(ctx, R.anim.marks);
slide.setInterpolator(new AccelerateInterpolator());
slide.setDuration(700);
offset += 100;
slide.setStartOffset(offset);
holder.card.startAnimation(slide);
} else {
Animation slide = AnimationUtils.loadAnimation(ctx, R.anim.mark_groups);
slide.setInterpolator(new AccelerateInterpolator());
slide.setDuration(700);
offset += 50;
slide.setStartOffset(offset);
holder.group.startAnimation(slide);
}
lastPosition = holder.posito;
}
}
#Override
public int getItemCount() {
return marks.size();
}
}
Note: I already tried the itemViewType method but it didn't work for me
Thanks in advance!
I believe you missed to set holder.card back to visible, like this
if(marks.get(holder.posito).getClass() == Test.class) {
holder.card.setVisibility(View.Visible); //add back this
holder.group.setVisibility(View.GONE);

Highlight effect while pressing item in custom ListView adapter

There's a system visual effect everytime you click a view in Android. In Lollipop it's the ripple effect. When I created a ListView and associated it to an ordinary ArrayAdapter, this effect was present. Now that I've added a custom ListView, this effect is lost.
Now, I've tried to isolate what the problem is, and since using the same list item layout with a default adapter worked nicely, I would say that the problem is on my custom adapter.
I've seen many solutions related to this case that just implemented the ripple effect calling some drawables; this is not what I'm trying to do. The ripple effect shows only because I'm running the app on Android 5, now what I want to do is to have the default system highlight effect for my items when they're being clicked.
Here are the (hopefully) related pieces of my custom adapter:
public class CustomCardSetsAdapter extends BaseAdapter {
List<Card> totalList;
ArrayList<Boolean> hiddenItems;
ListView parentLV;
Integer curPosition = -1;
public static int selectedRowIndex;
public CustomCardSetsAdapter(CardSets cardList, ListView parentListView)
{
this.parentLV = parentListView;
assignSetValues(cardList);
totalList = cardList.getBlackrockMountain();
totalList.addAll(cardList.getClassic());
totalList.addAll(cardList.getCurseofNaxxramas());
totalList.addAll(cardList.getGoblinsvsGnomes());
Collections.sort(totalList,
new Comparator<Card>() {
public int compare(Card f1, Card f2) {
return f1.toString().compareTo(f2.toString());
}
});
hiddenItems = new ArrayList<>();
for (int i = 0; i < totalList.size(); i++) {
if(!totalList.get(i).getCollectible())
hiddenItems.add(true);
else
hiddenItems.add(false);
}
}
#Override
public int getCount() {
return (totalList.size() - getHiddenCount());
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final int index = getRealPosition(position);
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.card_list_item, parentLV, false);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer prevPosition = curPosition;
curPosition = position;
if(prevPosition >= parentLV.getFirstVisiblePosition() &&
prevPosition <= parentLV.getLastVisiblePosition())
{
View view = parentLV.getChildAt(prevPosition- parentLV.getFirstVisiblePosition());
parentLV.getAdapter().getView(prevPosition,view, parentLV);
}
v.setBackgroundColor(Color.WHITE);
}
});
Card curCard = totalList.get(index);
TextView cardName = (TextView) convertView.findViewById(R.id.cardName);
cardName.setText(curCard.getName());
setRarityColor(curCard,cardName);
TextView manaCost = (TextView) convertView.findViewById(R.id.manaCost);
manaCost.setText((curCard.getCost()).toString());
ImageView setIcon = (ImageView) convertView.findViewById(R.id.setIcon);
setSetIcon(curCard,setIcon);
if(position == curPosition)
convertView.setBackgroundColor(Color.WHITE);
else
convertView.setBackgroundColor(Color.TRANSPARENT);
return convertView;
}
#Override
public int getItemViewType(int position) {
return R.layout.card_list_item;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public boolean isEmpty() {
return false;
}
private int getHiddenCount()
{
int count = 0;
for(int i = 0;i <totalList.size();i++)
if(hiddenItems.get(i))
count++;
return count;
}
private int getRealPosition(int position) {
int hElements = getHiddenCountUpTo(position);
int diff = 0;
for(int i=0;i<hElements;i++) {
diff++;
if(hiddenItems.get(position+diff))
i--;
}
return (position + diff);
}
private int getHiddenCountUpTo(int location) {
int count = 0;
for(int i=0;i<=location;i++) {
if(hiddenItems.get(i))
count++;
}
return count;
}
}
Thanks in advance.
in your ListView XML, add:
android:drawSelectorOnTop="true"
I also think you are using your adapter wrong...
Use the ViewHolder Pattern on your Adapter:
public class CustomCardSetsAdapter extends BaseAdapter {
List<Card> totalList;
ArrayList<Boolean> hiddenItems;
ListView parentLV;
Integer curPosition = -1;
public static int selectedRowIndex;
private class ViewHolderRow{
TextView cardName;
TextView manaCost;
ImageView setIcon;
}
public CustomCardSetsAdapter(CardSets cardList, ListView parentListView)
{
this.parentLV = parentListView;
assignSetValues(cardList);
totalList = cardList.getBlackrockMountain();
totalList.addAll(cardList.getClassic());
totalList.addAll(cardList.getCurseofNaxxramas());
totalList.addAll(cardList.getGoblinsvsGnomes());
Collections.sort(totalList,
new Comparator<Card>() {
public int compare(Card f1, Card f2) {
return f1.toString().compareTo(f2.toString());
}
});
hiddenItems = new ArrayList<>();
for (int i = 0; i < totalList.size(); i++) {
if(!totalList.get(i).getCollectible())
hiddenItems.add(true);
else
hiddenItems.add(false);
}
}
#Override
public int getCount() {
return (totalList.size() - getHiddenCount());
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final int index = getRealPosition(position);
ViewHolderRow theRow;
if(convertView == null) {
theRow = new ViewHolderRow();
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.card_list_item, parentLV, false);
// Cache your views
theRow.cardName = (TextView) convertView.findViewById(R.id.cardName);
theRow.manaCost = (TextView) convertView.findViewById(R.id.manaCost);
theRow.setIcon = (ImageView) convertView.findViewById(R.id.setIcon);
// Set the Tag to the ViewHolderRow
convertView.setTag(theRow);
}else{
// get the Row to re-use
theRow = (ViewHolderRow) convertView.getTag();
}
//... Removed convertView.setOnClickListener
Card curCard = totalList.get(index);
// Set Items
theRow.cardName.setText(curCard.getName());
setRarityColor(curCard,theRow.cardName);
theRow.manaCost.setText((curCard.getCost()).toString());
setSetIcon(curCard,theRow.setIcon);
if(position == curPosition){
convertView.setBackgroundColor(Color.WHITE);
}else{
convertView.setBackgroundColor(Color.TRANSPARENT);
}
return convertView;
}
#Override
public int getItemViewType(int position) {
return R.layout.card_list_item;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public boolean isEmpty() {
return false;
}
private int getHiddenCount()
{
int count = 0;
for(int i = 0;i <totalList.size();i++)
if(hiddenItems.get(i))
count++;
return count;
}
private int getRealPosition(int position) {
int hElements = getHiddenCountUpTo(position);
int diff = 0;
for(int i=0;i<hElements;i++) {
diff++;
if(hiddenItems.get(position+diff))
i--;
}
return (position + diff);
}
private int getHiddenCountUpTo(int location) {
int count = 0;
for(int i=0;i<=location;i++) {
if(hiddenItems.get(i))
count++;
}
return count;
}
}
Set an onListItemClickListener instead of using this on the entire convertView...
yourListView.setOnItemClickListener(ListListener);
private final OnItemClickListener ListListener = new OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// ... Do something on click
}
}

Categories

Resources