; paras[X - 1].parentNode.insertBefore(ad1, paras[X]); } if (paras.length > X + 4) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 3].parentNode.insertBefore(ad2, paras[X + 4]); } if (isMobile && paras.length > X + 8) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 7].parentNode.insertBefore(ad3, paras[X + 8]); } });

Advertisement

Showing posts with label RecyclerView OnScrollListener. Show all posts
Showing posts with label RecyclerView OnScrollListener. Show all posts

LoadMore RecyclerView with progress bar showing at bottom


Update to My Last Post.

I am trying to get Endless RecyclerView with progress bar showing at bottom when you are loading data from  web service. But faced different problems.
May be some one needs this.

My StackOverflow answer. Plesae upvote my answer. if it is helpful in your project.
http://stackoverflow.com/questions/31000964/how-to-implement-setonscrolllistener-in-recyclerview/31178493#31178493


I want to show complete example on this.


Step: 1
======
Create a new Interface


1
2
3
public interface OnLoadMoreListener {
void onLoadMore();
}

Step: 2
======
Create a new Model Object Name Student.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.pratap.endlessrecyclerview;

import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private String name;

private String emailId;

public Student() {

}
public Student(String name, String emailId) {
this.name = name;
this.emailId = emailId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}


}



Step: 3
======

Create a new Activity with Recyclerview in the Xml Layout.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.pratap.endlessrecyclerview;

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.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

private TextView tvEmptyView;
private RecyclerView mRecyclerView;
private DataAdapter mAdapter;
private LinearLayoutManager mLayoutManager;

private List<Student> studentList;


protected Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tvEmptyView = (TextView) findViewById(R.id.empty_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
studentList = new ArrayList<Student>();
handler = new Handler();
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Students");

}

loadData();

// 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(studentList, mRecyclerView);

// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
// mAdapter.notifyDataSetChanged();


if (studentList.isEmpty()) {
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
studentList.add(null);
mAdapter.notifyItemInserted(studentList.size() - 1);

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);

}
});

}


// load initial data
private void loadData() {

for (int i = 1; i <= 20; i++) {
studentList.add(new Student("Student " + i, "androidstudent" + i + "@gmail.com"));

}


}


}



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:scrollbars="vertical" />


<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No Records"
android:visibility="gone" />


</LinearLayout>



Create a row for recyclerview

list_row.xml



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground">

<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="18sp" />

<TextView
android:id="@+id/tvEmailId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_margin="5dp"
android:text="Email Id"
android:textColor="@android:color/black"
android:textSize="12sp" />
</RelativeLayout>

</android.support.v7.widget.CardView>


create a progress bar to show at the bottom.

progressbar_item.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
<?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:orientation="vertical" >

<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" />

</LinearLayout>


Step: 4
======

Create an Adapter Class for RecyclerView


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.pratap.endlessrecyclerview;

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).tvName.setText(singleStudent.getName());

((StudentViewHolder) holder).tvEmailId.setText(singleStudent.getEmailId());

((StudentViewHolder) holder).student= singleStudent;

} 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 tvName;

public TextView tvEmailId;

public Student student;

public StudentViewHolder(View v) {
super(v);
tvName = (TextView) v.findViewById(R.id.tvName);

tvEmailId = (TextView) v.findViewById(R.id.tvEmailId);

v.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"OnClick :" + student.getName() + " \n "+student.getEmailId(),
Toast.LENGTH_SHORT).show();

}
});
}
}

public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;

public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}



Source Code Link

Sample Apk Link 


ScreenShots
=========

 


Sample Demo
==========







Endless RecyclerView OnScrollListener Example in android


Note:

I have updated this post.Please see the updated post



If you have thousands of records in your database server, rather than getting all records loading at a time, try to load some x number of records in the onscroll event and update the ui,

Example:
If you have 1000 records in the server db, get 50 records first time, if the user reached to the last record in the ui, then again load 50 more records in the onscrolllistener event.

