Adding views to PagerAdapter too slow - java

My method add views to PagerAdapter:
public void initdata() {
for(int i = 0;i<=1189;i++){
view = inflater.inflate(R.layout.testss,null);
tx = (TextView)view.findViewById(R.id.txts);
tx.setText(textseries[i]));
pageList.add(tx);}
}
My pagerAdapter:
#Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(pageList.get(position));
return pageList.get(position);}
it takes 5~6 sec add views to PagerAdapter , but the TextView is just change it's content,how to avoid call view method so much times ?

how to avoid call view method so much times ?
The best solution would be to replace ViewPager with RecyclerView, so that pages can get recycled.
The next-best solution is to work out some PagerAdapter that can manage recycling of your pages.
The solution closest to what you have is:
Step #1: Get rid of initdata()
Step #2: Get rid of pageList()
Step #3: Inflate the layout and set the text on the TextView in instantiateItem(), so you only need to do it for pages that the user visits, and then only on demand

Related

Recyclerview onViewScrolled called multiple times

Currently working on recyclerview that refreshes every time I choose something.
Everytime I would set again the adapter after I selected a choice.
For example, if I choose Fruit, the recyclerview will change to fruits.
I would attach a new adapter to the recyclerview everytime.
The problem with this is whenever I check on onViewScrolled, it is getting called the same amount as I changed the recyclerview data.
My question is, why does this occurs ? Is it because the old adapter didn't detach ?
If yes, how do I detach it properly ?
I think you have a problem with the recycler adapter class. For example, you change some data and scroll down. When your scroll is up and that shows your old data.
Add this code in the adapter class.
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}

Refreshing a RecyclerView when all items are removed

I have a RecyclerView that shows a list of items.
If there is no item to show, The recyclerview shows one item with a specific view (to tell the user there is no item instead of a white screen).
Within HistoryFragment:
private void initRecyclerView(Boolean isNoResult){
HistoryRecyclerViewAdapter adapter = new HistoryRecyclerViewAdapter(mContext, mRecords, **isNoResult**);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mRecyclerView.setAdapter(adapter);
}
Within HistoryRecyclerViewAdapter:
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if(**isEmpty**) {
**view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory_empty, parent, false);**
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory, parent, false);
}
ViewHolder holder = new ViewHolder(view);
return holder;
}
So, it's possible to remove items one by one, if we click on them.
I would like to set isEmpty to true and refresh the RecyclerView when the dataSet is null.
I already know where to call that method but I really don't know how I can do that? (i.e. refresh the RecyclerView with isEmpty = true so I can display the cell that explain to the user that there is no record anymore).
Don't inflate different view-holders, because when the adapter has no items, not a single one of them will ever be inflated. Instead one can wrap the RecyclerView together with a "no data" Fragment into a ViewFlipper, which then can be switched to the Fragment, when the RecyclerView adapter has no items.
Best practice is to use an empty view outside of RecyclerView but in case you like to do what you want:
1.in onCreateViewHolder only inflate one layout which has empty and item views
on item delete check if your array is empty and then add a null item
then in onBindViewHolder check the model if model is Null visible empty view otherwise show item view
summary:
onBind:
model is null : empty View visible
model is not null: item View visible
use interface to refresh the RecyclerView after remove something like this
public interface RefreshRecyclerView {
public void Refresh();
}
then in activity or fragment implement the interface
Fragment implements RefreshRecyclerView
you will have override method like this
#Override
public void Refresh() {
// set adapter again here
}
then pass the interface to adapter like this
RefreshRecyclerView refresh = (RefreshRecyclerView) this;
yourRecycler.setadapter(refresh);
fially when user clicked on adapter use this
refresh. Refresh();

Nested ViewPager returns null after rotation

