Long app loading on "cold start" with 2 recycler views - java

I've started building a new app from scratch, so currently it contains only one activity. This activity contains 2 vertical RecyclerViews, first contains 30 Buttons, then space separating RecyclerViews and then second RecyclerView, containing 50 Buttons. Each RecyclerView has set GridLayoutManager. Any of RecyclerView doesn't fetch data from database/network, they have just fixed size and count. When I run this code, it launches in ~ 2 secs and logcat shows skipped frames at rate ~70-80.
When I skip setting layout it launches just fine.
This is my activity:
private GridLayoutManager mLayoutManager, mSecondManager;
private RecyclerView mRecyclerViewFirst, mRecyclerViewSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvAdapter firstAdapter = new rvAdapter();
rvAdapter2 secondAdapter = new rvAdapter2();
mRecyclerViewFirst = findViewById(R.id.recycler_view1);
mRecyclerViewSecond = findViewById(R.id.recycler_view2);
mRecyclerViewFirst.setNestedScrollingEnabled(false);
mRecyclerViewSecond.setNestedScrollingEnabled(false);
mRecyclerViewFirst.setAdapter(firstAdapter);
mRecyclerViewSecond.setAdapter(secondAdapter);
}
#Override
protected void onResume() {
super.onResume();
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mLayoutManager = new GridLayoutManager(getApplicationContext(), 8);
mSecondManager = new GridLayoutManager(getApplicationContext(), 8);
} else {
mLayoutManager = new GridLayoutManager(getApplicationContext(), 5);
mSecondManager = new GridLayoutManager(getApplicationContext(), 5);
}
mRecyclerViewFirst.setLayoutManager(mSecondManager);
mRecyclerViewSecond.setLayoutManager(mLayoutManager);
}
one of adapters (second is the same, just count different):
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext().getApplicationContext())
.inflate(R.layout.items_recyclerview, viewGroup, false));
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.mButton.setText(String.valueOf(position + 1));
}
#Override
public int getItemCount() {
return 50;
}
class ViewHolder extends RecyclerView.ViewHolder {
private Button mButton;
private ViewHolder(#NonNull View view) {
super(view);
mButton = view.findViewById(R.id.button);
}
}
}
Layouts: https://gitlab.com/Domin_PL/sample/blob/master/activity_main
I had troubles in adding XML code to stack
This is the whole application for now, so the problem is with LayoutManager. Btw, detecting device's orientation in onResume doesn't cause this lag.
Any ideas how can I fix this lag issue?
Thanks in advance

May be your problem with your xml file . If you use constraintlayout for recyclerview cell then lagging too much. Because too much load on main ui thread. Avoid constraintlayout in Recyclerview item. Please share your xml file of items of recyclerview.

I managed to get it down to an average 30~35 skipped frames upon cold launch of the app. I used a single RecyclerView with different view types and ViewHolders for the titles and the buttons. Here is the code with which I obtained the that result:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setAdapter(new RvAdapter(
ContextCompat.getColor(this, R.color.colorPrimaryDark),
ContextCompat.getColor(this, R.color.white)
));
}
#Override
protected void onResume() {
super.onResume();
final int orientation = getResources().getConfiguration().orientation;
final int columnCount = orientation == Configuration.ORIENTATION_LANDSCAPE ? 8 : 5;
final GridLayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), columnCount);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int i) {
return (i == 0 || i == 31) ? columnCount : 1;
}
});
mRecyclerView.setLayoutManager(layoutManager);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
RvAdapter.java
public class RvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int BUTTON_FIRST_GROUP_COUNT = 30;
private static final int BUTTON_SECOND_GROUP_COUNT = 50;
private static final int TYPE_TITLE = 0;
private static final int TYPE_BUTTON = 1;
#ColorInt
private final int colorPrimary;
#ColorInt
private final int colorWhite;
private String[] content = new String[BUTTON_FIRST_GROUP_COUNT + BUTTON_SECOND_GROUP_COUNT + 2 /* titles */];
RvAdapter(#ColorInt final int colorPrimary, #ColorInt final int colorWhite) {
this.colorPrimary = colorPrimary;
this.colorWhite = colorWhite;
initContent();
}
private void initContent() {
for (int i = 0; i < content.length; i++) {
final String prefix = isTitle(i) ? "Title " : "";
content[i] = prefix + (i + 1);
}
}
private boolean isTitle(final int position) {
return (position == 0 || position == BUTTON_FIRST_GROUP_COUNT + 1);
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
final LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
if (viewType == TYPE_TITLE) {
return new TitleViewHolder(layoutInflater.inflate(R.layout.item_title, viewGroup, false));
}
return new ButtonViewHolder(layoutInflater.inflate(R.layout.item_button, viewGroup, false));
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
final String currentContent = content[position];
if (holder instanceof TitleViewHolder) {
final TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
titleViewHolder.mTextView.setText(currentContent);
} else {
final ButtonViewHolder buttonViewHolder = (ButtonViewHolder) holder;
buttonViewHolder.setBackgroundColor(position <= BUTTON_FIRST_GROUP_COUNT ? colorPrimary : colorWhite);
buttonViewHolder.mButton.setText(currentContent);
}
}
#Override
public int getItemViewType(int position) {
return isTitle(position) ? TYPE_TITLE : TYPE_BUTTON;
}
#Override
public int getItemCount() {
return content.length;
}
}
Hope that it is going to help you.

