在回收站视图中,我想在滚动回收站视图时更新数据。 但是这样做,Recycler视图不会滚动。我的代码是 请给我解决方案,以便我可以继续完成我的项目
package com.piyushjaiswal.lyricswala;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.widget.SearchView;
import android.widget.Toast;
import com.airbnb.lottie.LottieAnimationView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class Hindi extends Fragment {
private List<Contact> listContact = new ArrayList<>();
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference();
private RecyclerViewAdapter recyclerViewAdapter;
private LottieAnimationView progressBar;
private SearchView searchView;
private RecyclerView myRecyclerview;
private final int ITEM_LOAD_COUNT = 5;
private int total_item = 0,last_visible_item;
private boolean isLoading = false, isMaxData = false;
private String last_node="",last_key="";
public Hindi() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hindi, container, false);
progressBar= v.findViewById(R.id.progressbar);
searchView = v.findViewById(R.id.searchViewinHindi);
progressBar.setVisibility(View.VISIBLE);
myRecyclerview = v.findViewById(R.id.recyclerView);
getLastKeyFromFirebase();
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
myRecyclerview.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(myRecyclerview.getContext(),layoutManager.getOrientation());
myRecyclerview.addItemDecoration(dividerItemDecoration);
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getContext(),listContact);
myRecyclerview.setAdapter(recyclerViewAdapter);
getUsers();
myRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
total_item = layoutManager.getItemCount();
last_visible_item = layoutManager.findLastVisibleItemPosition();
if(!isLoading && total_item <= ((last_visible_item +ITEM_LOAD_COUNT))){
getUsers();
isLoading=true;
} }
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
return v;
}private void getUsers() {
if(!isMaxData){
Query query;
if(TextUtils.isEmpty(last_node))
query = FirebaseDatabase.getInstance().getReference().child("Hindi").orderByKey().limitToFirst(ITEM_LOAD_COUNT);
else
query = FirebaseDatabase.getInstance().getReference().child("Hindi").orderByKey().startAt(last_node).limitToFirst(ITEM_LOAD_COUNT);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChildren()){
List<Contact> newContact = new ArrayList<>();
for(DataSnapshot userSnapshot: dataSnapshot.getChildren()){
newContact.add(userSnapshot.getValue(Contact.class));
}
searchView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
last_node = newContact.get(newContact.size()-1).getName();
if(!last_node.equals(last_key))
newContact.remove(newContact.size()-1);
else
last_node = "end";
RecyclerViewAdapter mRecycleradapter = new RecyclerViewAdapter(getContext(),newContact);
myRecyclerview.setAdapter(mRecycleradapter);
isLoading = false;
}
else
{
isLoading = false;
isMaxData = true;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
isLoading = false;
}
});
}
}
private void getLastKeyFromFirebase()
{
Query getLastKey = FirebaseDatabase.getInstance().getReference().child("Hindi").orderByKey().limitToLast(1);
getLastKey.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
last_key = dataSnapshot1.getKey();
}
} @Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(),databaseError.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
private void search(String newText) {
ArrayList<Contact> myList = new ArrayList<>();
for (Contact object : listContact) {
if(object.getName().toLowerCase().contains(newText.toLowerCase()))
{
myList.add(object);
}
}
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getContext(),myList);
myRecyclerview.setAdapter(recyclerViewAdapter);
}
}
运行我的应用程序时,它仅显示4个项目,但在Firebase数据库中创建了7个节点。