In my App I have a RecyclerView connected with an adapter but everytime my device changes from landscape to portrait or something else, the view get reloaded and I'll get to the top again. I want to save my scroll position to the last item I saw.
Here is what I tried:
I have a Fragment that extends from another fragment the state should be save within the MainPageFragment. I tried to save the last position loaded and put it into the savedState, it gets correctly retrieved but If I call "scrollToPosition" nothing happens.
package com.pr0.pr0grammreloaded.fragment;
/**
* Created by Dominik on 22.02.2015.
*/
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.pr0.pr0grammreloaded.MainActivity;
import com.pr0.pr0grammreloaded.R;
import com.pr0.pr0grammreloaded.config.FilterConfig;
import com.pr0.pr0grammreloaded.util.FixedRecyclerView;
import com.pr0.pr0grammreloaded.util.MainPageSaveParcelable;
import com.pr0.pr0grammreloaded.util.MyLayoutManager;
import com.pr0.pr0grammreloaded.util.RecyclerViewDelegate;
import java.util.ArrayList;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate;
/**
* Created by Dominik on 17.01.2015.
*/
public class MainPageFragment extends Pr0MainPageFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static MainPageFragment newInstance(FilterConfig config) {
MainPageFragment fragment = new MainPageFragment();
fragment.config = config;
return fragment;
}
public MainPageFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(savedInstanceState != null){
MainPageSaveParcelable saveParcelable = savedInstanceState.getParcelable("save");
View rootView = inflater.inflate(R.layout.mainpage_fragment, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.list);
adapter = new ItemAdapter();
itemList = saveParcelable.itemList;
config = saveParcelable.config;
// use better layout manager, maybe write our own?
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.getActivity(), 3));
recyclerView.setAdapter(adapter);
restorePositon = saveParcelable.position;
mPullToRefreshLayout = (PullToRefreshLayout) rootView.findViewById(R.id.ptr_layout);
// Now setup the PullToRefreshLayout
ActionBarPullToRefresh.from(getActivity())
// Mark All Children as pullable
.allChildrenArePullable()
// Set a OnRefreshListener
.listener(new OnRefreshListener() {
#Override
public void onRefreshStarted(View view) {
if (!isBlocked()) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, MainPageFragment.newInstance(MainActivity.filterConfig))
.commit();
}
}
})
// Finally commit the setup to our PullToRefreshLayout
.useViewDelegate(FixedRecyclerView.class, new RecyclerViewDelegate())
.setup(mPullToRefreshLayout);
loadFeed();
// recyclerView.scrollToPosition(saveParcelable.);
return rootView;
}else {
View rootView = inflater.inflate(R.layout.mainpage_fragment, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.list);
adapter = new ItemAdapter();
itemList = new ArrayList<>();
// use better layout manager, maybe write our own?
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.getActivity(), 3));
recyclerView.setAdapter(adapter);
mPullToRefreshLayout = (PullToRefreshLayout) rootView.findViewById(R.id.ptr_layout);
// Now setup the PullToRefreshLayout
ActionBarPullToRefresh.from(getActivity())
// Mark All Children as pullable
.allChildrenArePullable()
// Set a OnRefreshListener
.listener(new OnRefreshListener() {
#Override
public void onRefreshStarted(View view) {
if (!isBlocked()) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, MainPageFragment.newInstance(MainActivity.filterConfig))
.commit();
}
}
})
// Finally commit the setup to our PullToRefreshLayout
.useViewDelegate(FixedRecyclerView.class, new RecyclerViewDelegate())
.setup(mPullToRefreshLayout);
loadFeed();
return rootView;
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onSaveInstanceState(Bundle state){
super.onSaveInstanceState(state);
restorePositon = lastPositon;
MainPageSaveParcelable saveParcelable = new MainPageSaveParcelable(itemList,firstChunk,config,restorePositon);
state.putParcelable("save",saveParcelable);
}
}
Pr0MainPageFragment
package com.pr0.pr0grammreloaded.fragment;
import android.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pr0.pr0grammreloaded.MainActivity;
import com.pr0.pr0grammreloaded.R;
import com.pr0.pr0grammreloaded.api.Api;
import com.pr0.pr0grammreloaded.api.Feed;
import com.pr0.pr0grammreloaded.api.InstantDeserializer;
import com.pr0.pr0grammreloaded.config.FilterConfig;
import com.squareup.picasso.Picasso;
import org.joda.time.Instant;
import java.util.ArrayList;
import java.util.List;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;
import rx.functions.Action1;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout;
import static rx.android.observables.AndroidObservable.bindActivity;
/**
* Created by Dominik on 22.02.2015.
*/
public class Pr0MainPageFragment extends Fragment {
protected ItemAdapter adapter;
protected boolean firstChunk = true;
protected List<Feed.Item> itemList;
protected FilterConfig config;
private boolean blockLoading = false;
protected PullToRefreshLayout mPullToRefreshLayout;
protected int restorePositon = 0;
protected int lastPositon = 0;
protected RecyclerView recyclerView;
protected boolean isRestore = false;
/**
* Loads the feed from pr0gramm. This should be put into some kind of service
* that is injected into our activities.
*/
protected void loadFeed() {
if(!blockLoading) {
blockLoading = true;
Log.e("Pr0","Blocked loading!");
Gson gson = new GsonBuilder()
.registerTypeAdapter(Instant.class, new InstantDeserializer())
.create();
Api api = new RestAdapter.Builder()
.setEndpoint("http://pr0gramm.com")
.setConverter(new GsonConverter(gson))
.setLogLevel(RestAdapter.LogLevel.BASIC)
.build()
.create(Api.class);
// perform api request in the background and call
// back to the main thread on finish
if (firstChunk) {
bindActivity(MainActivity.getActivity(), api.itemsGet(config.getFlag(), 1)).subscribe(new Action1<Feed>() {
#Override
public void call(Feed feed) {
// we are now back in the main thread
firstChunk = false;
handleFeedResponse(feed);
}
});
} else {
//Log.e("Pr0", "Loading after ID : " + itemList.get(0).getId());
for(int x = 0; x < itemList.size();x ++){
Log.e("Pr0", "POS: " + x + ", ID : " + itemList.get(x).getId());
}
bindActivity(MainActivity.getActivity(), api.olderGet(itemList.get(itemList.size() - 1).getPromoted(), config.getFlag(), 1)).subscribe(new Action1<Feed>() {
#Override
public void call(Feed feed) {
// we are now back in the main thread
handleFeedResponse(feed);
}
});
}
}
}
/**
* Display the elements from the feed
*
* #param feed The feed to display
*/
private void handleFeedResponse(Feed feed) {
// display feed now.
//Log.i("MainActivity", "Number of items: " + feed.getItems().size());
adapter.addItems(feed.getItems());
mPullToRefreshLayout.setRefreshComplete();
restorePostion();
}
protected class ItemAdapter extends RecyclerView.Adapter<ItemView> {
ItemAdapter() {
setHasStableIds(true);
}
#Override
public ItemView onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(MainActivity.getActivity());
View view = inflater.inflate(R.layout.item_view, viewGroup, false);
return new ItemView(view);
}
#Override
public void onBindViewHolder(ItemView itemView, int i) {
// Log.e("Pr0","load id : " + i);
//Log.e("Pr0",String.valueOf(itemList.get(i).getId()));
String url = "http://thumb.pr0gramm.com/" + itemList.get(i).getThumb();
Picasso.with(getActivity())
.setIndicatorsEnabled(true);
Picasso.with(getActivity())
.load(url)
.into(itemView.image);
lastPositon = i;
Log.w("Pr0","last positon : " + i);
if(i > itemList.size() - 5){
//Log.e("Pr0","SIZE : " + itemList.size());
//Log.e("Pr0","End Reached Load After ID : " + itemList.get(0).getId());
loadFeed();
}
}
#Override
public int getItemCount() {
return itemList.size();
}
public void addItems(List<Feed.Item> itemsToAdd) {
int oldCount = itemList.size();
itemList.addAll(itemsToAdd);
notifyItemRangeInserted(oldCount, itemList.size());
/*
for(Feed.Item item : itemsToAdd) {
//Log.e("Pr0","Added image ID : " + item.getId());
int oldCount = itemList.size();
itemList.add(item);
notifyItemRangeInserted(oldCount, itemList.size());
}
*/
blockLoading = false;
Log.e("Pr0","Unblocked Loading");
//
}
#Override
public long getItemId(int position) {
return itemList.get(position).getId();
}
}
/**
* View holder for a view in the list of items
*/
private class ItemView extends RecyclerView.ViewHolder {
final ImageView image;
public ItemView(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
public boolean isBlocked(){
return this.blockLoading;
}
public void reset(){
itemList.clear();
firstChunk = true;
}
public void restorePostion(){
Log.w("Pr0", "Restore : " + restorePositon);
recyclerView.scrollToPosition(restorePositon);
}
}
And here is my Parcelable
package com.pr0.pr0grammreloaded.util;
import android.os.Parcel;
import android.os.Parcelable;
import com.pr0.pr0grammreloaded.account.User;
import com.pr0.pr0grammreloaded.api.Feed;
import com.pr0.pr0grammreloaded.config.FilterConfig;
import java.util.List;
/**
* Created by Dominik on 27.02.2015.
*/
public class MainPageSaveParcelable implements Parcelable{
public boolean firstChunk;
public List<Feed.Item> itemList;
public FilterConfig config;
public int position;
public MainPageSaveParcelable(List<Feed.Item> itemList, boolean firstChunk, FilterConfig config, int position){
this.firstChunk = firstChunk;
this.itemList = itemList;
this.config = config;
this.position = position;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(itemList);
dest.writeValue(config);
dest.writeValue(firstChunk);
dest.writeInt(position);
}
/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<MainPageSaveParcelable> CREATOR = new Parcelable.Creator<MainPageSaveParcelable>() {
public MainPageSaveParcelable createFromParcel(Parcel pc) {
return new MainPageSaveParcelable(pc);
}
public MainPageSaveParcelable[] newArray(int size) {
return new MainPageSaveParcelable[size];
}
};
/**Ctor from Parcel, reads back fields IN THE ORDER they were written */
public MainPageSaveParcelable(Parcel pc){
pc.readList(itemList,List.class.getClassLoader());
config = (FilterConfig) pc.readValue(FilterConfig.class.getClassLoader());
firstChunk = (boolean) pc.readValue(Boolean.class.getClassLoader());
position = pc.readInt();
}
}
you can use scroll listener and save first items or RecyclerView state on scrolling in SharedPreference or whatever you prefer
define these variables as private variables in your activity or fragment
int firstCompleteVisibleItemPosition;
int firstVisibleItemPosition;
Parcelable recyclerViewState;
use recyclerview on scroll listener
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
firstCompleteVisibleItemPosition = recyclerView.getLayoutManager().findFirstCompletelyVisibleItemPosition();
firstVisibleItemPosition = recyclerView.getLayoutManager().findFirstVisibleItemPosition();
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
}
});
and if orientation changes you can start your recyclerview by last position u saved by this method
linearLayoutManager.scrollToPositionWithOffset(firstVisibleItemPosition, 0);
or by last state of recyclerview
rv.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Related
I face a difficulty when working using recyclerview and Gridview. The problem is when my application success load next page (next data), the recyclerview always back to the top.
I want the recyclerview start from last index.
Example : First page load 10 items, after success loan next page, the recycler view start scrolling from item 8.
For resolve that problem, i have tried all the solution on stackoverflow still get nothing.
Here are my code :
package com.putuguna.sitehinduapk.adapters;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.putuguna.sitehinduapk.R;
import com.putuguna.sitehinduapk.activities.DetailBlogPostActivity;
import com.putuguna.sitehinduapk.models.listposts.ItemPostModel;
import com.putuguna.sitehinduapk.utils.GlobalFunction;
import com.putuguna.sitehinduapk.utils.GlobalVariable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class ListPostAdapter extends RecyclerView.Adapter<ListPostAdapter.ViewHolder>{
private List<ItemPostModel> mListPost;
private Context mContext;
public ListPostAdapter(List<ItemPostModel> mListPost, Context mContext) {
this.mListPost = mListPost;
this.mContext = mContext;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.adapter_item_list_post, null);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ItemPostModel post = mListPost.get(position);
List<String> listURL = extractUrls(post.getContentPosting());
Glide.with(mContext)
.load(listURL.get(0))
.into(holder.ivImagePost);
holder.tvTitle.setText(post.getTitle());
holder.llItemPost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TO DO OnClick
}
});
List<String> listLabel = post.getListLabel();
String labelPost="";
for(int i=0; i<listLabel.size();i++){
labelPost += "#"+listLabel.get(i) + " ";
}
holder.tvLabel.setText(labelPost);
if ((position >= getItemCount() - 1))
load();
}
public abstract void load();
#Override
public int getItemCount() {
return mListPost.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView ivImagePost;
public TextView tvTitle;
public LinearLayout llItemPost;
public TextView tvLabel;
public ViewHolder(View itemView) {
super(itemView);
ivImagePost = (ImageView) itemView.findViewById(R.id.iv_image_post);
tvTitle = (TextView) itemView.findViewById(R.id.tv_blog_title);
llItemPost = (LinearLayout) itemView.findViewById(R.id.ll_item_post);
tvLabel = (TextView) itemView.findViewById(R.id.textview_label_adapter);
}
}
public static List<String> extractUrls(String input) {
List<String> result = new ArrayList<String>();
Pattern pattern = Pattern.compile(
"\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
"(\\w+:\\w+#)?(([-\\w]+\\.)+(com|org|net|gov" +
"|mil|biz|info|mobi|name|aero|jobs|museum" +
"|travel|[a-z]{2}))(:[\\d]{1,5})?" +
"(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
"((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
"(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
"(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
result.add(matcher.group());
}
return result;
}
}
This code of MainActivity.java
private void getListPost(){
mSwipeRefreshLayout.setRefreshing(true);
String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
BloggerApiService apiService = BloggerApiClient.getClient().create(BloggerApiService.class);
Call<ListPostModel> call = apiService.getListPost(GlobalVariable.APP_KEY_V3);
call.enqueue(new Callback<ListPostModel>() {
#Override
public void onResponse(Call<ListPostModel> call, Response<ListPostModel> response) {
ListPostModel listpost = response.body();
initDataView(listpost);
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<ListPostModel> call, Throwable t) {
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
/**
* this method used for post (next page)
*/
private void getNextListPost(){
mSwipeRefreshLayout.setRefreshing(true);
String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
BloggerApiService apiService = BloggerApiClient.getClient().create(BloggerApiService.class);
Call<ListPostModel> call = apiService.getNexPageListPost(GlobalVariable.APP_KEY_V3,nextPageToken);
call.enqueue(new Callback<ListPostModel>() {
#Override
public void onResponse(Call<ListPostModel> call, Response<ListPostModel> response) {
ListPostModel listpost = response.body();
initDataView2(listpost);
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<ListPostModel> call, Throwable t) {
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onRefresh() {
mListPost.clear();
getListPost();
}
private void initDataView(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
mListPost.addAll(listpost.getListItemsPost());
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
getNextListPost();
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
//mPostAdapter.notifyDataSetChanged();
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
}
/**
* this method used for set view (next page)
* #param listpost
*/
private void initDataView2(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
List<ItemPostModel> itemNextPost = listpost.getListItemsPost();
// itemNextPost.addAll(mListPost);
mListPost.addAll(itemNextPost);
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
if(nextPageToken==null){
}else{
getNextListPost();
}
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
an
}
Any suggestion will be appreciated.
The problem with the code is, your resetting the complete data again into your adapter and setting it to RecyclerView again, That's why it is re instanced everything in RecyclerView and i.e scrolling to top. Instead of that you can try something like just add /append the updated data into the list ( where you holds the data ) and then just call the adapter.notifyDataSetChanged(); method. automatically it will add the updated data and you no need to set the recycler view again.
Probably for your case you need to change your initDataView2() mehtod like below
private void initDataView2(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
mListPost.addAll(listpost.getListItemsPost());\
mPostAdapter .notifyDataSetChanged();
}
For nextPageToken thing you can move into your main code onCreate() or the initDataView() method where you already have the adapter initialization
like this,
private void initDataView(ListPostModel listpost){
GlobalFunction.saveString(this,GlobalVariable.keySharedPreference.TOKEN_PAGINATION, listpost.getNextPageToken());
final String nextPageToken = GlobalFunction.getStrings(this, GlobalVariable.keySharedPreference.TOKEN_PAGINATION);
mListPost.addAll(listpost.getListItemsPost());
mPostAdapter = new ListPostAdapter(mListPost, this) {
#Override
public void load() {
ItemPostModel item = mListPost.get(mListPost.size()-1);
if(nextPageToken==null){
}else{
getNextListPost();
}
}
};
mRecyclerviewPost.setAdapter(mPostAdapter);
//mPostAdapter.notifyDataSetChanged();
mRecyclerviewPost.setHasFixedSize(true);
mGridViewLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
mRecyclerviewPost.setLayoutManager(mGridViewLayoutManager);
}
I hope it will work :)
I want to implement endless scrolling in recyclerview by calling new JSON request with increment of page number and adding those result with the previous results. first request shows 20 results but there are 200+ results left to show.How can i have endless scroll with onLoadMore() function in 'TopratedFragment.java'.Please help with the codes
TopratedFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.mitab.mentor.Movies.Pages.L;
import com.example.mitab.mentor.Movies.Pages.MyApplication;
import com.example.mitab.mentor.Movies.Pages.VolleySingleton;
import com.example.mitab.mentor.Movies.Pages.movie;
import com.example.mitab.mentor.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import static com.example.mitab.mentor.Movies.Pages.Keys.EndpointToprated.*;
/**
* A simple {#link Fragment} subclass.
* Use the {#link TopratedFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class TopratedFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String Top_rated="http://api.themoviedb.org/3/movie/top_rated";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
protected Handler handler;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
int page=1;
private RequestQueue requestQueue;
private ArrayList<movie> listMovies=new ArrayList<>();
private ArrayList<movie> newlistMovies=new ArrayList<>();
private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
private RecyclerView listMovieHits;
private AdapterToprated adapterToprated;
boolean loadingMore = false;
private int lastVisibleItemId=0;
public TopratedFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static TopratedFragment newInstance(String param1, String param2) {
TopratedFragment fragment = new TopratedFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
volleySingleton= VolleySingleton.getsInstance();
requestQueue=volleySingleton.getRequestQueue();
sendJsonRequest();
}
private void sendJsonRequest(){
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, getRequestUrl(page), (String)null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
listMovies=parseJSONResponse(response);
adapterToprated.setMovieList(listMovies);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
private ArrayList<movie> parseJSONResponse(JSONObject response){
ArrayList<movie> listMovies=new ArrayList<>();
if (response==null || response.length()>0){
try {
JSONArray arrayMovies=response.getJSONArray(KEY_MOVIES);
for (int i=0;i<arrayMovies.length();i++){
long id=-1;
String title="NA";
String poster="NA";
String release="NA";
String overview="NA";
String vote="NA";
String votecount="NA";
JSONObject currentMovie=arrayMovies.getJSONObject(i);
//get the id of current movie
if (currentMovie.has(KEY_ID)&& !currentMovie.isNull(KEY_ID)){
id=currentMovie.getLong(KEY_ID);
}
if (currentMovie.has(KEY_TITLE)&& !currentMovie.isNull(KEY_TITLE)){
title=currentMovie.getString(KEY_TITLE);
}
if (currentMovie.has(KEY_POSTER_PATH)&& !currentMovie.isNull(KEY_POSTER_PATH)){
poster=currentMovie.getString(KEY_POSTER_PATH);
}
if (currentMovie.has(KEY_RELEASE_DATE)&& !currentMovie.isNull(KEY_RELEASE_DATE)){
release=currentMovie.getString(KEY_RELEASE_DATE);
}
if (currentMovie.has(KEY_OVERVIEW)&& !currentMovie.isNull(KEY_OVERVIEW)){
overview=currentMovie.getString(KEY_OVERVIEW);
}
if (currentMovie.has(KEY_AVERAGE_VOTE)&& !currentMovie.isNull(KEY_AVERAGE_VOTE)){
vote=currentMovie.getString(KEY_AVERAGE_VOTE);
}
if (currentMovie.has(KEY_VOTE_COUNT)&& !currentMovie.isNull(KEY_VOTE_COUNT)){
votecount=currentMovie.getString(KEY_VOTE_COUNT);
}
JSONArray genre=currentMovie.getJSONArray(KEY_GENRE_IDS);
if (currentMovie.has(KEY_GENRE_IDS)&& !currentMovie.isNull(KEY_GENRE_IDS)){
for (int j=0;j<genre.length();j++){
try {
String itemInArray=genre.getString(j);
}
catch (JSONException e){
}
}
}
movie movie=new movie();
movie.setId(id);
movie.setTitle(title);
movie.setOverview(overview);
movie.setAveragevote(vote);
Date date=null;
try {
date=dateFormat.parse(release);
}
catch (ParseException e){
}
movie.setReleasedate(date);
movie.setImage(poster);
movie.setVotecount(votecount);
if (id!=-1 && !title.equals("NA"))
{
listMovies.add(movie);
}
}
}
catch (JSONException e){
}
}
return listMovies;
}
public static String getRequestUrl(int page){
return Top_rated +"?api_key="+ MyApplication.API_KEY + "&page="+page;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_toprated, container, false);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
handler=new Handler();
listMovieHits=(RecyclerView) view.findViewById(R.id.listMovieHits);
listMovieHits.setLayoutManager(linearLayoutManager);
adapterToprated=new AdapterToprated(getActivity());
listMovieHits.setAdapter(adapterToprated);
//sendJsonRequest();
// Add the scroll listener
listMovieHits.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount) {
loadingMore=true;
sendJsonRequest();
int curlsize=adapterToprated.getItemCount();
listMovies.addAll(newlistMovies);
adapterToprated.notifyItemRangeChanged(curlsize,listMovies.size()-2);
}
});
// Inflate the layout for this fragment
return view;
}
}
AdapterToprated.java
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.mitab.mentor.MainActivity;
import com.example.mitab.mentor.Movies.Pages.L;
import com.example.mitab.mentor.Movies.Pages.VolleySingleton;
import com.example.mitab.mentor.Movies.Pages.movie;
import com.example.mitab.mentor.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class AdapterToprated extends RecyclerView.Adapter<AdapterToprated.ViewHolderToprated> {
private LayoutInflater layoutInflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private ArrayList<movie> listMovies=new ArrayList<movie>();
private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
private RecyclerView.OnItemTouchListener onItemTouchListener;
Context context;
public AdapterToprated(Context context){
layoutInflater=LayoutInflater.from(context);
volleySingleton=VolleySingleton.getsInstance();
imageLoader=volleySingleton.getImageLoader();
this.context = context;
}
public void setMovieList(ArrayList<movie> listMovies){
this.listMovies=listMovies;
notifyItemRangeChanged(0, listMovies.size());
}
#Override
public ViewHolderToprated onCreateViewHolder(ViewGroup parent, int viewType) {
View view=layoutInflater.inflate(R.layout.individual_toprated, parent, false);
ViewHolderToprated viewHolderToprated=new ViewHolderToprated(view);
return viewHolderToprated;
}
#Override
public void onBindViewHolder(final ViewHolderToprated holder, int position) {
final movie currentMovie=listMovies.get(position);
holder.movieTitle.setText(currentMovie.getTitle());
Date movieReleaseDate=currentMovie.getReleasedate();
if (movieReleaseDate!=null){
String formattedDate=dateFormat.format(movieReleaseDate);
holder.movieReleaseDate.setText(formattedDate);
}else{
holder.movieReleaseDate.setText("NA");
}
holder.movieRating.setText(currentMovie.getAveragevote());
String urlThumnail=currentMovie.getImage();
if (!urlThumnail.equals("NA")){
imageLoader.get(urlThumnail, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.movieThumbnail.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
holder.lnrLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,Intentpass.class);
Bundle extras=new Bundle();
extras.putString("Title","currentMovie.getTitle()");
extras.putString("Date","formattedDate");
extras.putString("Rating","currentMovie.getAveragevote()");
intent.putExtras(extras);
context.startActivity(intent);
//intent.putExtra("details",currentMovie.getTitle());
//context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return listMovies.size();
}
static class ViewHolderToprated extends RecyclerView.ViewHolder{
private ImageView movieThumbnail;
private TextView movieTitle;
private TextView movieReleaseDate;
private TextView movieRating;
private RelativeLayout lnrLayout;
public ViewHolderToprated(View itemView) {
super(itemView);
movieThumbnail=(ImageView) itemView.findViewById(R.id.movieThumbnail);
movieTitle=(TextView) itemView.findViewById(R.id.movieTitle);
movieReleaseDate=(TextView) itemView.findViewById(R.id.movieReleaseDate);
movieRating=(TextView) itemView.findViewById(R.id.movieRating);
lnrLayout=(RelativeLayout)itemView.findViewById(R.id.lnLayout);
}
}
}
EndlessRecyclerViewScrollListener.java
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
RecyclerView.LayoutManager mLayoutManager;
public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
}
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public int getLastVisibleItem(int[] lastVisibleItemPositions) {
int maxSize = 0;
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i];
}
else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i];
}
}
return maxSize;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
#Override
public void onScrolled(RecyclerView view, int dx, int dy) {
int lastVisibleItemPosition = 0;
int totalItemCount = mLayoutManager.getItemCount();
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
// get maximum element within the list
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
} else if (mLayoutManager instanceof LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
} else if (mLayoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
}
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) {
this.loading = true;
}
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount);
loading = true;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
}
You only have to add your new data to the last position of list movies and everything will work fine. For this-
Create a new List like ArrayList<movie> newListMovies and add your updated data into it. Now add newListMovies into listMovies and notify to adapter. Just like below-
ArrayList<movie> newListMovies = new ArrayList<>();
// your code..
listMovieHits.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount) {
loadingMore=true;
sendJsonRequest();
int currentSize = adapter.getItemCount();
listMovies.addAll(newListMovies);
adapter.notifyItemRangeInserted(currentSize, listMovies.size() - 2);
}
});
Hope it will help.
I am using a RecyclerView and after a user input I would like to update the RecyclerView. But there simply happens nothing.
The Layout is just refreshed when I reload the whole Fragment again.
My code:
package com.stack.overflow.fragment.Einstellungen;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.stack.overflow.HelperClasses.RecyclerView.DividerItemDecoration;
import com.stack.overflow.HelperClasses.RecyclerView.ItemClickSupport;
import com.stack.overflow.R;
import com.stack.overflow.fragment.Einstellungen.QrCode.FragmentQrCode;
import com.afollestad.materialdialogs.MaterialDialog;
import com.pixplicity.easyprefs.library.Prefs;
import java.util.ArrayList;
import java.util.List;
public class FragmentEinstellungen extends Fragment {
private static final String KEY_STUFE = "Stufe";
private static final String DEFAULT_EINTRAG_STUFE = "keine Stufe ausgewählt";
private RecyclerView mRecyclerView;
private EinstellungenDataAdapter mAdapter;
private static String[] titles = null;
private static String[] titlesStufen = null;
private static String[] titlesKlassen = null;
int callbackDialog;
String stufe = "";
public FragmentEinstellungen(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titles = getActivity().getResources().getStringArray(R.array.einstellungen);
titlesStufen = getActivity().getResources().getStringArray(R.array.stufenauswahl);
titlesKlassen = getActivity().getResources().getStringArray(R.array.klassenauswahl);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_einstellungen, container, false);
mRecyclerView = (RecyclerView) layout.findViewById(R.id.einstellungenRecyclerView);
mAdapter = new EinstellungenDataAdapter(getActivity(), getData());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
final ItemClickSupport itemClick = ItemClickSupport.addTo(mRecyclerView);
itemClick.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView parent, View child, int position, long id) {
//SnackbarManager.show(
// Snackbar.with(getActivity())
// .text("KLICK"));
displayView(0);
}
});
return layout;
}
public static List<EinstellungenItem> getData() {
List<EinstellungenItem> data = new ArrayList<>();
// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
EinstellungenItem navItem = new EinstellungenItem();
navItem.setTitle(titles[i]);
if(i==0){
navItem.setSubtitle("kein gültiger Scan");
} else if(i==1){
navItem.setSubtitle(Prefs.getString(KEY_STUFE, DEFAULT_EINTRAG_STUFE));
}
data.add(navItem);
}
return data;
}
private void displayView(int position) {
switch (position) {
case 0:
break;
case 1:
new MaterialDialog.Builder(getActivity())
.title(R.string.title_dialog_stufe)
.items(R.array.stufenauswahl)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
#Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
callbackDialog = which;
stufe = titlesStufen[which];
if (callbackDialog <= 4) {
new MaterialDialog.Builder(getActivity())
.title(R.string.title_dialog_klasse)
.items(R.array.klassenauswahl)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
#Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
stufe = stufe + titlesKlassen[which];
Prefs.putString(KEY_STUFE, stufe);
mAdapter.notifyDataSetChanged();
return true;
}
})
.positiveText(R.string.choosetext_dialog_klasse)
.show();
} else {
Prefs.putString(KEY_STUFE, stufe);
mAdapter.notifyDataSetChanged();
}
return true;
}
})
.positiveText(R.string.choosetext_dialog_stufe)
.show();
callbackDialog = 0;
break;
default:
break;
}
}
}
Am I doing the refresh at the wrong time?
Your mAdapter uses getData() which returns every time another instance of new ArrayList<>() and when you call the mAdapter.notifyDataSetChanged(); the adapter tries to refresh the old instance of the arrayList but there is no changes.
You need to save your data into a field and update that field like mArrayList.clear() and mArrayList.addAll(getData()).
Clearing or adding data does not get updated in the method addData in my custom ArrayAdapter class. What is going on? I have also tried updating the List which is fed into the ArrayAdapter and that does not get changed either, both times the amount of items in the adapter stay the same. Please help!
Class:
package sukh.app.ireddit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* While this looks like a lot of code, all this class
* actually does is load the posts in to the listview.
*
* #author Hathy
*/
public class PostsFragment extends Fragment{
ListView postsList;
PostAdapter adapter;
Handler handler;
Activity activity;
String subreddit;
List<Post> posts;
PostsHolder postsHolder;
public PostsFragment(){
handler=new Handler();
posts=new ArrayList<Post>();
activity=getActivity();
}
public static Fragment newInstance(String subreddit){
PostsFragment pf=new PostsFragment();
pf.subreddit=subreddit;
pf.postsHolder=new PostsHolder(pf.subreddit);
return pf;
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.posts
, container
, false);
postsList=(ListView)v.findViewById(R.id.posts_list);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initialize();
}
private void initialize(){
// This should run only once for the fragment as the
// setRetainInstance(true) method has been called on
// this fragment
if(posts.size()==0){
// Must execute network tasks outside the UI
// thread. So create a new thread.
new Thread(){
public void run(){
posts.addAll(postsHolder.fetchPosts());
// UI elements should be accessed only in
// the primary thread, so we must use the
// handler here.
handler.post(new Runnable(){
public void run(){
createAdapter();
}
});
}
}.start();
}else{
createAdapter();
}
}
/**
* This method creates the adapter from the list of posts
* , and assigns it to the list.
*/
private void createAdapter(){
// Make sure this fragment is still a part of the activity.
if(getActivity()==null) return;
adapter=new PostAdapter(posts);
postsList.setAdapter(adapter);
Log.i("sukh", "setting adapter");
postsList.setOnScrollListener(adapter);
}
protected class PostAdapter extends ArrayAdapter<Post> implements OnScrollListener {
private int previousTotal = 0;
private boolean loading = true;
//PostAdapter _adapter = this;
public PostAdapter(List<Post> posts){
super(getActivity(), R.layout.post_item, posts);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.post_item, null);
}
Post thePost = getItem(position);
TextView postTitleTextView = (TextView) convertView.findViewById(R.id.post_title);
postTitleTextView.setText(thePost.getTitle());
TextView postDetailsTextView = (TextView) convertView.findViewById(R.id.post_details);
postDetailsTextView.setText(thePost.getDetails());
TextView postScoreTextView = (TextView) convertView.findViewById(R.id.post_score);
postScoreTextView.setText(thePost.getScore());
return convertView;
}
private void addData() {
//final List<Post> newPosts = new ArrayList<Post>();
Log.i("update1", this.adapterToString());
new Thread(){
public void run(){
//neither of these following statements make a difference
//neither does posts.clear() or posts.addAll...
//adapter.addAll(postsHolder.fetchMorePosts());
adapter.clear();
}
};
Log.i("sukh", "executed scroll");
adapter.notifyDataSetChanged();
Log.i("update2", this.adapterToString());
}
private String adapterToString() {
StringBuilder sb = new StringBuilder();
int total = this.getCount();
sb.append("contents: ");
/*for (int i = 0; i < this.getCount(); i ++) {
sb.append(this.getItem(i).title + "/n");
}*/
//return sb.toString();
return sb.append(total).toString();
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading &&
(firstVisibleItem + visibleItemCount + 1 >= totalItemCount)
) {
addData();
loading = true;
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
}
EDIT: fixed. Added new class that extends ASyncTask and had it fetchMorePosts before updating the adapter.
Class is shown :
private class AddPostsTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
Log.i("update2", "start fetchMorePosts");
posts.addAll(postsHolder.fetchMorePosts());
Log.i("update2", "end fetchMorePosts");
return null;
}
#Override
protected void onPostExecute(Void result) {
Log.i("update2", "start adapterChange");
adapter.notifyDataSetChanged();
Log.i("update2", "start adapterChange");
super.onPostExecute(result);
}
}
in your addData function, you're clearing adapter in a thread 'after' notifyDataSetChanged() is called.
One of my app activity is called DayGallery activity (infinite images gallery) ,
when i open the gallery, it show first image randomly , and not start with first image i specified in DayGallery activity code .
what am trying to achieve is:
FIRST: start with first image specified in DayGallery activity code as below :
when open Day1 gallery ,first image to appear is:
R.drawable.day_one_1
and when open Day2 gallery ,first image to appear is:
R.drawable.day_two_1
and like that for all Days gallery, also keep infinite scrolling in both sides.
SECOND : if am in gallery stopped on image named day_one_7 for example then press back to go to previous activity and return again to gallery , i want to see the same image i saw before i left gallery ,
but if i exit the app then open the gallery again , it must reset to show the first image i specified in DayGallery activity code , explained as bellow image .
actually i searched google for that purpose but i cant get any helpful thing about it ,
any help will be highly appreciated .
DayGallery.java:
#SuppressWarnings("deprecation")
public class DayGallery extends Activity {
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// Set the layout to use
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
tv = (TextView) findViewById(R.id.title_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
}
InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
galleryOne.setAdapter(initializeImages());
galleryOne.setSelection(galleryOne.getCount()/2);
}
private InfiniteGalleryAdapter initializeImages() {
InfiniteGalleryAdapter galleryAdapter = null;
String day = getIntent().getStringExtra("dayname");
if(day.equalsIgnoreCase("Day1")){
int[] tempimages = { R.drawable.day_one_1, R.drawable.day_one_2,R.drawable.day_one_3,
R.drawable.day_one_4, R.drawable.day_one_5,R.drawable.day_one_6,R.drawable.day_one_7,
R.drawable.day_one_8, R.drawable.day_one_9,R.drawable.day_one_10,R.drawable.day_one_11,
R.drawable.day_one_12
};
String[] name = { "00:35","00:35","00:35","1:07","2:00","2:01","2:09",
"2:12","2:15","6:13","6:13","6:13"
};
tv.setText("Day one pictures");
galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);
}
else if(day.equalsIgnoreCase("Day2")){
int[] tempimages = { R.drawable.day_two_1, R.drawable.day_two_2,R.drawable.day_two_3,
R.drawable.day_two_4, R.drawable.day_two_5,R.drawable.day_two_6,R.drawable.day_two_7,
R.drawable.day_two_8, R.drawable.day_two_9,R.drawable.day_two_10,R.drawable.day_two_11,
R.drawable.day_two_12
};
String[] name = { "12:04","12:04", "12:04","12:05","12:06", "12:07",
"12:07","12:07","12:08","12:10","12:10","12:10"
};
tv.setText("Day two pictures");
galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);
}
// AND THE SAME FOR REST OF DAYS TILL Day10//
return galleryAdapter;
}
}
class InfiniteGalleryAdapter extends BaseAdapter {
private Context mContext;
private int[] images;
private String[] name;
public InfiniteGalleryAdapter(Context c, int[] imageIds,String[] names) {
this.mContext = c;
images = imageIds;
name=names;
inflater = (LayoutInflater)mContext.getSystemService ( Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
private LayoutInflater inflater=null;
public class ViewHolder{
public TextView text;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = getImageView();
int itemPos = (position % images.length);
try { i.setImageResource(images[itemPos]); ((BitmapDrawable) i.getDrawable()).setAntiAlias(true);
}
catch (OutOfMemoryError e) { Log.e("InfiniteGalleryAdapter", "Out of memory creating imageview. Using empty view.", e);
}
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.gallery_items, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else holder=(ViewHolder)vi.getTag();
holder.text.setText(name[itemPos]);
final int stub_id=images[itemPos];
holder.image.setImageResource(stub_id);
return vi;
}
private ImageView getImageView() {
ImageView i = new ImageView(mContext);
return i;
}
}
#SuppressWarnings("deprecation")
class InfiniteGallery extends Gallery {
public InfiniteGallery(Context context) {
super(context);
init();
}
public InfiniteGallery(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public InfiniteGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
// These are just to make it look pretty
setSpacing(25);
setHorizontalFadingEdgeEnabled(false);
}
}
UPDATE:
I add this line of code :
galleryOne.setSelection(0);
after this line :
galleryOne.setSelection(galleryOne.getCount()/2);
in my code it result in showing the first image as specified it in DayGallery activity , but it result to one way infinite scrolling to left side only but not in both side ,
How we can get two way infinite scrolling of my gallery images with showing the first image as specified it in DayGallery activity ?
really appreciate any help , thanks.
I can tell you the logic to write infinite viewpager which uses fragments. This viewpager can scroll in both direction infinitely. Also it can be launched from any particular fragment. My requiremnet was:
I had an ebook and user can click on any menu(chapters) and it should launch that and then it should be possible to scroll both ways. I guess your are trying to achieve same thing with gallery:
Check the code:
InfinitePagerAdapter.java:
package com.example.multiplepages;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
* A PagerAdapter that wraps around another PagerAdapter to handle paging
* wrap-around.
*
*/
public class InfinitePagerAdapter extends PagerAdapter {
private static final String TAG = "InfinitePagerAdapter";
private static final boolean DEBUG = true;
private PagerAdapter adapter;
public InfinitePagerAdapter(PagerAdapter adapter) {
this.adapter = adapter;
}
#Override
public int getCount() {
// warning: scrolling to very high values (1,000,000+) results in
// strange drawing behaviour
return Integer.MAX_VALUE;
}
/**
* #return the {#link #getCount()} result of the wrapped adapter
*/
public int getRealCount() {
return adapter.getCount();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
debug("getRealCount: " + getRealCount());
int virtualPosition = position % getRealCount();
debug("instantiateItem: real position: " + position);
debug("instantiateItem: virtual position: " + virtualPosition);
// only expose virtual position to the inner adapter
return adapter.instantiateItem(container, virtualPosition);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
int virtualPosition = position % getRealCount();
debug("destroyItem: real position: " + position);
debug("destroyItem: virtual position: " + virtualPosition);
// only expose virtual position to the inner adapter
adapter.destroyItem(container, virtualPosition, object);
}
/*
* Delegate rest of methods directly to the inner adapter.
*/
#Override
public void finishUpdate(ViewGroup container) {
adapter.finishUpdate(container);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return adapter.isViewFromObject(view, object);
}
#Override
public void restoreState(Parcelable bundle, ClassLoader classLoader) {
adapter.restoreState(bundle, classLoader);
}
#Override
public Parcelable saveState() {
return adapter.saveState();
}
#Override
public void startUpdate(ViewGroup container) {
adapter.startUpdate(container);
}
/*
* End delegation
*/
private void debug(String message) {
if (DEBUG) {
Log.d(TAG, message);
}
}
}
InfiniteViewPager.java:
package com.example.multiplepages;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
/**
* A {#link ViewPager} that allows pseudo-infinite paging with a wrap-around
* effect. Should be used with an {#link InfinitePagerAdapter}.
*
*/
public class InfiniteViewPager extends ViewPager {
int mPageOffset = 0;
public InfiniteViewPager(Context context) {
super(context);
}
public InfiniteViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setAdapter(PagerAdapter adapter) {
super.setAdapter(adapter);
// offset first element so that we can scroll to the left
setCurrentItem(0);
}
public void setpageOffset(int pageOffset) {
mPageOffset = pageOffset;
}
#Override
public void setCurrentItem(int item) {
// offset the current item to ensure there is space to scroll
item = getOffsetAmount() + (item % getAdapter().getCount());
item = item + mPageOffset;
super.setCurrentItem(item);
}
private int getOffsetAmount() {
if (getAdapter() instanceof InfinitePagerAdapter) {
InfinitePagerAdapter infAdapter = (InfinitePagerAdapter) getAdapter();
// allow for 100 back cycles from the beginning
// should be enough to create an illusion of infinity
// warning: scrolling to very high values (1,000,000+) results in
// strange drawing behaviour
return infAdapter.getRealCount() * 100;
} else {
return 0;
}
}
}
MultiplePageScroll.java : This is MainActivity
package com.example.multiplepages;
import java.util.ArrayList;
import java.util.List;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
public class MultiplePageScroll extends FragmentActivity {
private int listSize = 0;
public int listPos = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_multiple_page_scroll);
Bundle bundle = getIntent().getExtras();
listSize = bundle.getInt("listSize");
listPos = bundle.getInt("itemPosition");
PagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
#Override
public int getCount() {
return listSize;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt("identifier", position);
fragment.setArguments(args);
return fragment;
}
};
// wrap pager to provide infinite paging with wrap-around
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
// actually an InfiniteViewPager
InfiniteViewPager viewPager = (InfiniteViewPager) findViewById(R.id.pager);
viewPager.setpageOffset(listPos);
viewPager.setAdapter(wrappedAdapter);
}
}
PageFragment.java:
package com.example.multiplepages;
import java.io.IOException;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
//import android.webkit.WebSettings;
import android.widget.Toast;
public class PageFragment extends Fragment {
String path="s_english/contents";
AssetManager assMan = null;
int pageIndex;
String pagePath = null;
String[] pageList = null;
private int listPosition;
public static PageFragment newInstance(int index) {
PageFragment pageFragment = new PageFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", index);
pageFragment.setArguments(bundle);
return pageFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
listPosition = args.getInt("identifier");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
WebView mWebView = (WebView) view.findViewById(R.id.webview);
mWebView.addJavascriptInterface(new JavaScriptInterface(getActivity()), "NativeFunc");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.getSettings().setBuiltInZoomControls(true);
assMan = getActivity().getAssets();
try {
pageList = assMan.list(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pageIndex = listPosition;
pagePath=pageList[pageIndex];
mWebView.loadUrl("file:///android_asset/s_english/contents/" + pagePath);
//mWebView.loadUrl("http://192.168.0.33:8080/orginalsource/contents/" + pagePath);
//MultiplePageScroll.mSpinner.setVisibility(View.GONE);
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("dummy", true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
}
activity_multiple_page_scroll.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.multiplepages.InfiniteViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
You can use sharedPrefernce to save the launchedpage and use that. By modifying this code a little you can achieve what you want.