Related

Using ViewTreeObserver i am adding setMaxLines and setEllipsize in MaterialTextView inside RecyclerView.Adapter

I am set MaterialTextView inside RelativeLayout and set RelativeLayout size programmatically different size for every device.
Then i have using ViewTreeObserver to set setMaxLines and setEllipsize in MaterialTextView but i am facing some problem show the text in MaterialTextView using RecyclerView.Adapter.
I am using load more RecyclerView i am getting all data then after show text automatically in list and also notify data adapter then show text.
not showing text inside MaterialTextView in list
phone lock on/off then showing data in MaterialTextView
set recyclerview in fragment
recyclerView = view.findViewById(R.id.recyclerView_sub_category);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter code
public class SubCategoryAdapter extends RecyclerView.Adapter {
private final int VIEW_TYPE_LOADING = 0;
private final int VIEW_TYPE_ITEM = 1;
private final int VIEW_TYPE_QUOTES = 2;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
other data load
} else if (viewType == VIEW_TYPE_QUOTES) {
View v = LayoutInflater.from(activity).inflate(R.layout.quotes_adapter, parent, false);
return new Quotes(v);
} else if (viewType == VIEW_TYPE_LOADING) {
progressbar load
}
return null;
}
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, #SuppressLint("RecyclerView") final int position) {
if (holder.getItemViewType() == VIEW_TYPE_ITEM) {
final ViewHolder viewHolder = (ViewHolder) holder;
// ------------- code -----
} else if (holder.getItemViewType() == VIEW_TYPE_QUOTES) {
final Quotes quotes = (Quotes) holder;
quotes.relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, columnWidth / 2));
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "text_font/" + subCategoryLists.get(position).getQuote_font());
quotes.textView.setTypeface(typeface);
quotes.textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
quotes.textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int noOfLinesVisible = quotes.textView.getHeight() / quotes.textView.getLineHeight();
quotes.textView.setMaxLines(noOfLinesVisible);
quotes.textView.setEllipsize(TextUtils.TruncateAt.END);
quotes.textView.setText(subCategoryLists.get(position).getStatus_title());
}
});
}
}
#Override
public int getItemCount() {
return subCategoryLists.size() + 1;
}
#Override
public int getItemViewType(int position) {
if (position != subCategoryLists.size()) {
if (subCategoryLists.get(position).getStatus_type().equals("quote")) {
return VIEW_TYPE_QUOTES;
} else {
return VIEW_TYPE_ITEM;
}
} else {
return VIEW_TYPE_LOADING;
}
}
public class Quotes extends RecyclerView.ViewHolder {
private RelativeLayout relativeLayout;
private MaterialTextView textView;
public Quotes(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView_quotes_adapter);
relativeLayout = itemView.findViewById(R.id.rel_quotes_adapter);
}
}
}
I am not completely sure about the problem that you are having there. However, I think I found some problems in your code.
The first problem that I see in setting up your RecyclerView is setting the layout size fixed by the following.
recyclerView.setHasFixedSize(true);
Looks like you are changing the item layout size dynamically. Hence you need to remove the line above while setting up your RecyclerView.
The second problem that I see is, there is no textView_category in the ViewHolder for Quote. Hence the following should throw an error.
quotes.textView_category.setText(subCategoryLists.get(position).getCategory_name());
One problem I can see is that you are calling
quotes.textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
in the first line of your listener callback no matter what. In a RecyclerView the first few times this is called the layout might still be unmeasured but you're cancelling the listener callback after the first callback. So this line fires, and if the view is still unmeasured then the next few lines are going to fail and your code will get no more opportunities to fill the view. Try checking the measuredWidth or measuredHeight of your view for something greater than 0 before cancelling future listener callbacks.
I have using this. works perfectly
textView.setText(subCategoryLists.get(position).getStatus_title());
textView.post(new Runnable() {
#Override
public void run() {
ViewGroup.LayoutParams params = textView.getLayoutParams();
if (params == null) {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
final int widthSpec = View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(textView.getHeight(), View.MeasureSpec.UNSPECIFIED);
textView.measure(widthSpec, heightSpec);
textView.setMaxLines(heightSpec / textView.getLineHeight());
textView.setEllipsize(TextUtils.TruncateAt.END);
}
});

How to set a progress for many progress-bars in RecyclerView

I am using a RecyclerView to view many Progress Bars as a slide show and an adapter.
This my adapter class
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder> {
private static final String TAG = "RecAdapter";
private ArrayList progressbars = new ArrayList<>();
private Context pcontex;
public RecAdapter(Context pcontex ,ArrayList progressbars) {
this.progressbars = progressbars;
this.pcontex = pcontex;
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.progress,viewGroup,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onBindViewHolder: called");
}
#Override
public int getItemCount() {
return progressbars.size() ;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ProgressBar b ;
ConstraintLayout parent;
public ViewHolder(#NonNull View itemView) {
super(itemView);
b = itemView.findViewById(R.id.progressBar);
parent= itemView.findViewById(R.id.parent);
}
}
}
My MainActivity code-
Prog = new ArrayList<>(); b2 = findViewById(R.id.progressBar) ;
b = findViewById(R.id.progressBar);
probitmap();
private void probitmap(){
Log.d(TAG, "probitmap: preparing bit maps");
Prog.add(b);
Prog.add(b2);
Recycle();// this method is out of on create
}
private void Recycle(){
Log.d(TAG, "Recycle: init recycleview");
recyclerView =findViewById(R.id.Rec);
RecAdapter Adapter = new RecAdapter(this,Prog);
recyclerView.setAdapter(Adapter);
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
}
The code is working perfectly but I need to retrieve every progress bar that is in the recycle view and set progress for each one.
I tried multiple ways but it doesn't work. Here is what I tried-
for (int i = 0; i < recyclerView.getChildCount(); i++) {
RecAdapter.ViewHolder holder = (RecAdapter.ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
holder.b.setProgress(50);
}
Use OnBindViewHolder to manage each "row" generated by the recycler.
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.b.setProgress(progressbar.get(i)); //i is the position of the recycler
viewHolder.b.setListener....
}

How to put main cell on the top of StaggeredGridLayout(recycler view)?

I use StaggeredGridLayout in my Recycler View
What I need :
I can divide recycler view to 2 columns, but I don't know how to put main cell on top of the recycler view
I am not sure, but maybe I can put it in container and try to hide it during scrolling? Like we do with toolbar when user start to scroll toolbar gone up.
But I am not sure about it
How to put main cell on the top of StaggeredGridLayout(recycler view) ?
Thanks in advance!
Add these lines in onBindViewHolder method
if(position == 0){
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
layoutParams.setFullSpan(true);
}
Eventually there is my adapter maybe will help someone to understand how does it work
public class AdapterRecViewMain
extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<BaseMarkerElement> mainCardList;
private final int HEADER_VIEW = 0;
private final int FOOTER_VIEW = 1;
public AdapterRecViewMain() {
}
public void setData(List<BaseMarkerElement> mainCardList) {
this.mainCardList = mainCardList;
}
#Override public int getItemViewType(int position) {
if (position == 0) {
return HEADER_VIEW;
}
return FOOTER_VIEW;
}
#Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
if (type == FOOTER_VIEW) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_main_activity, viewGroup, false);
return new MainCardViewHolder(v);
} else {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.header_view_main_activity, viewGroup, false);
return new HeaderViewHolder(v);
}
}
#Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int positionItem) {
final int position = viewHolder.getAdapterPosition();
if (viewHolder instanceof HeaderViewHolder) {
StaggeredGridLayoutManager.LayoutParams layoutParams =
(StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
layoutParams.setFullSpan(true);
BaseMarkerElement item = mainCardList.get(position);
if (item instanceof HeaderView) {
HeaderView header = (HeaderView) mainCardList.get(position);
// need to add implementation
}
} else if (viewHolder instanceof MainCardViewHolder) {
MainCardViewHolder currentView = (MainCardViewHolder) viewHolder;
CardMainActivity currentCard = (CardMainActivity) mainCardList.get(position);
currentView.ivMainCard.setImageResource(currentCard.getIvMainCard());
currentView.tvBrandName.setText(currentCard.getTvBrandName());
currentView.tvPrice.setText(currentCard.getTvPrice());
currentView.tvType.setText(currentCard.getTvType());
}
}
#Override public int getItemCount() {
return mainCardList.size();
}
private class MainCardViewHolder extends RecyclerView.ViewHolder {
ImageView ivMainCard;
TextView tvBrandName;
TextView tvType;
TextView tvPrice;
MainCardViewHolder(View view) {
super(view);
ivMainCard = (ImageView) view.findViewById(R.id.imageViewMainCard);
tvBrandName = (TextView) view.findViewById(R.id.tvBrandName);
tvType = (TextView) view.findViewById(R.id.tvType);
tvPrice = (TextView) view.findViewById(R.id.tvPrice);
}
}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
}

