我试图在android中实现recylerview无尽的listview。一切正常。在滚动侦听器的处理程序中再次调用我的asynctask,这是另一个线程并且睡了一段时间,但是我不想在处理程序中调用任何调用,如果在那种情况下我无法在底部看到我的进度条加载非常快,不可见。我怎么能实现这一点,请建议我。
这是我的主要activity.java
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
Activity _activity;
private String jsonResult;
boolean loadingMore = false;
ProgressDialog progress;
private TextView tvEmptyView;
private RecyclerView mRecyclerView;
private DataAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
private List<Student> dataArrayList;
private LoadJsonData loadJsonData;
protected Handler handler;
private int count;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
progress = new ProgressDialog(this);
_activity = this;
loadJsonData = new LoadJsonData();
tvEmptyView = (TextView) findViewById(R.id.empty_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
dataArrayList = new ArrayList<Student>();
handler = new Handler();
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Endless Scrollling data ");
}
// loadData();
if (dataArrayList.isEmpty()) {
loadJsonData.execute();
}
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
// use a linear layout manager
mRecyclerView.setLayoutManager(mLayoutManager);
// create an Object for Adapter
mAdapter = new DataAdapter(dataArrayList, mRecyclerView);
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
// mAdapter.notifyDataSetChanged();
if (dataArrayList.isEmpty() && count > 0) {
mRecyclerView.setVisibility(View.GONE);
tvEmptyView.setVisibility(View.VISIBLE);
} else {
mRecyclerView.setVisibility(View.VISIBLE);
tvEmptyView.setVisibility(View.GONE);
}
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
// //add null , so the adapter will check view_type and show progress bar at bottom
dataArrayList.add(null);
mAdapter.notifyItemInserted(dataArrayList.size());
//
handler.postDelayed(new Runnable() {
@Override
public void run() {
// // remove progress item
// studentList.remove(studentList.size() - 1);
// mAdapter.notifyItemRemoved(studentList.size());
// //add items one by one
// int start = studentList.size();
// int end = start + 20;
new LoadJsonData().execute();
// for (int i = start + 1; i <= end; i++) {
// studentList.add(new Student("Student " + i, "AndroidStudent" + i + "@gmail.com"));
// mAdapter.notifyItemInserted(studentList.size());
// }
// mAdapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 1000);
}
});
}
private class LoadJsonData extends AsyncTask<Void, Void, String> {
// ProgressDialog progress;
@Override
protected void onPreExecute() {
super.onPreExecute();
if (dataArrayList.size() == 0) {
progress = ProgressDialog.show(_activity, "Progress",
"Please wait", true);
} else {
dataArrayList.remove(dataArrayList.size() - 1);
// mAdapter.notifyItemRemoved(dataArrayList.size()-);
}
}
@Override
protected String doInBackground(Void... params) {
String jsonResult = "";
loadingMore = true;
// lv.removeFooterView(footerView);
// jsonResult=get("https://datatables.net/examples/server_side/scripts/server_processing.php");
// return jsonResult;
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet("https://datatables.net/examples/server_side/scripts/server_processing.php");
HttpResponse httpResponse = httpclient.execute(request);
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null) {
jsonResult = convertInputStreamToString(inputStream);
} else {
jsonResult = "Did not work!";
}
} catch (Exception e) {
Log.e(TAG, "GET failed", e);
}
return jsonResult;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progress.dismiss();
try {
JSONObject dataJsontObject = new JSONObject(result);
JSONArray dataJsonArray = dataJsontObject.getJSONArray("data");
for (int i = 0; i < dataJsonArray.length(); i++) {
JSONArray dataSubArray = dataJsonArray.getJSONArray(i);
Student dataObject = new Student();
dataObject.setName((String) dataSubArray.get(0));
dataObject.setType((String) dataSubArray.get(1));
dataObject.setProfession((String) dataSubArray.get(2));
dataObject.setCountry((String) dataSubArray.get(3));
dataObject.setCurrency((String) dataSubArray.get(5));
// handler.postDelayed(new Runnable() {
// @Override
// public void run() {
// // remove progress item
// studentList.remove(studentList.size() - 1);
// mAdapter.notifyItemRemoved(studentList.size());
// //add items one by one
// int start = studentList.size();
// int end = start + 20;
//
// for (int i = start + 1; i <= end; i++) {
// studentList.add(new Student("Student " + i, "AndroidStudent" + i + "@gmail.com"));
// mAdapter.notifyItemInserted(studentList.size());
// }
// mAdapter.setLoaded();
// //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
// }
// }, 2000);
// lv.addFooterView(footerView);
dataArrayList.add(dataObject);
mAdapter.notifyItemInserted(dataArrayList.size());
}
count++;
mAdapter.setLoaded();
// mAdapter.notifyDataSetChanged();
loadingMore = false;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
// load initial data
// private void loadData() {
//
//// for (int i = 1; i <= 20; i++) {
//// studentList.add(new Student("Student " + i, "androidstudent" + i + "@gmail.com"));
////
//// }
// loadJsonData.execute();
//
//
// }
public static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
inputStream.close();
return result;
}
}
DataAdapter.java
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class DataAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<Student> studentList;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
public DataAdapter(List<Student> students, RecyclerView recyclerView) {
studentList = students;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
.getLayoutManager();
recyclerView
.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
}
@Override
public int getItemViewType(int position) {
return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
RecyclerView.ViewHolder vh;
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_row, parent, false);
vh = new StudentViewHolder(v);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.progress_item, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {
Student singleStudent = (Student) studentList.get(position);
((StudentViewHolder) holder).name.setText(singleStudent.getName());
((StudentViewHolder) holder).type.setText(singleStudent.getType());
((StudentViewHolder) holder).profession.setText(singleStudent.getProfession());
((StudentViewHolder) holder).country.setText(singleStudent.getCountry());
((StudentViewHolder) holder).currency.setText(singleStudent.getCurrency());
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return studentList.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
//
public static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView type;
public TextView profession;
public TextView country;
public TextView currency;
public StudentViewHolder(View convertView) {
super(convertView);
name = (TextView) convertView.findViewById(R.id.name);
type = (TextView) convertView.findViewById(R.id.type);
profession = (TextView) convertView.findViewById(R.id.profession);
country = (TextView) convertView.findViewById(R.id.country);
currency = (TextView) convertView.findViewById(R.id.currecy);
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
答案 0 :(得分:0)
您好,您可以使用此代码:
完整源代码:github.com
<jsp:useBean id="bc" class="com.go.Credentials" scope="session"/>
<%
out.println( bc.getUserid());
%>