SwipeRefreshLayout not working - android

I'm using SwipeRefreshListener from the support libraries. I am calling setRefresh from onPreExecute and onPostExecute of my AsyncTask. However, nothing changes, there is no animation. Is there something that I'm missing? Are there certain setup parameters I need to set in order for it to work properly?

This has worked for me, see more here: http://antonioleiva.com/swiperefreshlayout/
SwipeRefreshLayout: The layout
you only need to decorate the swipable content (probable the whole layout) with this new layout. This view must be scrollable, such a ScrollView or a ListView. As a simple example:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
The code
We just need to get the layout, and assign some colours and the listener. The refreshing listener is a post delayed handler.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
#Override public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}

//Please check this answers it hepls me :
//SwipeRefreshLayout setRefreshing() not showing indicator initially
//especially this
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
}
});

Related

RecyclerView gets Recreated when the videoview is in fullscreen

I have a recyclerview which onClick shows a videoview with fullscreen button..
However when i click the full screen button on the videoview.. My RecyclerView gets displayed (or created) again..
I want to display only the video in the videoview in the fullscreen mode..
The following is my Acticity OnCreate() Method :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mangal_stuti_activity);
toolbar = findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
setSupportActionBar(toolbar);
recyclerView = (findViewById(R.id.recycler_view));
mAdapter = new Adapter(this, movieList, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL,16));
recyclerView.setAdapter(mAdapter);
recyclerView.setBackgroundColor(getResources().getColor(R.color.recyclerViewBackground));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), this.recyclerView, new RecyclerTouchListener.ClickListener()
{
public void onClick(View view, int position) {
toolbar.setVisibility(View.GONE);
playVideo();
}
public void onLongClick(View paramAnonymousView, int paramAnonymousInt) {}
}));
prepareMovieData(); // possibly this function is creating the recyclerview again when the fullScreen is called..
mVideoView = findViewById(R.id.videoView);
mYouTuDraggingView = findViewById(R.id.youtube_view);
mYouTuDraggingView.setCallback(this);
controlPanel = new YoutubeControlPanel(this);
controlPanel.setYouTuDraggingView(mYouTuDraggingView);
mVideoView.setControlPanel(controlPanel);
controlPanel.getFullScreenIv().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mYouTuDraggingView.fullScreenChange();
}
});
controlPanel.getDownIv().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toolbar.setVisibility(View.VISIBLE);
mYouTuDraggingView.fullScreenGoMin();
}
});
}
The fullScreenChange() in YouTuDraggingView class is as below:
void fullScreenChange() {
if (isLandscape()) {
isLandscapeToPortrait = true;
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else {
isPortraitToLandscape = true;
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
}
The mangal_stuti_activity.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
tools:context="com.amitabh.dhamma_jaagran.MangalStuti"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/recyclerViewBackground"
android:id="#+id/activity_main"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="#layout/toolbar_stuti_activities"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<com.amitabh.YouTuDraggingView
android:id="#+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:orientation="vertical"
android:visibility="invisible"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:id="#+id/footer_text"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#color/colorPrimaryDark"
android:text="#string/copyright_footer"
android:textAlignment="center"
android:textColor="#color/white" />
</RelativeLayout>
</LinearLayout>
When i click the fullscreen button on the videoview.. the videoview only should show on the fullscreen.. not the recyclerview...
i think the problem might be in
mAdapter = new Adapter(this, movieList, this);
where another movieList gets created when the videoview enters fullscreen..
Any help regarding this would be of great help to me..
thanks in advance..
can you please share mangal_stuti_activity.xml code.
Make sure that youtube_view width and height is set to match_parent.
It looks like you're requesting an orientation change when make your video full screen. When the activity orientation changes, the activity gets destroyed and is recreated. This is part of the Activity Life Cycle. What you're going to have to do save and restore your state of the activity and RecyclerView when this happens.
Here's an example on how to do that:
https://guides.codepath.com/android/Handling-Configuration-Changes#recyclerview
I Just added this line to my activity in which the video is being played in the AndroidManifest.xml file
android:configChanges="orientation|screenSize"
This prevented the activity from being recreated.
Thanks to vignesh-ramachandra for his answer which is provided in here

Android ProgressBar styled like progress view in SwipeRefreshLayout

I use android.support.v4.widget.SwipeRefreshLayout in my Android app. It wraps a ListView. The content of the list view is downloaded from a server.
A progress view is shown when user swipes down in order to reload data from server. The progress view looks like a piece of circle that grows and shrinks during the animation. It seems that the style of this progress view cannot be customized much. However, I am fine with its built-in style.
I also show the same progress animation during initial data loading. This can be achieved by calling mySwipeRefreshLayout.setRefreshing(true). That's perfectly OK too.
However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.
Is there a simple and clean way to have the same progress indicator for both a SwipeRefreshLayout and a form activity that does not contain any list view and refresh layout and does not support any swipe gesture?
Thanks.
However, I would like to show the same progress indication through the
whole app. Consider e.g. another activity that looks like a form with
submit button. There is neither a ListView nor a SwipeRefreshLayout in
this form activity. Some progress indication should be displayed while
the submitted data are transferred to the server. I want to show a
progress bar with the same animation like in SwipeRefreshLayout.
yes you can use SwipeRefreshLayout to show like ProgressDialog when button click. check below.
i have added SwipeRefreshLayout inside my layout file still there is not ListView or ScrollView. just like below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="niu.com.djandroid.jdroid.androidstack.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/activity_main_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="54dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
now in my activity i used AsyncTask to show SwipeRefreshLayout. also use mSwipeRefreshLayout.setOnRefreshListener(null) to stop swipe gesture.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(null);
((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// mSwipeRefreshLayout.setRefreshing(true);
// call AsyncTask
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// stop mSwipeRefreshLayout
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
protected void onPreExecute() {
// start mSwipeRefreshLayout
mSwipeRefreshLayout.setRefreshing(true);
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
EDITED:15/02/20
After 4 year, Here is great explanation on android developer site
mSwipeRefreshLayout.setOnRefreshListener(null)
didn't work for me, so I made this method to enable and disable refreshing.
private void setToRefresh(boolean refresh) {
if (refresh) {
mSwipeRefreshLayout.setEnabled(true);
mSwipeRefreshLayout.setRefreshing(true);
} else {
mSwipeRefreshLayout.setRefreshing(false);
mSwipeRefreshLayout.setEnabled(false);
}
}

Can I use Pull to Refresh without using ScrollView/ListView?

I tried to implement Pull to Refresh in my app.
I wrapped my xml code with android.support.v4.widget.SwipeRefreshLayout and it's working, but the GUI doesn't look good.
When I pull down, I should see the regular animated circle icon with my defined colors, right?, but I see it as a blank circle and not a turning one with colors as it should.
Furthermore, I can't "play" with it and even hold it for a while after I pull it, it goes down for one second (do what it does) and then disappears.
I think it's because I'm not using ListView but LinearLayout instead.
What do you think?
Code:
XML:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/swipe">
<LinearLayout
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:layout_width="match_parent"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#7B68EE"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".HomeActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loin"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/welcome"
android:textColor="#B0C4DE"
android:id="#+id/welcomeBackID"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/webViewCurren"
android:layout_alignEnd="#+id/webViewCurren" />
....
</android.support.v4.widget.SwipeRefreshLayout>
Java:
public class HomeActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout mSwipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_green_light,
android.R.color.holo_blue_bright,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeRefreshLayout.setOnRefreshListener(this);
#Override
public void onRefresh() {
Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
sharedPrefEditor.putString("coordinateX", "34.7");
sharedPrefEditor.putString("coordinateY", "32.5");
sharedPrefEditor.putString("saved", "false");
sharedPrefEditor.commit();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 3000);
}
}
Try changing the SwipeRefreshLayout color scheme.
swipeRefreshLayout.setColorSchemeColors(
Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);
Also, the SwipeRefreshLayout will be visible only till onRefresh() completes its execution.

How to add a header to a listView which is inside SwipeRefreshLayout?

Background
I've noticed an issue (link here) when adding a header view inside a ListView, in case the ListViewis inside a SwipeRefreshLayout.
The problem
The issue is that if the header view is partially visible, and you scroll up, you will see the circular view of the SwipeRefreshLayout.
You can check the link above to see a video of what I'm talking about.
Sample code
Here's how to reproduce this issue (you can check the link above in case you don't want to copy-paste) :
MainActivity.java
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView=(ListView)findViewById(android.R.id.list);
final TextView tv=new TextView(this);
tv.setText("a very large header view");
tv.setTextSize(70);
listView.addHeaderView(tv);
final SwipeRefreshLayout swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipeToRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener()
{
#Override
public void onRefresh()
{
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
swipeRefreshLayout.setRefreshing(false);
}
},1000);
}
});
final ArrayList<String> arrayList=new ArrayList<String>();
for(int i=0;i<1000;++i)
arrayList.add(Integer.toString(i));
listView.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList));
}
}
res/layout/activity_main.xml
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeToRefreshLayout"
android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:paddingEnd="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingStart="4dp"
android:scrollingCache="false"
android:smoothScrollbar="false"
android:textFilterEnabled="true"/>
</android.support.v4.widget.SwipeRefreshLayout>
The question
Is there a way to solve this problem?
I guess I could use multi-types for the listView , but is there maybe another solution that is more elegant, simple or better in any other way?
Very similar to Android issue 78375.
Try to override SwipeRefreshLayout#canChildScrollUp
#Override
public boolean canChildScrollUp() {
View target = getChildAt(0);
if (target instanceof AbsListView) {
final AbsListView absListView = (AbsListView) target;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return target.getScrollY() > 0;
}
}
EDIT: It seems it occurs even if I use multi-type items for the ListView. Later I've discovered that it just always occur for the first item on the listView.
If the first item is large enough, it will be easy to reproduce.
So, for now, there is a simple workaround:
Before using the listview in any way, first add an empty view as a header view, and never remove it:
listView.addHeaderView(new View(this));
This way, as the view is practically invisible, the bug won't be shown to the user.
This is just a workaround, but I think the best thing should be to fix this issue.

