Listview with Swipe Referesh use to refereshing list of data to new data is refereshed at runtime.and refreshing adapter to notify of adapter in swipe refresh.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/swipeLayout"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
MainActivity.java
package com.example.bhaumik.listviewradiobutton; import android.graphics.Color; import android.support.v4.view.MenuItemCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckedTextView; import android.widget.ListView; import android.widget.SearchView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; ArrayList<String> list; ArrayAdapter adapter; String[] version = {"Aestro","Blender","CupCake","Donut","Eclair","Froyo","GingerBread","HoneyComb","IceCream Sandwich", "Jelly Bean","Kitkat","Lolipop","Marshmallow","Nought","Oreo"}; SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.list_view); swipeRefreshLayout = findViewById(R.id.swipeLayout); list = new ArrayList<>(); for (int i = 0;i<version.length;i++){ list.add(version[i]); } swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { list = new ArrayList<>(); for (int i = 0;i<version.length;i++){ list.add(version[i]); } Toast.makeText(MainActivity.this, "Refreshed Data..", Toast.LENGTH_SHORT).show(); swipeRefreshLayout.setRefreshing(false); } }); listView.setChoiceMode(listView.CHOICE_MODE_SINGLE); adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "Selected -> " + version[i], Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); MenuItem searchViewItem = menu.findItem(R.id.app_bar_search); final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchViewItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { searchView.clearFocus(); return false; } @Override public boolean onQueryTextChange(String newText) { adapter.getFilter().filter(newText); return false; } }); return super.onCreateOptionsMenu(menu); } }
0 comments:
Post a Comment