Skip to content

Commit

Permalink
logic update & new xml attrs added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugurcan Yildirim committed May 25, 2016
1 parent e2dce38 commit fb18ddb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# InfiniteListView [![JitPack](https://jitpack.io/v/ugurcany/InfiniteListView.svg)](https://jitpack.io/#ugurcany/InfiniteListView) [![API](https://img.shields.io/badge/API-11%2B-red.svg?style=flat)](https://android-arsenal.com/api?level=11) [![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)
# InfiniteListView [![JitPack](https://jitpack.io/v/ugurcany/InfiniteListView.svg)](https://jitpack.io/#ugurcany/InfiniteListView)

**InfiniteListView** is a custom Android ListView that gets extended at each time new items are loaded by swiping to the bottom of list. It also supports swipe-to-refresh behavior.

Expand Down Expand Up @@ -30,13 +30,15 @@ See the `app` module for the sample usage of **InfiniteListView** and **Infinite

- Includes the following methods:
- `infiniteListView.addNewItem(item);` -> adds new item to list
- `infiniteListView.clearList();` -> clears entire list (and triggers `onNewLoadRequired()`)
- `infiniteListView.clearList();` -> clears entire list
- `infiniteListView.startLoading();` -> call this before item loading starts
- `infiniteListView.stopLoading();` -> call this after item loading ends
- `infiniteListView.setEndOfLoading();` -> call this when there is no more item to load
- `infiniteListView.setEndOfLoading(isEndOfLoading);` -> call this to let the view know whether there is more to load or not

- Custom XML attributes:
- `swipeRefreshIndicatorColor` (*color*)
- `scrollbarVisible` (*boolean*)
- `dividerVisible` (*boolean*)



Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {

defaultConfig {
applicationId "com.izmyr.infinitelistview"
minSdkVersion 11
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.izmyr.infinitelistview.MainActivity">
<activity
android:name="com.izmyr.infinitelistview.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/izmyr/infinitelistview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {

infiniteListView.init(adapter, loadingView);

refreshList();
loadNewItems();
}

//SIMULATES ITEM LOADING
Expand Down Expand Up @@ -74,6 +74,8 @@ protected void onPostExecute(Void param) {
}
itemOffset += ITEM_COUNT_TO_LOAD;
Log.d("InfiniteListView", "Current item count = " + itemOffset);

infiniteListView.setEndOfLoading(false);
}

infiniteListView.stopLoading();
Expand All @@ -84,12 +86,12 @@ protected void onPostExecute(Void param) {
//DO THIS ON SWIPE-REFRESH
public void refreshList() {
itemOffset = 0;
infiniteListView.setEndOfLoading(false);
infiniteListView.clearList(); //TRIGGERS ONNEWLOADREQUIRED
infiniteListView.clearList();
loadNewItems();
}

//DO THIS ON ITEM CLICK
public void clickItem(final int position) {
public void clickItem(int position) {
Snackbar.make(container, "Item clicked: " + position, Snackbar.LENGTH_SHORT).show();
}

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
android:id="@+id/infiniteListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:swipeRefreshIndicatorColor="@color/colorAccent" />
app:swipeRefreshIndicatorColor="@color/colorAccent"
app:scrollbarVisible="true"
app:dividerVisible="true" />

</LinearLayout>
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 11
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
Expand Down
21 changes: 16 additions & 5 deletions library/src/main/java/com/izmyr/views/InfiniteListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class InfiniteListView<T> extends FrameLayout {
private boolean loading = false;
private View loadingView;

private boolean endOfLoading = true;
private boolean isEndOfLoading = true;

private InfiniteListAdapter infiniteListAdapter;

Expand Down Expand Up @@ -54,6 +54,7 @@ public void onRefresh() {
});

listView = (ListView) view.findViewById(R.id.listView);
listView.setFooterDividersEnabled(false);

//XML CONFIG
if(attrs != null) {
Expand All @@ -64,6 +65,16 @@ public void onRefresh() {
int swipeRefreshIndicatorColor = typedArray.getColor(R.styleable.InfiniteListView_swipeRefreshIndicatorColor, context.getResources().getColor(android.R.color.black));
swipeRefreshLayout.setColorSchemeColors(swipeRefreshIndicatorColor);

//SCROLLBAR VISIBILITY
boolean scrollbarVisible = typedArray.getBoolean(R.styleable.InfiniteListView_scrollbarVisible, true);
listView.setVerticalScrollBarEnabled(scrollbarVisible);

//DIVIDER VISIBILITY
boolean dividerVisible = typedArray.getBoolean(R.styleable.InfiniteListView_dividerVisible, true);
if(!dividerVisible) {
listView.setDividerHeight(0);
}

} finally {
typedArray.recycle();
}
Expand All @@ -75,7 +86,6 @@ public void init(InfiniteListAdapter<T> infiniteListAdapter, final View loadingV

this.infiniteListAdapter = infiniteListAdapter;
listView.setAdapter(infiniteListAdapter);
listView.setFooterDividersEnabled(false);

this.loadingView = loadingView;

Expand All @@ -86,7 +96,7 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(endOfLoading)
if(isEndOfLoading)
return;

int lastVisibleItem = visibleItemCount + firstVisibleItem;
Expand Down Expand Up @@ -124,6 +134,7 @@ public void addNewItem(T newItem){
}

public void clearList(){
isEndOfLoading = true;
infiniteListAdapter.clearList();
}

Expand All @@ -142,8 +153,8 @@ public void stopLoading(){
loading = false;
}

public void setEndOfLoading(boolean endOfLoading) {
this.endOfLoading = endOfLoading;
public void setEndOfLoading(boolean isEndOfLoading) {
this.isEndOfLoading = isEndOfLoading;
}

}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<resources>
<declare-styleable name="InfiniteListView">
<attr name="swipeRefreshIndicatorColor" format="color"/>
<attr name="scrollbarVisible" format="boolean"/>
<attr name="dividerVisible" format="boolean"/>
</declare-styleable>
</resources>

0 comments on commit fb18ddb

Please sign in to comment.