Android PullToRefesh library customize loading pulling/refreshing image

I am using com.handmark.pulltorefresh.library.PullToRefreshListView. By default it shows:
But I want pulltorefresh images/desing as following:
Here is xml i am using:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="#+id/lv_Inbox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:scrollbars="none"
android:smoothScrollbar="true"
ptr:ptrAnimationStyle="flip"/>
<ProgressBar
android:id="#+id/progress_LoadingList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible" />
<TextView
android:id="#+id/lbl_NoMessagesFound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
The animation style controls how the Pull-to-Refresh functionality is presented to the user. The different options are:
Rotate (default)
Flip
Customizing icon and custom labels
And you want flip,So in your xml view
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/pull_to_refresh_listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
ptr:ptrAnimationStyle="rotate"
/>
Change
ptr:ptrAnimationStyle="rotate"
to
ptr:ptrAnimationStyle="flip"
And add xmlns:ptr="http://schemas.android.com/apk/res-auto" in com.handmark.pulltorefresh.library.PullToRefreshListView tag as
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/lv_Inbox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:scrollbars="none"
android:smoothScrollbar="true"
ptr:ptrAnimationStyle="flip"/>
For more info see android-pull-to-refresh Customization
An advanced pull to refresh will help you to provide modern approach in your app.
It is very smooth and easy to implement. And it will support for the lower versions also (from v2.3). The below screen shot will recall the new kind of refresh.
UI part
Create an Android from your eclipse or Android studio.
In your activity layout add the SwipeRefreshLayout.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"/>
</ScrollView>
Note : It has only View inside the layout. Add the scrollView inside the SwipeRefresh layout to support pull to refresh. For ListView and GridView no need to add ScrollView inside SwipeRefreshLayout.
code part
Add the following lines in onCreate method of your Activity.
public class SwipeActivity extends Activity implements OnRefreshListener {
SwipeRefreshLayout swipeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
}
#Override
public void onRefresh() {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
#Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}
swipeLayout.setOnRefreshListener(this); sets Refresh listener for your layout.
Loading color schemes are adding using
swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
To stop the loading progress swipeLayout.setRefreshing(false);.
Thats it. Run your appplicaton you also did the new pull to refresh.
SWIPE TO REFRESH WITH LISTVIEW
Here I add an example of pull to refresh with listview. Then you will know how easy it is to integrate.
Create a new Activity in your application, add the listview in your SwipeRefreshLayout.
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
Declare and initialze variables for listview, adapter, String array for list items. Below codes will add the list items from array when you do pull to refresh.
public class SwipeActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.swipe, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnRefreshListener {
SwipeRefreshLayout swipeLayout;
ListView listView;
ArrayAdapter adapter;
ArrayList< String> arrayList;
String [] array = new String[]{"Apple","Batman","captain America","darkknight"};
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_swipe, container, false);
swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
listView= (ListView) rootView.findViewById(R.id.listview);
adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, appendData());
listView.setAdapter(adapter);
return rootView;
}
#Override
public void onRefresh() {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
#Override public void run() {
appendData();
adapter.notifyDataSetChanged();
swipeLayout.setRefreshing(false);
}
}, 5000);
}
private ArrayList appendData(){
if(arrayList==null)
arrayList = new ArrayList();
for (String items : array) {
arrayList.add(items);
}
return arrayList;
}
}
}
Note: I have used appcompat library to support the Actionbar for lower versions. So My Activity extends ActionBarActivity. In updated ADT version of eclipse and studio, your application will be created with fragment concepts. So application is created with instant fragment concepts.
I guess you like this example. Try it yourself and get practiced.
Happy coding :-).
And resultant screen, see this:
And for your existing program, use this link

Categories

Resources