I need a reference to VerticalViewPager in PageFragSanningellerKonsekvens.
I got two ViewPagers: one for horizontal swiping that goes on for ever, and one for vertical swiping that loads three fragments (mid, swipe up, swipe down) that loads content. The horizontal ViewPager loads a new instance of VerticalViewPager on every swipe. I need a reference in the actual fragment that is loaded by the VerticalViewPager, so that I can swipe vertically by tapping a button. I also need a reference to the horizontal ViewPager to allow the first page to have a button that triggers a horizontal swipe.
I've solved it by sending the reference as an argument like this:
FragSanningEllerKonsekvens - The class that initializes the VerticalViewPager.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_sanning_eller_konsekvens, container, false);
initComponents();
setComponents();
return view;
}
private void initComponents() {
verticalViewPager = (VerticalViewPager) view.findViewById(R.id.verticalviewpager);
mStatementPagerAdapter = new TruthDarePagerAdapter(getChildFragmentManager()).newInstance(getChildFragmentManager(),mViewPager, verticalViewPager);
}
Its transfers in TruthOrDarePagerAdapter:
private ViewPager mViewPager;
private VerticalViewPager mVerticalViewPager;
public static TruthDarePagerAdapter newInstance(FragmentManager fm, ViewPager mViewPager, VerticalViewPager mVerticalViewPager){
TruthDarePagerAdapter horizontalPagerAdapter = new TruthDarePagerAdapter(fm);
horizontalPagerAdapter.setViewPager(mViewPager);
horizontalPagerAdapter.setVerticalViewPager(mVerticalViewPager);
return horizontalPagerAdapter;
}
public VerticalViewPager getVerticalViewPager() {
return mVerticalViewPager;
}
public void setVerticalViewPager(VerticalViewPager mVerticalViewPager) {
this.mVerticalViewPager = mVerticalViewPager;
}
private void setViewPager(ViewPager mvViewPager){
this.mViewPager = mvViewPager;
}
private ViewPager getViewPager(){
return mViewPager;
}
#Override
public Fragment getItem(int position) {
PageFragSanningellerKonsekvens frag = new PageFragSanningellerKonsekvens().newInstance(getViewPager(), getVerticalViewPager());
.
.
.
return frag;
}
PageFragSanningellerKonsekvens - The class that needs the reference.
public static PageFragSanningellerKonsekvens newInstance(ViewPager mViewPager, VerticalViewPager mVerticalViewPager){
PageFragSanningellerKonsekvens frag = new PageFragSanningellerKonsekvens();
frag.setViewPager(mViewPager);
frag.setVerticalViewPager(mVerticalViewPager);
return frag;
}
private void setupMenu() {
viewTop = view.findViewById(R.id.sek_top);
viewBottom = view.findViewById(R.id.sek_bottom);
viewTop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getVerticalViewPager().setCurrentItem(0, true);
}
});
viewBottom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getVerticalViewPager().setCurrentItem(2, true);
}
});
}
The code above works excellent until I rotate the device. After rotation the getVerticalViewPager() returns null. In other parts of the application I've used this pattern without any problems: on rotation the ViewPager is sent as a parameter and the application reloads without dropping the reference.
So the conclusion I've drawn is that the VerticalViewPager doesn't get reinitialized on rotation, while PageFragSanningellerKonsekvens is.
My question is; how can I solve it? Can I force it to be reinitialized, or can I obtain a reference to it in some other way?
EDIT 1: I'll think its related to getItem() in the FragmentStatePagerAdapter. It will be called to allocate 3 fragments at every given time. I think this might nullify the getVerticalViewPager() method.
EDIT 2: I moved some code to allow the ViewPager and the button to be initialized in the same class, thus making it somewhat null-proof. Thanks for the help!
Ok this might not be a complete answer but I think it would be too long a comment.
I think you are right that the null pointer happens at getItem in the adapter. (a logcat report to confirm this would be nice).
The reason for this (I think) is that the adapter tries to recreate the inner fragments before the inflation of the ViewPager is complete. Normally I create similar adapters in onCreate because it normally do not rely on anything but a fragmentmanager.
Having this in mind, and the fact that you might have a hard time avoiding the null pointer in the current design, I propose a new design.
Am only at my phone right now so is hard to give you the code I have in mind. For now I just sketch the idea:
Move the getter/setter of ViewPagers from the adapter to FragSanningEllerKonsekvens, leaving the creation of TruthOrDarePagerAdapter only depend on fragment manager
Now do the same for PageFragSanningellerKonsekvens making it simply PageFragSanningellerKonsekvens.newInstance() in getItem of the adapter
Finally, you are now missing the reference to the ViewPagers in PageFragSanningellerKonsekvens PageFragSanningellerKonsekvens, but remember you now have the getters of both ViewPagers in the parent FragSanningEllerKonsekvens
So the buttons now become:
((FragSanningEllerKonsekvens)getParentFragment()).getVerticalViewPager().setCurrentItem(0, true);
And
((FragSanningEllerKonsekvens)getParentFragment()).getVerticalViewPager().setCurrentItem(2, true);

