SwipeRefreshLayout not update recyclerview - java

When I use SwipeRefreshLayout .
.recyclerview is not updated
. In the setOnRefreshListener method, I wrote this command:
The recyclerview commands are currently not updated with these commands.
(I mean, when I edit the information from the server, it will not be displayed)
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
setupRecyclerView();
swipeRefreshLayout.setRefreshing(false);
}
});
private void setupRecyclerView() {
recyclerView.setLayoutManager(linearLayoutManager);
linearLayoutManager.setSmoothScrollbarEnabled(false);
recyclerView.setAdapter(postsAdapter);
getPosts(1);
listemer = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
progressBar.setVisibility(View.VISIBLE);
getPosts(page);
Log.e("PAGe ", "" + page);
}
};
recyclerView.addOnScrollListener(listemer);
public void getPosts(int page) {
apiService.getPosts(page, new ApiService.OnPostsReceived() {
#Override
public void onReceived(List<Post> posts) {
openHelper.addPosts(posts);
postsAdapter.addpost(posts);
postsAdapter.notifyDataSetChanged();
progressDialog.hide();
progressBar.setVisibility(View.GONE);
}
});

Related

How to add Pagingnation to a Recycleview in android studio

Hello guys please how can i add pagingnation to a recycleview in android studio using java. Am trying to load photos from firebase database which is working fine, but the issue i have is that its trying to load all photos at once which makes the app run slow. How can i load at list 6 photos first and when the user scroll it load more photos.
Here is my code for my My fragment
public class MemesFragment extends Fragment {
private RecyclerView recyclerViewMemePosts;
private MemePostAdapter memePostAdapter;
private List<MemePosts> memePosts;
private DatabaseReference root = FirebaseDatabase.getInstance().getReference("MemePosts");
private ProgressBar progressBar;
private Button upload;
private Context mContext;
private AdView mAdView, adView;
private ImageView shocker;
private InterstitialAd mInterstitialAd;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_memes, container, false);
try {
progressBar = view.findViewById(R.id.progress_circular);
shocker = view.findViewById(R.id.memeMaker);
shocker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(mContext, MemeMakerActivity.class));
}
});
recyclerViewMemePosts = view.findViewById(R.id.recycleViewPostsMeme);
recyclerViewMemePosts.setHasFixedSize(true);
recyclerViewMemePosts.setItemViewCacheSize(20);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
recyclerViewMemePosts.setLayoutManager(linearLayoutManager);
memePosts = new ArrayList<>();
memePostAdapter = new MemePostAdapter(getContext(), memePosts);
recyclerViewMemePosts.setAdapter(memePostAdapter);
mContext = getActivity();
root.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
try {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
MemePosts model = dataSnapshot.getValue(MemePosts.class);
memePosts.add(model);
}
memePostAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
} catch (Exception e) {
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
upload = view.findViewById(R.id.Upload);
upload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(mContext, MemePostActivity.class));
}
});
MobileAds.initialize(mContext, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
MobileAds.initialize(mContext, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
adView.loadAd(adRequest);
} catch (Exception e) {
e.getMessage();
}
return view;
}

Move object from a RecyclerView to another RecyclerView

