Swipe down on your screen to refresh the ListView

Demonstration:

 

Step 1:

See on the leftside in Android Studio, Change the view mode from “Android” to “Project”.
Then, go to “Your Project Name” -> “App” -> open “build.gradle” file. Add the following line to the dependencies:

compile ‘com.baoyz.pullrefreshlayout:library:1.2.0’
Step 2: file main activity.xml

<!--?xml version="1.0" encoding="utf-8"?-->

<button>
</button>

 

Step 3: main.java, code below:

 

public class MainActivity extends Activity {
private Button btn;
private EditText et;
private ListView lv;
ArrayList arr = new ArrayList();
ArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
et = (EditText)findViewById(R.id.et);
lv = (ListView)findViewById(R.id.lv);
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, arr);
lv.setAdapter(adapter);

// This is a hack :))
// =====================================================
PullRefreshLayout layout = (PullRefreshLayout) findViewById(R.id.swipeRefreshLayout);
layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES);
layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
adapter.notifyDataSetChanged();
}
});
layout.setRefreshing(false);
// ====================================================================

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arr.add(et.getText().toString());
et.setText("");
}
});
}
}

Thanks!

Advertisements

2 thoughts on “Swipe down on your screen to refresh the ListView

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s