Use same fragment in ViewPager but fragment will have different layout each time

I want to keep my application thin.
Problem: I would like to reuse my Fragment class code to create 3 different instances in the ViewPager which will have 3 pages. Each Fragment will have a different ImageView or background Drawable. What are best practices regarding this? I noticed that using factory methods like here seem to be good, any other alternatives?
I have one Fragment which has the following methods:
Fragment.java
public static Fragment newInstance(Context context) {
FragmentTutorial f = new FragmentTutorial();
Bundle args = new Bundle();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
return root;
}
I have a ViewPagerAdapter class which has the following methods:
ViewPagerAdapter.java
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
#Override
public Fragment getItem(int position) {
return new FragmentTutorial().newInstance(mContext);
}
#Override
public int getCount() {
return totalPage;
}
What I've found is the "best" way to do it (in my opinion, of course) is to do the following:
Have the fragment contain methods to set the customizable data (background, text, etc)
Note: Be careful of trying to load the data in when first creating the fragment. You may be able to set the data before onCreateView() even runs, or at other times it may run after onCreateView(). I personally use a boolean to check if the data has been set. Inside onCreateView() [or onActivityCreated()], I check if the data has been set already. If it has, load in the data. Alternatively, while setting the data, I check if the views have been created/cached already. This is done by simply having variables to cache the data, say private ImageView mBackgroundView. If the view is not null, then I safely set the data on the views.
The above is also an alternative to using newInstance, although both methods work pretty well. However, for more flexibility, I only use newInstance if a) the data is already known before the fragment has to be inserted and b) the data doesn't need to change according to input from elsewhere much.
Let the ViewPager handle all the data
Pass in all the data - a list of ImageViews, a array of Strings, define where all the data is in Resources, etc - at the very beginning [say, in the constructor]
Have the ViewPager create an ArrayList of the fragments- set up each fragment as early as possible (say when first getting all the data) and add it to the list
Let getCount() just use the size of the list
Let getItem() just get the item in the list at the position
Note: If you have any dynamic data, set it up in the getItem() method. Furthermore, you can always add more data+fragments during runtime as well [just notify the adapter that the dataset has been changed]
Essentially, the fragment is like a simple servant- it does simply the least work necessary. If it doesn't have to handle choosing the data, all the better. It'll thus be far more flexible. Just give methods to set the data/views appropriately on the fragment. Now, the ArrayAdapter can do all the grimy hard work with managing the data and giving it to the appropriate fragment. Take advantage of that.
Now, note that this is assuming you want to use a single layout but want to change different aspects of that layout (texts, background, etc). If you want to make a master fragment class that can use any sort of defined layout, you can but note that it decreases the runtime flexibility (how can you change the text or background to something you get from the internet? You simply can't if you only can define and choose from pre-set layouts).
Either way, the ArrayAdapter should take care of all the different data while the fragment simply does as it's designed to do, in a more flexible manner preferably.
Edit:
Here is the project where I most recently implemented this sort of pattern. Note that it has far more to it, so I'll replace it with some not-so-pseudo pseudo-code in the morning/afternoon.
ViewPager [a bit sloppy with all the different things I was trying to do, including extending from a FragmentStatePagerAdapter without actually using any of the specific features of a StatePagerAdapter. In other words, I still need to work on the lifecycle implementations everywhere]
Fragment [Also may be a bit sloppy but shows the pattern still]
The object (actually another fragment) that uses the ViewPager [it's actually a "VerticalViewpager" from a library, but other than the animations and direction to change the current fragment, it's exactly the same- particularly code-wise]
Edit2:
Here is a more (if overly) simplified example of the pattern described above.
Disclaimer: The following code has absolutely no lifecycle management implementations and is older code that has been untouched since around August '14
Fragment simply allows the user of the fragment to set the background color and the text of the single TextView
Link to BaseFragment
Link to layout file
The adapter creates three instances of the fragment and sets the background color and text of each. Each fragment's text, color, and total fragments is hard coded.
Link to Activity+adapter
Link to layout file
Now, here are the exact relevant portions of the code:
BaseFragment
// Note: Found out later can extend normal Fragments but must use v13 adapter
public class BaseFragment extends android.support.v4.app.Fragment {
FrameLayout mMainLayout; // The parent layout
int mNewColor = 0; // The new bg color, set from activity
String mNewText = ""; // The new text, set from activity
TextView mMainText; // The only textview in this fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the fragment's layout
View view = inflater.inflate(R.layout.fragment_base,container,false);
// Save the textview for further editing
mMainText = (TextView) view.findViewById(R.id.textView);
// Save the framelayout to change background color later
mMainLayout = (FrameLayout) view.findViewById(R.id.mainLayout);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// If there is new text or color assigned, set em
if(mNewText != ""){
mMainText.setText(mNewText);
}
if(mNewColor != 0){
mMainLayout.setBackgroundColor(mNewColor);
}
}
#Override
public void onStart(){
super.onStart();
}
// Simply indicate to change the text of the fragment
public void changeText(String newText){
mNewText=newText;
}
// Simply indicate to change the background color of the fragment
public void changeBG(int color) {
// If no color was passed, then set background to white
if(color == 0)
{
mNewColor=getResources().getColor(R.color.white);
}
// else set the color to what was passed in
else{
mNewColor=color;
}
}
}
MyAdapter
class MyAdapter extends FragmentPagerAdapter{
// Three simple fragments
BaseFragment fragA;
BaseFragment fragB;
BaseFragment fragC;
public MyAdapter(FragmentManager fm) {
super(fm);
}
public void setFragments(Context c){
// Set up the simple base fragments
fragA = new BaseFragment();
fragB = new BaseFragment();
fragC = new BaseFragment();
Resources res = c.getResources();
fragA.changeText("This is Fragment A!");
fragB.changeText("This is Fragment B!");
fragC.changeText("This is Fragment C!");
fragA.changeBG(res.getColor(R.color.dev_blue));
fragB.changeBG(res.getColor(R.color.dev_green));
fragC.changeBG(res.getColor(R.color.dev_orange));
}
#Override
public Fragment getItem(int position) {
// TODO: Make this more efficient, use a list or such, also comment more
Fragment frag = null;
if(position == 0){
frag = fragA;
}
else if(position == 1){
frag = fragB;
}
else if(position == 2){
frag = fragC;
}
return frag;
}
#Override
public int getCount() {
return 3;
}
}
You need to pass some sort of id along with newInstance() while creating instance. And according to that id you can use if..else to choose layout file.
See my reference code below:
int id;
public static Fragment newInstance(Context context, int id) {
FragmentTutorial f = new FragmentTutorial();
Bundle args = new Bundle();
this.id = id;
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(id == 1)
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment1, null);
else
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment2, null);
return root;
}
Can't you just introduce fields to the Fragment class to account for the variances in background, etc. and add them to its constructor? Then in getItem instantiate the Fragment class with different values depending on the value of position.

How to populate ViewPager with strings from ArrayList?

I have a list of items that each has their own ArrayList<String>. When an item is clicked, the ViewPager activity is opened. I want each string to fill up an entire view in ViewPager, so that the user can swipe through each of these strings as pages.
Is ViewPager the best way to go about doing this?
If so, how can I populate it with the contents of the ArrayList<String>?
You should probably use a ViewPager. To use it you need to supply a PagerAdapter. In your subclass of PagerAdapter, just have your instatiateItem override create a TextView (either programmatically, or by inflating a layout with a TextView) and set the text to the String at the position in the array corresponding to the position parameter of instantiateItem. Something like this
#Override
public Object instantiateItem (ViewGroup container, int position) {
TextView tv = new TextView(getContext());
tv.setText(mList.get(position));
return tv;
}

Categories