I'm currently making a booking app for some football fields. I have two recycler views: active bookings and cancelled bookings. I also have a custom recycler view adapter. Both of the recycler views use the same adapter.
What I want to do: if I click on a button inside my active bookings recycler view, I want to remove the booking from the active bookings and put it into my cancelled bookings. I'm gonna leave my custom adapter with the mention that I use Firebase in order to get my booking status (active or cancelled).
public class RecyclerViewAdapterRezervare extends RecyclerView.Adapter<RecyclerViewAdapterRezervare.RecycleViewHolder> {
private List<Rezervare> rezervari;
private Context context;
private LayoutInflater inflater;
private String numeUtilizator;
private String status;
private Rezervare rezervare = new Rezervare();
private DatabaseReference reff;
public RecyclerViewAdapterRezervare(List<Rezervare> rezervari, Context context, String numeUtilizator) {
this.rezervari = rezervari;
this.context = context;
this.numeUtilizator = numeUtilizator;
this.inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public RecycleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rezervari_layout, parent, false);
return new RecycleViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecycleViewHolder holder, int position) {
rezervare = rezervari.get(position);
reff = FirebaseDatabase.getInstance().getReference();
List<String> listaOre = rezervare.getOre();
Map<String, Object> mapOre = new HashMap<>();
for(String ora : listaOre){
mapOre.put(ora, false);
}
// this will return the status ("activa" is the booking is active, or "anulata" is it's cancelled)
getStatus(rezervare, new StatusCallback() {
#Override
public void onCallback(String value) {
status = value;
holder.btn_anuleaza.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("status", status);
new AlertDialog.Builder(view.getContext()).setTitle("Anulare rezervare").setMessage("Sunteti sigur ca doriti anularea rezervarii?")
.setPositiveButton(R.string.da, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// if the cancel button is clicked, i update my database so the status of the booking will be "anulata" - cancelled reff.child("Users").child(numeUtilizator).child("rezervari").child(rezervare.getData()).child(rezervare.getNumeTeren()).child("status").setValue("anulata");
reff.child("Rezervari").child(rezervare.getData()).child("Fotbal").child(rezervare.getNumeTeren()).updateChildren(mapOre);
rezervare.setEsteAnulata(false);
Toast.makeText(view.getContext(), "Anulare cu succes!", Toast.LENGTH_LONG).show();
}
}).setNegativeButton(R.string.nu, null).show();
}
});
}
});
}
#Override
public int getItemCount() {
return rezervari.size();
}
// i have updated my code with the delete and insert a booking
public void stergeRezervare(int position){
rezervari.remove(position);
notifyDataSetChanged();
}
public void adaugaRezervare(Rezervare rezervare){
rezervari.add(rezervare);
notifyDataSetChanged();
}
This is the function that gets me the bookings from firebase
i have 3 separate lists for active bookings, cancelled and past bookings.
public interface RezervariListCallback {
void onCallback(List<Rezervare> rezervariActuale, List<Rezervare> rezervariTrecute, List<Rezervare> rezervariAnulate);
}
public void getRezervari(String nume, RezervariListCallback myCallback){
reff.child("Users").child(nume).child("rezervari").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot data : snapshot.getChildren()){
String dataRezervare = data.getKey();
dateRezervari.add(dataRezervare);
reff.child("Users").child(nume).child("rezervari").child(dataRezervare).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot data : snapshot.getChildren()){
String numeTeren = data.getKey();
reff.child("Users").child(nume).child("rezervari").child(dataRezervare).child(numeTeren).child("ore").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
List<String> ore = new ArrayList<>();
for(DataSnapshot data : snapshot.getChildren()){
String ora = data.getKey();
ore.add(ora);
}
getAdresa(nume, dataRezervare, numeTeren, new AdresaCallback() {
#Override
public void onCallbackAdresa(String value) {
Rezervare rezervare = new Rezervare(numeTeren, ore, dataRezervare, value);
String dataAzi = getDataAzi();
reff.child("Users").child(nume).child("rezervari").child(dataRezervare).child(numeTeren).child("status").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String status = (String) snapshot.getValue();
if(comparaData(dataAzi, rezervare.getData())){
if(status.equals("activa")){
rezervariActive.add(rezervare);
} else {
rezervariAnulate.add(rezervare);
}
} else {
rezervariTrecute.add(rezervare);
}
myCallback.onCallback(rezervariActive, rezervariTrecute, rezervariAnulate);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
and this is where i set the adapters
public void updateRezervari(View view, RecyclerView activeBookingsRecycler, RecyclerView pastBookingsRecycler, RecyclerView cancelledBookingsRecycler){
AsyncTask.execute(new Runnable() {
#Override
public void run() {
getRezervari(numeUtilizator, new RezervariListCallback() {
#Override
public void onCallback(List<Rezervare> rezervariActuale, List<Rezervare> rezervariTrecute, List<Rezervare> rezervariAnulate) {
if(rezervariActuale.size()>0){
tvFaraRezervariActive.setEnabled(false);
tvFaraRezervariAnulate.setEnabled(false);
tvFaraRezervariTrecute.setEnabled(false);
}
Log.v("rezAc", rezervariActuale.toString());
Log.v("rezTr", rezervariTrecute.toString());
RecyclerViewAdapterRezervare adapter = new RecyclerViewAdapterRezervare(rezervariActuale, view.getContext(), numeUtilizator);
activeBookingsRecycler.setAdapter(adapter);
adapter = new RecyclerViewAdapterRezervare(rezervariTrecute, view.getContext(), numeUtilizator);
pastBookingsRecycler.setAdapter(adapter);
adapter = new RecyclerViewAdapterRezervare(rezervariAnulate, view.getContext(), numeUtilizator);
cancelledBookingsRecycler.setAdapter(adapter);
}
});
}
});
}
Can you give me a few tips on how to move from one recycler view to another? Thank you!
Since you are using Firebase to fetch status of a booking, you can update the status to cancelled in your firebase database and then you will get a callback from your firebase listener where you can update your arraylists and call notifyDataSetChanged().

Recycler Adapter item bug

I'm trying to make a comment page like instagram but I'm getting a ridiculous error;
Every comment row has one recyclerview and user click the "show replies" button I make Recyclerview visible but After 10-11 items, recyclerview of that item is also visible.
I know my English is terrible by mistake but i need help.
holder.replyCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(main,"sdf",Toast.LENGTH_LONG).show();
new Comment().getCommentResponse(getCommentModels.get(position).getId(), new IMainResponse() {
#Override
public <T> void Succsess(Response<T> _response) {
getCommentModel = (List<getCommentModel>) _response.body();
AdapterComment adapterComment = new AdapterComment(main, getCommentModel);
holder.recyclerView.setVisibility(View.VISIBLE);
holder.recyclerView.setAdapter(adapterComment);
holder.recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(main);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
holder.recyclerView.setLayoutManager(linearLayoutManager);
}
#Override
public void Error(ErrorModel _eresponse) {
}
});
}
});
Create a boolean in your model class to keep track of visibility
if (getCommentModels.get(position).isVisible()) {
holder.recyclerView.setVisibility(View.VISIBLE);
} else {
holder.recyclerView.setVisibility(View.GONE);
}
holder.replyCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getCommentModels.get(position).setVisible(!getCommentModels.get(position).isVisible());
notifyItemChanged(position);
new Comment().getCommentResponse(getCommentModels.get(position).getId(), new IMainResponse() {
#Override
public <T> void Succsess(Response<T> _response) {
getCommentModel = (List<getCommentModel>) _response.body();
AdapterComment adapterComment = new AdapterComment(main, getCommentModel);
holder.recyclerView.setAdapter(adapterComment);
holder.recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(main);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
holder.recyclerView.setLayoutManager(linearLayoutManager);
}
#Override
public void Error(ErrorModel _eresponse) {
}
});
}
});