SectionedRecyclerView over Write first item

I'm Trying to use : SimpleSectionedRecyclerViewAdapter , i have arrayList that have 30 elements with strings , i'm going to group every 10 elements with header Title ,
first item should be "Item0" with header name "First 10 elements start"
but when doing that i get :
"First 10 elements start" // header titile
"Item1" <---! note : it should "Item0"
so where is index number 0 gone?
//adapter.addItem3(CategoriesList.get(0));
List<SimpleSectionedRecyclerViewAdapter.Section> sections =
new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>();
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "First 10 elements start"));
//Add your adapter to the sectionAdapter
SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()];
SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new
SimpleSectionedRecyclerViewAdapter(activity, R.layout.drawer_header_book, R.id.headerName, adapter);
mSectionedAdapter.setSections(sections.toArray(dummy));
mDrawerList.setLayoutManager(new LinearLayoutManager(activity));
mDrawerList.setAdapter(mSectionedAdapter);
I'm using exactly as adapter :
https://gist.github.com/gabrielemariotti/4c189fb1124df4556058
public class DrawerItemCustomAdapterForAllBooks extends RecyclerView.Adapter<DrawerItemCustomAdapterForAllBooks.SimpleViewHolder> {
private final Context context;
Typeface custom_font;
ArrayList<Categories> mData;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
int BookID;
public void add(Categories s,int position) {
position = position == -1 ? getItemCount() : position;
mData.add(position,s);
notifyItemInserted(position);
}
public void remove(int position){
if (position < getItemCount() ) {
mData.remove(position);
notifyItemRemoved(position);
}
}
public DrawerItemCustomAdapterForAllBooks(Context context, ArrayList<Categories> Categories2, int BookID) {
this.mData = Categories2;
this.context = context;
this.BookID = BookID;
custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/HelveticaNeueLTArabic-Light.ttf");
}
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(context).inflate(R.layout.listview_item_row, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
if (holder != null) {
final Categories currentItem = getItem(holder.getAdapterPosition());
SimpleViewHolder genericViewHolder = (SimpleViewHolder) holder;
genericViewHolder.position = holder.getAdapterPosition();
genericViewHolder.CategoryName.setText(currentItem.getName());
genericViewHolder.CategoryName.setTypeface(custom_font);
genericViewHolder.CategoryName.setTag(holder.getAdapterPosition());
genericViewHolder.itemView.setTag(holder.getAdapterPosition());
if (currentItem.getBookID() == 1) {
genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_white));
genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_darkrose));
Picasso.with(context).load(R.drawable.list_icon_e02)
.error(R.drawable.no_internet)
.tag(context)
.placeholder(R.drawable.no_spic)
.into(genericViewHolder.CategoryImage);
} else if (currentItem.getBookID() == 2) {
genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_openrose));
genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_darkrose));
Picasso.with(context).load(R.drawable.list_icon_08)
.error(R.drawable.no_internet)
.tag(context)
.placeholder(R.drawable.no_spic)
.into(genericViewHolder.CategoryImage);
} else if (currentItem.getBookID() == 3) {
genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_darkrose));
genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_white));
Picasso.with(context).load(R.drawable.list_icon_08)
.error(R.drawable.no_internet)
.tag(context)
.placeholder(R.drawable.no_spic)
.into(genericViewHolder.CategoryImage);
}
}
}
#Override
public int getItemCount() {
return mData.size()-2;
}
public Categories getItem(int position) {
return mData.get(position-1);
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
protected TextView CategoryName;
protected ImageView CategoryImage;
protected View itemView;
int position = -1;
public SimpleViewHolder(View itemView) {
super(itemView);
this.CategoryName = (TextView) itemView.findViewById(R.id.CategoryName);
this.CategoryImage = (ImageView) itemView.findViewById(R.id.CategoryImage);
// CategoryName.setTypeface(custom_font);
this.itemView = itemView;
getAdapterPosition();
this.CategoryName = (TextView) itemView.findViewById(R.id.CategoryName);
this.CategoryImage = (ImageView) itemView.findViewById(R.id.CategoryImage); }
}
}
With the library SectionedRecyclerViewAdapter you can group your items in sections and add a header to each section without worrying about the "position" of the headers:
class MySection extends StatelessSection {
String title;
List<Categories> list;
public MySection(String title, List<Categories> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.list = list;
}
#Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
#Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
#Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position).getName());
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
}
}
Then you set up the RecyclerView with your Sections:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with a sub list of data from mData
MySection data1Section = new MySection("First 10 elements start", categories1List);
MySection data2Section = new MySection("Elements from 10 to 20 start", categories2List);
MySection data3Section = new MySection("Elements from 20 to 30 start", categories3List);
// Add your Sections to the adapter
sectionAdapter.addSection(data1Section);
sectionAdapter.addSection(data2Section);
sectionAdapter.addSection(data3Section);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

