我希望有一个recyclerview来加载更多关于滚动的数据。它与加载更多数据完美配合。然后我扩展代码以显示最后的进度条以显示加载。但它不能正常工作。它一次显示两个进度条,而且我的加载数据也不合适。在这里,我实现了我的代码。请帮帮我。
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MyAdapter adapter;
final ArrayList<String > data = new ArrayList<>();
int end=20;
int start=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager lm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(lm);
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);
load_data();
recyclerView.setOnScrollListener(new EndLessScroll(lm) {
@Override
protected void loadMore(int current_page) {
start = end + 1;
end = end + 10;
load_data();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private void load_data() {
Handler handler = new Handler();
data.add(null);
adapter.notifyItemInserted(data.size()-1);
handler.postDelayed(new Runnable() {
@Override
public void run() {
data.remove(data.size() - 1);
adapter.notifyItemRemoved(data.size());
//wait
for (int i = start; i <= end; i++) {
data.add("Item-" + i);
adapter.notifyItemInserted(data.size());
}
EndLessScroll.setLoaded();
}
},2000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyAdapter.Java
/**
* Created by nteam on 13/10/15.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<String> data;
final int VIEW_ITEM=1;
final int VIEW_PROG=0;
public MyAdapter(ArrayList<String> data){
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh=null;
if(viewType == VIEW_ITEM){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_card,parent,false);
vh = new DataViewHolder(view);
}else{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_indicator,parent,false);
vh = new ProgressViewHolder(view);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof DataViewHolder) {
((DataViewHolder) holder).title.setText(data.get(position));
}else{
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
@Override
public int getItemViewType(int position) {
return data.get(position)!=null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
public class DataViewHolder extends RecyclerView.ViewHolder{
public TextView title;
public DataViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
public class ProgressViewHolder extends RecyclerView.ViewHolder{
public ProgressBar progressBar;
public ProgressViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progress1);
}
}
}
EndlessScroll.java
/**
* Created by nteam on 13/10/15.
*/
public abstract class EndLessScroll extends RecyclerView.OnScrollListener {
public static String TAG = EndLessScroll.class.getSimpleName();
private int prevTotal = 0;
public static boolean isLoading = true;
private int visibleThresold = 10;
int firstVisisbleItem,visibleItemCount,totalItemCount;
private int current_page=1;
private LinearLayoutManager linearLayoutManager;
public EndLessScroll(LinearLayoutManager linearLayoutManager){
this.linearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
firstVisisbleItem = linearLayoutManager.findFirstVisibleItemPosition();
if(isLoading){
if(totalItemCount>prevTotal){
isLoading=false;
prevTotal = totalItemCount;
}
}else if(!isLoading && (totalItemCount-visibleItemCount)<= (firstVisisbleItem+visibleThresold)) {
//end reached
current_page++;
loadMore(current_page);
isLoading = true;
}
}
public static void setLoaded(){
isLoading=false;
}
protected abstract void loadMore(int current_page);
}
答案 0 :(得分:2)
我还没有实际运行代码,但至少这一行:
current_page++;
loadMore(current_page);
isLoading = true;
应该是:
isLoading = true;
current_page++;
loadMore(current_page);
当然,这必须弄乱进度条和项目,因为可以同时启动多个加载。