Showing posts with label refresh. Show all posts
Showing posts with label refresh. Show all posts

Sunday, September 3, 2017

Swipe to Refresh ListView using SwipeRefreshLayout

Swipe to Refresh ListView using SwipeRefreshLayout



With android.support.v4.widget.SwipeRefreshLayout, user can refresh the contents of a view via a vertical swipe gesture. This example show how to implement Swipe-to-Refresh ListView using SwipeRefreshLayout.


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidswiperefresh.MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<android.support.v4.widget.SwipeRefreshLayout
android_id="@+id/swiperefreshlayout"
android_layout_height="match_parent"
android_layout_width="match_parent">
<ListView
android_id="@+id/swipelist"
android_layout_width="match_parent"
android_layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>


MainActivity.java
package com.blogspot.android_er.androidswiperefresh;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

SwipeRefreshLayout swipeRefreshLayout;
ListView swipeList;

List<String> myList;
ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefreshlayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});

swipeList = (ListView)findViewById(R.id.swipelist);

myList = new ArrayList<>();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, myList);
swipeList.setAdapter(adapter);
}

private void refresh(){
//insert dummy string of current date/time
String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
myList.add(currentDateTime);
swipeList.invalidateViews();
swipeRefreshLayout.setRefreshing(false);
}
}


In this example, the ListView items is updated in SwipeRefreshLayout.OnRefreshListener(). But normally refreshing involve longtime task, such as loading from Internet. The next example show how to refresh with longtime task run in background thread.


download file now

Read more »

Thursday, August 24, 2017

SwipeRefreshLayout refresh in background thread

SwipeRefreshLayout refresh in background thread



Last post show a basic example of SwipeRefreshLayout. But, normally refreshing involve some longtime task; such as loading data from Internet. This example show how to refresh with longtime task in background thread.


MainActivity.java
package com.blogspot.android_er.androidswiperefresh;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

SwipeRefreshLayout swipeRefreshLayout;
ListView swipeList;

List<String> myList;
ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefreshlayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});

swipeList = (ListView)findViewById(R.id.swipelist);

myList = new ArrayList<>();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, myList);
swipeList.setAdapter(adapter);

swipeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, item, Toast.LENGTH_LONG).show();
}
});
}

private void refresh(){

final int pos = myList.size();
myList.add(pos, "Refreshing...");
swipeList.invalidateViews();
swipeRefreshLayout.setRefreshing(true);

//refresh long-time task in background thread
new Thread(new Runnable() {
@Override
public void run() {
try {
//dummy delay for 2 second
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

//update ui on UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
String currentDateTime =
DateFormat.getDateTimeInstance().format(new Date());
myList.set(pos, pos + " - " + currentDateTime);
swipeList.invalidateViews();
swipeRefreshLayout.setRefreshing(false);
}
});

}
}).start();
}
}


Keep using the layout in last post.

more:
- SwipeRefreshLayout, work with RecyclerView

download file now

Read more »

Sunday, August 13, 2017

Story of my Gateway VX1120 and Custom Refresh Rates on Windows and Linux

Story of my Gateway VX1120 and Custom Refresh Rates on Windows and Linux


A few months ago I picked up a Gateway VX1120 from a computer store in town for $5, not knowing if it would work or not. I was always a fan of CRT monitors, and seeing something of this size, I had to jump on it.

Got it home, plugged it in, and found out that it worked fine. Instantly replaced the Acer S201HL I was using without regrets. Colors were more vibrant, and animations are much smoother (one of my favorite things to do when switching from LCD to CRT is to move a window across my desktop to observe the smoothness). Plus the resolutions available were extensive, so I was sure I could find the right resolution to use daily. The other CRT monitors I have in the house were limited to around 1024x768, or something slightly higher at a lower refresh rate, so they werent too ideal.

After some time, I found that the resolution of 1280x960 suited perfectly as a default resolution. Its not high enough to make text hard to see, and at the same time, its large enough for the desktop not to feel cramped. The highest stock refresh rate for this resolution was 85Hz, which was great and all, but I soon looked into a way to improve that.

On Windows, I used a tool called Custom Resolution Utility (CRU). It allowed me to generate custom resolutions and refresh rates. I wanted to run at 120Hz just to be able to claim I had such a monitor, so that was the first refresh rate I tried. It didnt work; and the monitor complained about the frequency being out-of-range.

After a little bit of research, I found that my horizontal scan frequency could go as high as 121kHz. So going by that, I then had the idea to keep trying resolutions with CRU until my horizontal scan frequency was just under that limit. Lucky me, 119Hz seemed to have worked fine.

So in Windows, the refresh rate wasnt too hard to handle. Things got a bit more interesting when I went over to Linux however. I use an AMD Radeon HD 7850 graphics card (this specifically), so I have the choice of either using AMDs proprietary driver (fglrx), or the open-source drivers. The refresh rate on my screen however seems to act differently depending on which driver I use though.

If I recall correctly, I was able to use the CVT-generated 119Hz with fglrx without issue (its been a while since I used fglrx with this monitor), but this wasnt the case with the open-source driver.


Looking back at cvt, I noticed that my hsync value was 122.59 for 119Hz. Definitely over the 121kHZ limit, so then I tried going to 118Hz. It ended up at 121.45, which was still over the limit. I wasnt sure if 121kHZ was a hard limit, so I added the refresh rate and tried it anyway. Both 119Hz and 118Hz failed to work; so I tried 117Hz. Got the hsync value of 120.31, which was safely under the limit, and worked fine.

Im unsure how CRU and cvt generate the numbers they do, but a comparison at some point would be a great idea, whenever I find the time to do so.

So far, this monitor is still working great, and I have no plans of replacing it anytime soon, unless I go back to my standing desk setup.

download file now

Read more »