Last active
October 22, 2015 21:10
-
-
Save ekirastogi/e610eebbf6c1d658522d to your computer and use it in GitHub Desktop.
Android: Infinite scroll using list view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingLeft="5dp" | |
android:paddingRight="5dp" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/title_person_list" | |
android:text="Person List" | |
android:layout_width="match_parent" | |
android:layout_height="50dp" | |
android:gravity="center" | |
android:textSize="22dp"/> | |
<ListView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:divider="@android:color/transparent" | |
android:dividerHeight="5dp" | |
android:layout_below="@+id/title_person_list" | |
android:id="@+id/person_list" | |
/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
android:id="@+id/buzz_item_container" | |
android:layout_height="94dp" | |
android:layout_width="match_parent" | |
android:weightSum="1" | |
android:padding="5dp" | |
android:background="#f7f7f7" | |
android:layout_margin="2dp" | |
android:elevation="3dp" | |
android:orientation="horizontal" | |
android:divider="@android:color/holo_blue_bright" | |
xmlns:android="http://schemas.android.com/apk/res/android" > | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:orientation="horizontal" | |
android:padding="2dp" | |
> | |
<ImageView | |
android:layout_width="160dp" | |
android:layout_height="90dp" | |
android:background="@drawable/user" | |
/> | |
</LinearLayout> | |
<LinearLayout | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="match_parent" | |
android:padding="2dp"> | |
<TextView | |
android:id="@+id/text_view_name" | |
android:layout_height="20dp" | |
android:textColor="@android:color/holo_blue_dark" | |
android:paddingLeft="15dp" | |
android:textStyle="bold" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true" | |
android:gravity="center_vertical" | |
android:layout_width="match_parent" | |
android:background="#e7e7e1" | |
xmlns:android="http://schemas.android.com/apk/res/android" /> | |
<TextView | |
android:id="@+id/text_view_email" | |
android:layout_height="20dp" | |
android:textAlignment="gravity" | |
android:textColor="@android:color/holo_blue_dark" | |
android:paddingLeft="15dp" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true" | |
android:gravity="center_vertical" | |
android:layout_width="wrap_content" | |
xmlns:android="http://schemas.android.com/apk/res/android" /> | |
<TextView | |
android:id="@+id/text_view_desc" | |
android:layout_height="20dp" | |
android:textAlignment="gravity" | |
android:textColor="@android:color/holo_blue_dark" | |
android:paddingLeft="15dp" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true" | |
android:gravity="center_vertical" | |
android:layout_width="wrap_content" | |
xmlns:android="http://schemas.android.com/apk/res/android" /> | |
</LinearLayout> | |
</LinearLayout> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ekiras.demo; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.AbsListView; | |
import android.widget.ListView; | |
import com.ekiras.demo.adapter.PersonAdapter; | |
import com.ekiras.demo.model.Person; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private ListView listView; | |
private int bootCounter=0; | |
private int maxRecords = 400; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
final PersonAdapter personAdapter = new PersonAdapter(this,bootData()); | |
listView = (ListView) findViewById(R.id.person_list); | |
listView.setAdapter(personAdapter); | |
listView.setOnScrollListener(new AbsListView.OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(AbsListView absListView, int i) { | |
} | |
@Override | |
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
if(firstVisibleItem+visibleItemCount > totalItemCount-2 && totalItemCount<maxRecords){ | |
personAdapter.add(bootData()); | |
personAdapter.notifyDataSetChanged(); | |
} | |
} | |
}); | |
} | |
private List<Person> bootData(){ | |
List<Person> persons = new ArrayList<Person>(); | |
for(int i=bootCounter;i<bootCounter+20;i++){ | |
Person person = new Person(); | |
person.setName("person-" + i); | |
person.setDesc("description :" + i); | |
person.setEmail("person" + i + "@ekiras.com"); | |
person.setImage(R.drawable.user); | |
persons.add(person); | |
} | |
bootCounter+=20; | |
return persons; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ekiras.demo.model; | |
/** | |
* Created by ekansh on 19/10/15. | |
*/ | |
public class Person { | |
private String name; | |
private Integer image; | |
private String email; | |
private String desc; | |
@Override | |
public String toString() { | |
return "Person{" + | |
"name='" + name + '\'' + | |
", image='" + image + '\'' + | |
", email='" + email + '\'' + | |
", desc='" + desc + '\'' + | |
'}'; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Integer getImage() { | |
return image; | |
} | |
public void setImage(Integer image) { | |
this.image = image; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getDesc() { | |
return desc; | |
} | |
public void setDesc(String desc) { | |
this.desc = desc; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ekiras.demo.adapter; | |
import android.app.Activity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import com.ekiras.demo.R; | |
import com.ekiras.demo.model.Person; | |
import java.util.List; | |
/** | |
* Created by ekansh on 20/10/15. | |
*/ | |
public class PersonAdapter extends BaseAdapter{ | |
private List<Person> personList; | |
private Activity activity; | |
private LayoutInflater layoutInflater; | |
private ViewHolder viewHolder; | |
public PersonAdapter(Activity activity,List<Person> personList) { | |
this.personList = personList; | |
this.activity = activity; | |
} | |
public void add(List<Person> persons){ | |
this.personList.addAll(persons); | |
} | |
@Override | |
public int getCount() { | |
return personList.size(); | |
} | |
@Override | |
public Person getItem(int i) { | |
return personList.get(i); | |
} | |
@Override | |
public long getItemId(int i) { | |
return 0; | |
} | |
@Override | |
public View getView(int i, View view, ViewGroup viewGroup) { | |
if(view ==null){ | |
layoutInflater = LayoutInflater.from(activity); | |
view = layoutInflater.inflate(R.layout.list_item_person, viewGroup, false); | |
viewHolder = new ViewHolder(view); | |
view.setTag(viewHolder); | |
} | |
else | |
viewHolder = (ViewHolder) view.getTag(); | |
Person person = getItem(i); | |
viewHolder.setValues(person); | |
return view; | |
} | |
private class ViewHolder { | |
private TextView name; | |
private TextView email; | |
private TextView desc; | |
public ViewHolder(View view) { | |
name = (TextView) view.findViewById(R.id.text_view_name); | |
email = (TextView) view.findViewById(R.id.text_view_email); | |
desc = (TextView) view.findViewById(R.id.text_view_desc); | |
} | |
public void setValues(Person person) { | |
name.setText(person.getName()); | |
email.setText(person.getEmail()); | |
desc.setText(person.getDesc()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment