Pagination for spinner(dummy )
Step 1: Create xml activity_spinner_list.xm.
Step 2: SpinnerListActivity.class
Step 3: create adapter
Step 4: adapter inflator layout.
Step 5: create model
Step 6: Dependencies.
Output:
Step 1:
<?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:id="@+id/activity_spinner_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sciens.com.spinnerpagination.SpinnerListActivity">
<TextView
android:id="@+id/spinner_iv"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:drawableRight="@drawable/ic_arrow_drop_down"
android:text="Select country"
android:background="@drawable/spinner_background"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:id="@+id/listview_ll"
android:layout_width="150dp"
android:layout_height="match_parent"
android:padding="12dp"
android:background="#ffffff"
android:visibility="gone"
android:elevation="10dp">
<ListView
android:id="@+id/listivew_lv2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</RelativeLayout>
Step 2:
public class SpinnerListActivity extends AppCompatActivity {
private TextView mSpinner;
private String Root_url = "http://codex.scienstechnologies.com/PetLuvs/api";
private List<CountriesModel> countriesModelList = new ArrayList<>();
private String TAG = "LokeshMA";
private DetailsAPI api;
private ListView mListView;
private int visibleThreshold = 0;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;
private CountryAdapter adapter;
private LinearLayout listview_ll;
private RelativeLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_list);
mSpinner = (TextView) findViewById(R.id.spinner_iv);
mListView = (ListView) findViewById(R.id.listivew_lv2);
listview_ll = (LinearLayout) findViewById(R.id.listview_ll);
root = (RelativeLayout) findViewById(R.id.activity_spinner_list);
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listview_ll.setVisibility(View.GONE);
}
});
mSpinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mShowListViewLayout();
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(SpinnerListActivity.this, "you clicked at"+i, Toast.LENGTH_SHORT).show();
listview_ll.setVisibility(View.GONE);
mSpinner.setText(""+adapterView.getAdapter().getItemId(i));
}
});
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(Root_url).build();
api = adapter.create(DetailsAPI.class);
mListView.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 (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// I load the next page of gigs using a background task,
// but you can call any function here.
// new LoadGigsTask().execute(currentPage + 1);
mLoadData();
loading = true;
}
}
});
}
private void mShowListViewLayout() {
adapter= new CountryAdapter(countriesModelList,getApplicationContext());
mListView.setAdapter(adapter);
mLoadData();
}
private void mLoadData() {
api.insertUser(currentPage,new Callback<JsonArray>() {
@Override
public void success(JsonArray jsonArray, Response response) {
for (int i = 0; i < jsonArray.size(); i++) {
CountriesModel countriesModel = new CountriesModel();
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
countriesModel.setCountry(jsonObject.get("country").getAsString());
countriesModel.setId(jsonObject.get("id").getAsInt());
countriesModelList.add(countriesModel);
}
Log.d(TAG, "success: before"+countriesModelList.size());
filetDummyContriesListFromAPI(countriesModelList);
Log.d(TAG, "success: after"+countriesModelList.size());
adapter.notifyDataSetChanged();
listview_ll.setVisibility(View.VISIBLE);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(SpinnerListActivity.this, "Internet connection error!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "failure: retro"+error.getMessage());
}
});
}
private void filetDummyContriesListFromAPI(List<CountriesModel> countriesModelList) {
Log.d(TAG, "filetDummyContriesListFromAPI: before2"+countriesModelList.size());
TreeSet<CountriesModel> myset = new TreeSet<CountriesModel>(countriesModelList);
Log.d(TAG, "filetDummyContriesListFromAPI: after"+countriesModelList.size());
this.countriesModelList.clear();
this.countriesModelList.addAll(myset);
}
}
Step 3:
public class CountryAdapter extends BaseAdapter {
private List<CountriesModel> countriesModelList ;
private Context mContext;
private LayoutInflater inflater;
public CountryAdapter(List<CountriesModel> countriesModelList, Context mContext) {
this.countriesModelList = countriesModelList;
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return countriesModelList.size();
}
@Override
public Object getItem(int i) {
return countriesModelList.get(i);
}
@Override
public long getItemId(int i) {
return (long)countriesModelList.get(i).getId();
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Holder holder = new Holder();
if (inflater == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.simple_spinner_item, null);
holder.tvSpinner = (TextView) view.findViewById(R.id.spinner_tv);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
holder.tvSpinner.setText(countriesModelList.get(i).country);
return view;
}
class Holder {
private TextView tvSpinner;
}
}
Step 4:
simple_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_tv">
</TextView>
Step 5:
public class CountriesModel implements Comparable<CountriesModel>{
String country;
int id;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int compareTo(CountriesModel countriesModel) {
if(country.equals(countriesModel.getCountry()))
return 0;
else
return 1;
}
}
<uses-permission android:name="android.permission.INTERNET" />
compile 'com.squareup.retrofit:retrofit:1.9.0'
output: