Android: Implementing Progressbar and "Loading..." for Endless List Like Android Market

Endless RecyclerView with ProgressBar for pagination

HERE IS SIMPLER AND CLEANER APPROACH.

Implement Endless Scrolling from this Codepath Guide and then follow the following steps.

1. Add progress bar under the RecyclerView.

    <android.support.v7.widget.RecyclerView
android:id="@+id/rv_movie_grid"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingBottom="50dp"
android:clipToPadding="false"
android:background="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

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

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

Here android:paddingBottom="50dp" and android:clipToPadding="false" are very important.

2. Get a reference to the progress bar.

progressBar = findViewById(R.id.progressBar);

3. Define methods to show and hide progress bar.

void showProgressView() {
progressBar.setVisibility(View.VISIBLE);
}

void hideProgressView() {
progressBar.setVisibility(View.INVISIBLE);
}

Android Endless List

One solution is to implement an OnScrollListener and make changes (like adding items, etc.) to the ListAdapter at a convenient state in its onScroll method.

The following ListActivity shows a list of integers, starting with 40, adding items when the user scrolls to the end of the list.

public class Test extends ListActivity implements OnScrollListener {

Aleph0 adapter = new Aleph0();

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(adapter);
getListView().setOnScrollListener(this);
}

public void onScroll(AbsListView view,
int firstVisible, int visibleCount, int totalCount) {

boolean loadMore = /* maybe add a padding */
firstVisible + visibleCount >= totalCount;

if(loadMore) {
adapter.count += visibleCount; // or any other amount
adapter.notifyDataSetChanged();
}
}

public void onScrollStateChanged(AbsListView v, int s) { }

class Aleph0 extends BaseAdapter {
int count = 40; /* starting amount */

public int getCount() { return count; }
public Object getItem(int pos) { return pos; }
public long getItemId(int pos) { return pos; }

public View getView(int pos, View v, ViewGroup p) {
TextView view = new TextView(Test.this);
view.setText("entry " + pos);
return view;
}
}
}

You should obviously use separate threads for long running actions (like loading web-data) and might want to indicate progress in the last list item (like the market or gmail apps do).

Android listview lazy loading

You can achieve this by using endless Adapter implementation. This exactly does what you want. You can also restrict the number of rows to be refreshed per scroll. Here is a link to it.,

Android: Implementing progressbar and "loading..." for Endless List like Android Market

https://github.com/commonsguy/cwac-endless

To use it, you extend EndlessAdapter to provide details about how to handle the endlessness. Specifically, you need to be able to provide a row View, independent from any of the rows in your actual adapter, that will serve as a placeholder while you, in another method, load in the actual data to your main adapter. Then, with a little help from you, it seamlessly transitions in the new data.

Endless scrolling for listview

Found the solution by declaring my list as a global variable. Thanks anyway for your help.

Endless scrolling listview not working

To Show only 10 item first time in ListView and show more items on scroll follow following steps:

STEP 1: Create a chunkList method which divide ArrayList in parts of size 10:

static <T> List<ArrayList<T>> chunkList(List<T> list, final int L) {
List<ArrayList<T>> parts = new ArrayList<ArrayList<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}

STEP 2: Create ArrayList and a counter which show current part :

    private boolean isLoadingMore=false;
Handler mHandler = new Handler();
List<ArrayList<HashMap<String,String>>> mainArrayList;
private int count=0;

STEP 3: In onPostExecute break result and show data in ListView:

protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

super.onPostExecute(result);
// your code here...
mainArrayList=chunkList(result,10);
isLoadingMore=false;
count=0;
adapter = new CustomAdapterSent(Interestsent.this,
mainArrayList.get(count));
setListAdapter(adapter);
}

}

STEP 4: Create addAll method in Adapter to append data in current data source :

public void addAll(ArrayList<HashMap<String,String>> moreData){
this.listData.addAll(moreData);
this.notifyDataSetChanged();
}

STEP 5: In onLoadMore load more data in ListView:

mHandler = new Handler();
listview.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
if(count<mainArrayList.size()-1)
{
if(adapter !=null){
count++;
if(!isLoadingMore){
isLoadingMore=true;
mHandler.postDelayed(loadMoreRunnable,1000);
}
}
}
}
});
}

Runnable loadMoreRunnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
if(count<mainArrayList.size())
{
if(adapter !=null){
count++;
adapter.addAll(mainArrayList.get(count));
mHandler.removeCallbacks(loadMoreRunnable);
isLoadingMore=false;
}
}
}
};

Show ProgressBar till EditText content changes

Hide the Progressbar when the EditText fires the corresponding textchanged event:

EditText editText = (EditText)findViewById(R.id.editText);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar);
editText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
progressbar.setVisibility(View.GONE);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});

Better: Do not hide the ProgressBar when the EditText changes, but when your Bluetooth event is fired.

Recommendation/Advice for android application structure

You should watch Reto Meier's Google IO 2012 talk - https://www.youtube.com/watch?v=PwC1OlJo5VM.

In it he talks about mobile radios and explains why its better for battery life to fetch all your data at once. It's a great talk and worth while.

Unless you have crazy amounts of data in your list views, memory shouldn't be much of a problem.



Related Topics



Leave a reply



Submit