SwipeRefreshLayout Refresh Listener not calling onRefresh()

I currently have a SwipeRefreshLayout that isn't calling its OnRefresh method whenever I pull down. Is something wrong with my code?
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipe_view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe_view = (SwipeRefreshLayout) findViewById(R.id.this_will_work);
swipe_view.setOnRefreshListener(new OnRefreshListener()
{
#Override
public void onRefresh()
{
Toast.makeText(getApplicationContext(), "OnRefresh() test", Toast.LENGTH_SHORT).show();
}
});
You should have a service or something that will be updated like below :
private void refresh() {
startService(new Intent(this, UpdaterService.class));
}
then Refresh :
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
refresh();
}});
//here refreshed Item
//getLoaderManager().initLoader(0, null, this);
if (savedInstanceState == null) {
refresh();
}
and your onRefresh() should contain the same method refresh().

Android why do my adfonic interstitials appear for only a second then dissapear

So as question suggests im trying to get full screen ads working, and i have it so they do appear but they only stay there for 4 seconds or so but then dissapear
what am i not doing/ doing wrong, i cant seem to find any documentation from adfonic and all of admobs seems too different to be of any help.
public class AdPage extends Activity implements OnClickListener {
Button Close;
private final static String TAG = "Testing";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_layout);
final AdfonicView adView = (AdfonicView) findViewById(R.id.adfonicView);
adView.setAdListener(new AdListener() {
#Override
public void onReceivedAd() {
Log.d(TAG, "AdListener onReceivedAd()");
adView.showInterstitial();
}
#Override
public void onPresentScreen() {
Log.d(TAG, "AdListener onPresentScreen()");
}
#Override
public void onNoFill() {
Log.d(TAG, "AdListener onNoFill()");
}
#Override
public void onNetworkError() {
Log.d(TAG, "AdListener onNetworkError()");
}
#Override
public void onLeaveApplication() {
Log.d(TAG, "AdListener onLeaveApplication()");
}
#Override
public void onInvalidRequest() {
Log.d(TAG, "AdListener onInvalidRequest()");
}
#Override
public void onInternalError() {
Log.d(TAG, "AdListener onInternalError()");
}
#Override
public void onDismissScreen() {
Log.d(TAG, "AdListener onDismissScreen()");
}
#Override
public void onClick() {
Log.d(TAG, "AdListener onClick()");
}
});
This is the layout
<com.adfonic.android.AdfonicView
xmlns:adfonic="http://schemas.android.com/apk/lib/com.adfonic.android"
android:id="#+id/adfonicView"
android:layout_width="match_parent"
android:layout_height="match_parent"
adfonic:adfonic_adslot_id="######"
adfonic:language="en"
adfonic:refresh_ad="true"
adfonic:refresh_time="30" />
http://developer.adfonic.com/index.php/Android_SDK
With Adfonic at the moment for interstitial ads the timeout is 4 seconds.
I believe in the next update of the SDK they are adding the capability to setting the timeout for your interstitial ads.

Categories