Background color or Images shuffling on scrolling in recyclerview?

I am wondering that my images and color of layouts shuffling when i scrolls downwards or upwards, I created cardview using recyclerview. and set an image(changes color on click like to know if its user's favourite item) and setbackgroundcolor(randomly chossen) to the parent layout to make cardview attractive. but when i scrolls
1. the image that image changes position,
2. the layout background changes color automatically.
I am posting my adapter's code here.
public class TOAdapter extends RecyclerView.Adapter<TOAdapter.ViewHolder> {
JSONArray jsonArray;
private String title;
private String image;
private ImageLoader imageLoader;
private String subtitle;
private String subti;
private Context context;
private ImageView clip;
public TOAdapter(JSONArray jsonArray) {
this.jsonArray = jsonArray;
}
// Create new views (invoked by the layout manager)
#Override
public TOAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.top_twenty_list, parent, false);
final ViewHolder viewHolder = new ViewHolder(itemLayoutView);
final Random random = new Random(System.currentTimeMillis());// We add 155 since we want at least 155 in each channel.// Then we add to it a random number between 0 and 100.
int r = 155 + random.nextInt(101);
int g = 155 + random.nextInt(101);
int b = 155 + random.nextInt(101);
int color = Color.rgb(r, g, b);
viewHolder.frame.setBackgroundColor(color);
viewHolder.layer.setBackgroundColor(color);
clip = (ImageView) itemLayoutView.findViewById(R.id.ic_clip);
clip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int iColor = Color.parseColor("#0000FF");
int red = (iColor & 0xFF0000) / 0xFFFF;
int green = (iColor & 0xFF00) / 0xFF;
int blue = iColor & 0xFF;
float[] matrix = {0, 0, 0, 0, red
, 0, 0, 0, 0, green
, 0, 0, 0, 0, blue
, 0, 0, 0, 1, 0};
ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
clip.setColorFilter(colorFilter);
}
});
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
// myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/RobotoCondensedBoldItalic.ttf");
try {
JSONObject obj = jsonArray.getJSONObject(position);
title = obj.getString("title");
image = obj.getString("brand_logo");
subtitle = obj.getString("sub_title");
} catch (JSONException e) {
e.printStackTrace();
}
viewHolder.txtViewTitle.setText(subtitle);
viewHolder.subtitle.setText(title);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String full_Url = "http://mycompany/assets/new" + image;
viewHolder.thumbnail.setImageUrl(full_Url, imageLoader);
viewHolder.btn_get_deal.setTag(position);
viewHolder.btn_get_deal.setOnClickListener(new View.OnClickListener() {
public JSONObject obj;
public ArrayList<String> offerlist = new ArrayList();
#Override
public void onClick(View view) {
Intent offerpage = new Intent(AppController.getInstance().getApplicationContext(), OfferDetails.class);
Integer pos = (Integer) view.getTag();
try {
obj = jsonArray.getJSONObject(pos);
offerpage.putExtra("jsonObj", obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
//offerpage.getParcelableArrayListExtra(offerlist);
offerpage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AppController.getInstance().getApplicationContext().startActivity(offerpage);
}
});
//viewHolder.txtViewTitle.setTypeface(myTypeface);
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
private final NetworkImageView thumbnail;
private final RelativeLayout frame;
private final RelativeLayout layer;
public TextView txtViewTitle;
public TextView subtitle;
public ImageView clip;
public CardView btn_get_deal;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.txttitle_toptwenty);
subtitle = (TextView) itemLayoutView.findViewById(R.id.sub_title_toptwenty);
thumbnail = (NetworkImageView) itemLayoutView.findViewById(R.id.thumbnail_topwenty);
frame = (RelativeLayout) itemLayoutView.findViewById(R.id.frame);
layer = (RelativeLayout) itemLayoutView.findViewById(R.id.layer);
btn_get_deal = (CardView) itemLayoutView.findViewById(R.id.card_view);
}
}
// Return the size of your itemsData (invoked by the layout manager)
#Override
public int getItemCount() {
return jsonArray.length();
}
}
I have created a working example of what you are trying to accomplish. The source of the errors you experience is mostly that you don't understand view recycling. I am not going to explain the whole thing to you now, but anyway here is the example:
For the example I used this layout for each row:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/background"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
I used this model:
public class ExampleModel {
private final int mColor;
private final String mText;
public ExampleModel(int color, String text) {
mColor = color;
mText = text;
}
public int getColor() {
return mColor;
}
public String getText() {
return mText;
}
}
And this view holder:
public class ExampleViewHolder extends RecyclerView.ViewHolder {
private final FrameLayout mBackground;
private final TextView mTextView;
public ExampleViewHolder(View itemView) {
super(itemView);
mBackground = (FrameLayout) itemView.findViewById(R.id.background);
mTextView = (TextView) itemView.findViewById(R.id.textView);
}
public void bind(ExampleModel model) {
mBackground.setBackgroundColor(model.getColor());
mTextView.setText(model.getText());
}
}
As you can see nothing special, the Adapter implementation is equally simple:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleViewHolder> {
private final LayoutInflater mInflater;
private final List<ExampleModel> mModels;
public ExampleAdapter(Context context, List<ExampleModel> models) {
mInflater = LayoutInflater.from(context);
mModels = models;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = mInflater.inflate(R.layout.item_example, parent, false);
return new ExampleViewHolder(itemView);
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
final ExampleModel model = mModels.get(position);
holder.bind(model);
}
#Override
public int getItemCount() {
return mModels.size();
}
}
And you use the whole thing like this:
final Random mRandom = new Random(System.currentTimeMillis());
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
final List<ExampleModel> models = new ArrayList<>();
for (int i = 0; i < 100; i++) {
final int randomColor = generateRandomPastelColor();
models.add(new ExampleModel(randomColor, String.valueOf(i)));
}
final ExampleAdapter adapter = new ExampleAdapter(getActivity(), models);
recyclerView.setAdapter(adapter);
}
public int generateRandomPastelColor() {
final int baseColor = Color.WHITE;
final int red = (Color.red(baseColor) + mRandom.nextInt(256)) / 2;
final int green = (Color.green(baseColor) + mRandom.nextInt(256)) / 2;
final int blue = (Color.blue(baseColor) + mRandom.nextInt(256)) / 2;
return Color.rgb(red, green, blue);
}
This should do what you are looking for and you can use it as an example of how to implement your Adapter.
I got the same problem ,when i used the radioButton in my listView row, I solved it. As in every scrolling the Cache is cleared, you have to store the changed data in every row. just check my code
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// if(position==0)
// Toast.makeText(context, ""+QuestionViewPageAdapter.mRadioGroupData, Toast.LENGTH_SHORT).show();
final ViewHolder viewHolder;
View row=convertView;
radioButton=new RadioButton[Integer.parseInt(mNoOfCategoryGreading)];
LayoutInflater inflater =LayoutInflater.from(context);//(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.question_row, parent, false);
viewHolder=new ViewHolder();
viewHolder.tv_qContent=(TextView)row.findViewById(R.id.tv_question);
viewHolder.rg=(RadioGroup)row.findViewById(R.id.rgrp);
for(int i=0;i<Integer.parseInt(mNoOfCategoryGreading);i++)
{
radioButton[i]=new RadioButton(context);
radioButton[i].setLayoutParams(new RadioGroup.LayoutParams(0, LayoutParams.WRAP_CONTENT,1f));
//1f for layout_weight="1"
radioButton[i].setId(i);
radioButton[i].setText(""+(i+1));
viewHolder.rg.addView(radioButton[i]);
}
row.setTag(viewHolder);
for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
int p=Integer.parseInt(""+QuestionViewPageAdapter.mRadioGroupData.get(i).get("position"));
String id=QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id");
if(p!=-1 && id.equals(""+data.get(position).get(AppData.question_Set_category_QuestionID)))
{//radioButton[p].setSelected(true);
viewHolder.rg.check(p-1);}
//Toast.makeText(context, "Yes "+p, Toast.LENGTH_LONG).show();}
}
viewHolder.tv_qContent.setText((position+1)+". "+data.get(position).get(AppData.question_Set_category_QuestionContent));
viewHolder.tv_qContent.setTypeface(typeface_muso_regular300);
viewHolder.rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
String s=data.get(position).get(AppData.question_Set_category_QuestionID);
isAvailabel(s);
int radioButtonID = viewHolder.rg.getCheckedRadioButtonId();
View radioButton = viewHolder.rg.findViewById(radioButtonID);
int idx = viewHolder.rg.indexOfChild(radioButton);
HashMap<String, String> map=new HashMap<String, String>();
map.put("position", ""+(idx+1));
map.put("Id", ""+data.get(position).get(AppData.question_Set_category_QuestionID));
map.put("CategoryId", ""+mCategoryId);
QuestionViewPageAdapter.mRadioGroupData.add(map);
}
});
return row;
}
private void isAvailabel(String qId){
for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
if(qId.equals(QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id"))){
position=i;
QuestionViewPageAdapter.mRadioGroupData.remove(i);
}
}
}
class ViewHolder {
TextView tv_qContent ,tv_1,tv_2,tv_3;
RadioGroup rg;
RadioButton rBtn1,rBtn2,rBtn3,rBtn4,rBtn5;
}
After days of struggle and headache, i have found a very simple solution.
In your adapter you just need to override 2 methods.
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
Helped me resolve the shuffling issue of recycler view.
In my case overriding getItemId and setting sethasStableIds to true solved the shuffling issue.
in adapter:
#Override
public long getItemId(int position) {
Object listItem = feeds.get(position);
return listItem.hashCode();
}
in main activity/fragment :
adapter = new FeedAdapter(feeds, getContext());
adapter.setHasStableIds(true);

Categories