我正在使用mysql数据库来存储数据库,并通过使用API使用凌空来从数据库中获取数据。
自3天以来,我一直遇到问题。 向下滚动到回收站视图的最后一个位置后,我想在应用程序的开始处显示前10个(阈值)数据。我想从服务器中获取剩余数据,但是我的应用程序无法添加最后添加的条目,因此效果很好当我关闭我的应用程序并再次启动它时。 请通过这个指导我
我尝试刷新列表,但是没有用
MainActivity.java文件
public class MainActivity extends AppCompatActivity {
String url = "https://yourjobcareers.in/softwear/API/fetch_admit_card.php";
RecyclerView recyclerView;
List<ModelClass> modelClassList;
ProgressBar progressBar;
static Boolean isScrolling = false;
RequestQueue requestQueue;
Adapter adapter;
LinearLayoutManager LayoutManager;
SwipeRefreshLayout swipeRefreshLayout;
JsonObjectRequest request;
JSONArray jsonArray;
int len = 0;
int myThreshold = 10, initial = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = findViewById(R.id.refresh);
progressBar = findViewById(R.id.progs);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
LayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(LayoutManager);
progressBar.setVisibility(View.VISIBLE);
requestQueue = Volley.newRequestQueue(MainActivity.this);
loaddata(url, initial, myThreshold);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView1, int newState) {
super.onScrollStateChanged(recyclerView1, newState);
isScrolling = true;
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
jsonArray = response.getJSONArray("user_data");
len = jsonArray.length();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
requestQueue.getCache().clear();
//Toast.makeText(MainActivity.this, "" + len, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView1, int dx, int dy) {
super.onScrolled(recyclerView1, dx, dy);
final int total = LayoutManager.getItemCount();
final int current = LayoutManager.getChildCount();
final int scrolled = LayoutManager.findFirstVisibleItemPosition();
if (isScrolling && (current + scrolled == total) && (len > total)) {
//isScrolling = false;
Toast.makeText(MainActivity.this, "if...."+jsonArray.length(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.VISIBLE);
isScrolling = false;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, " add: " + len + "size: " + modelClassList.get(modelClassList.size()-1).title, Toast.LENGTH_SHORT).show();
initial = myThreshold;
myThreshold += 10;
if (len < myThreshold) {
newData(url, initial, len);
}
else {
newData(url, initial, myThreshold);
}
adapter.notifyDataSetChanged();
//newData(url, len);
progressBar.setVisibility(View.GONE);
LayoutManager.scrollToPosition(total);
}
},3000);
} else if ((len == total) && (isScrolling) && (current+scrolled == total)){
progressBar.setVisibility(View.VISIBLE);
isScrolling = false;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "No new data added!!!" + modelClassList.size(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
},3000);
}
else {
isScrolling = false;
}
}
});
}
public void loaddata(String url, final int initial, final int threshold) {
modelClassList = new ArrayList<>();
request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
jsonArray = response.getJSONArray("user_data");
//Toast.makeText(MainActivity.this, "Total: " + jsonArray.length() + "",Toast.LENGTH_SHORT).show();
if (jsonArray.length() < threshold) {
for (int i = 0; i < jsonArray.length();i++){
JSONObject User_data = jsonArray.getJSONObject(i);
final String id = User_data.getString("id");
final String name = User_data.getString("title");
modelClassList.add(new ModelClass(id, name));
progressBar.setVisibility(View.GONE);
}
}
else {
for (int i = initial; i < threshold; i++) {
JSONObject User_data = jsonArray.getJSONObject(i);
final String id = User_data.getString("id");
final String name = User_data.getString("title");
modelClassList.add(new ModelClass(id, name));
progressBar.setVisibility(View.GONE);
}
}
//Collections.reverse(modelClassList);
adapter = new Adapter(modelClassList,MainActivity.this);
recyclerView.setAdapter(adapter);
//adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
requestQueue.getCache().clear();
}
private void newData(String url, final int initial, final int end) {
request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
jsonArray = response.getJSONArray("user_data");
for (int i = initial; i < end;i++){
JSONObject User_data = jsonArray.getJSONObject(i);
final String id = User_data.getString("id");
final String name = User_data.getString("title");
modelClassList.add(new ModelClass(id, name));
}
adapter = new Adapter(modelClassList,MainActivity.this);
recyclerView.setAdapter(adapter);
//adapter.notifyDataSetChanged();
//Toast.makeText(MainActivity.this, "" + modelClassList.get(modelClassList.size()-1).id, Toast.LENGTH_SHORT).show();
//adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
requestQueue.getCache().clear();
}
}
Adapter.java文件
public class Adapter extends RecyclerView.Adapter<Adapter.Viewholder> {
public List<ModelClass> modelClassList;
Context context;
public Adapter(List<ModelClass> modelClassList, Context context) {
this.modelClassList = modelClassList;
this.context = context;
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull final Viewholder viewholder, int position) {
//final ModelClass modelClass = modelClassList.get(getItemCount() - position -1);
viewholder.textView.setText(modelClassList.get(position).getTitle());
viewholder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,details.class);
int pos = viewholder.getLayoutPosition();
intent.putExtra("KEY", pos);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return modelClassList.size();
}
class Viewholder extends RecyclerView.ViewHolder{
TextView textView;
public Viewholder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}
ModelClass.java
class ModelClass {
String id;
String title;
ModelClass(String id, String title) {
this.id = id;
this.title = title;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
android:id="@+id/progs"/>
</RelativeLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="20sp"
android:text="msdmkmksmks"
android:padding="30dp"/>
</android.support.v7.widget.CardView>
</LinearLayout>
预期结果:
如果jsonarray的长度为10或以上,则每次搜索有10条结果。
如果json的长度小于10,则应显示所有数据
即使用户在应用中,我也需要更新数据,并将回收站视图滚动到上次查看的位置
实际结果:
它工作正常,只有在我关闭应用并重新启动后,它才根据需要获取数据