Step: 1
======
In this example, i am using following github code snippet for endless RecyclerView

Credit goes to: WoongBi Kim
https://gist.github.com/ssinss/e06f12ef66c51252563e

Step: 2
======

Now, Create an Activity with RecyclerView in the XML Layout file.

CardViewActivity.java


  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.pratap.cardviews1;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

public class CardViewActivity extends AppCompatActivity {

private Toolbar toolbar;

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;

private List<Student> studentList;

// on scroll

private static int current_page = 1;

private int ival = 1;
private int loadLimit = 10;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);

studentList = new ArrayList<Student>();

loadData(current_page);

if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Students");

}

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// 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 CardViewDataAdapter(studentList);

// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);

mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(
mLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// do somthing...

loadMoreData(current_page);

}

});

}
// By default, we add 10 objects for first time.
private void loadData(int current_page) {

// I have not used current page for showing demo, if u use a webservice
// then it is useful for every call request

for (int i = ival; i <= loadLimit; i++) {
Student st = new Student("Student " + i, "androidstudent" + i
+ "@gmail.com", false);

studentList.add(st);
ival++;

}

}
// adding 10 object creating dymically to arraylist and updating recyclerview when ever we reached last item
private void loadMoreData(int current_page) {

// I have not used current page for showing demo, if u use a webservice
// then it is useful for every call request

loadLimit = ival + 10;

for (int i = ival; i <= loadLimit; i++) {
Student st = new Student("Student " + i, "androidstudent" + i
+ "@gmail.com", false);

studentList.add(st);
ival++;
}

mAdapter.notifyDataSetChanged();

}

}

activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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="match_parent"
android:orientation="vertical" >

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:scrollbars="vertical" />

</LinearLayout>



Step: 3
======
EndlessRecyclerOnScrollListener.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.pratap.cardviews1;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public abstract class EndlessRecyclerOnScrollListener extends
RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
.getSimpleName();

private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 1;

private LinearLayoutManager mLinearLayoutManager;

public EndlessRecyclerOnScrollListener(
LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached

// Do something
current_page++;

onLoadMore(current_page);

loading = true;
}
}

public abstract void onLoadMore(int current_page);
}

Step: 4
======
Now create an adapter for the RecyclerView

CardViewDataAdapter.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.pratap.cardviews1;

import java.util.List;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CardViewDataAdapter extends
RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

private List<Student> stList;

public CardViewDataAdapter(List<Student> students) {
this.stList = students;

}

// Create new views
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_row, null);

// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

viewHolder.tvName.setText(stList.get(position).getName());

viewHolder.tvEmailId.setText(stList.get(position).getEmailId());

viewHolder.singlestudent=stList.get(position);

}

// Return the size arraylist
@Override
public int getItemCount() {
return stList.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView tvName;
public TextView tvEmailId;

public Student singlestudent;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);

tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
// Onclick event for the row to show the data in toast
itemLayoutView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(
v.getContext(),
"Data : \n" + singlestudent.getName() + " \n"
+ singlestudent.getEmailId(),
Toast.LENGTH_SHORT).show();

}
});

}

}

}

cardview_row.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >

<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="name"
android:textColor="@android:color/black"
android:textSize="18sp" />

<TextView
android:id="@+id/tvEmailId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvName"
android:text="email"
android:textColor="@android:color/black"
android:textSize="18sp" />
</RelativeLayout>

</android.support.v7.widget.CardView>


Student.java 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.pratap.cardviews1;

import java.io.Serializable;

public class Student implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

private String name;

private String emailId;

private boolean isSelected;

public Student() {

}

public Student(String name, String emailId) {

this.name = name;
this.emailId = emailId;

}

public Student(String name, String emailId, boolean isSelected) {

this.name = name;
this.emailId = emailId;
this.isSelected = isSelected;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public boolean isSelected() {
return isSelected;
}

public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}

}

ScreenShots:
=========


  


Source Code 
=========

Download Link

UPTET news