How to make refresh animation with android listview? - android

I have a listview for my android app and I want to make an animation at the top of the list view when the user over scrolls like one the one for the instagram or twitter app. How would I go about this. Is there a method that is called when the user over scrolls and I was thinking about using a frameanimation for the actually animation.

You can add a swipe refresh layout on your xml file:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/favs_refresher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:id="#+id/favs_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</android.support.v4.widget.SwipeRefreshLayout>
Then on your activity:
swipeRefresher = (SwipeRefreshLayout)findViewById(R.id.favs_refresher);
swipeRefresher.setOnRefreshListener(this);
Be sure that your activity implements SwipeRefreshLayout.OnRefreshListener
then you can make use of the onRefresh() method:
#Override
public void onRefresh() {
//do something
}

Related

RecyclerView not clickable

I have a LinearLayout with a nested RecyclerView showing a list of items. I'd like to open a popup when RecyclerView is clicked (either one of the items or the background white area), but the usual setOnClickListener is not working.
Of course I can put a click listener on each of the items, but the white area between them remains unclickable.
Is there a way to make the entire RecyclerView area clickable?
EDIT: I've added some sample code. I'd like to have the entire layout clickable to open a popup, but while the first three views behave properly, the RecyclerView does not.
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_half"
android:background="#color/color_item_margin_divider"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/fragment_tags_title"
style="#style/ItemFragmentHeader"/>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_line"
android:background="#color/color_line_divider"/>
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_half"/>
</LinearLayout>
Add onClickListener in the viewHolder . Below is a snippet of my project where I had implemented Listener
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView shotThumbnail;
public MyViewHolder(View view) {
super(view);
shotThumbnail = (ImageView) view.findViewById(R.id.shotThumbnail);
shotThumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Put here stuff that you want the onclickListener should do
}
});
Check if this link helps:
Detect click on RecyclerView outside of items
You should use padding instead of margin.
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_padding="#dimen/spacing_half"/>
Cause whenever you use padding it will crop your RecyclerView. But whenever you use padding it will just shorten your inside functions. hence you need padding. Then you should use an onClickListener in MainActivity.
recyclerView.setOnClickListener(v->{
//Do whatever you want
});

How to set load more on top of RealmRecyclerView instead of bottom?

RealmRecyclerView i want to load more data on swipe top instead of bottom how to set RealmRecyclerView default load more on top.
If you want to load more data by swiping up in RecyclerView, I think, SwipeRefreshLayout is the good option. It is very easy to implement and also give efficient callback on swipe with loader. Please refer following code as a reference:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
</android.support.v4.widget.SwipeRefreshLayout>
Callback Method:
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
}
});
Hope this will help you.

ListView with specific behaviour

I have a ListView, and I have to implement specific behaviour for it.
When user scrolls up the ListView , which is in initial position:
ListView should move down a bit;
ProgressBar should appear;
Next image shows the result that I want to achieve:
Any suggestions how to do this? Thanks.
Use SwipeRefreshLayout from support-v4 https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
Wrap the ListView in a SwipeRefreshLayout in xml:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe"
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" />
</android.support.v4.widget.SwipeRefreshLayout>
And set the listener in your onCreate():
SwipeRefreshLayout swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(this);
and implement it
#Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override public void run() {
swipe.setRefreshing(false);
}
}, 5000);
}
"There's a library for that"
another library
I believe this library does what you're looking for.
I got this picture from this question
There are lots of libraries doing similar things like in this video https://www.youtube.com/watch?v=YOYtPF-4RPg
You can use SwipeRefreshLayout or other library as suggested in #Cobbles answer.
It is not exactly the view you looking for, but the same feature. The advantage of swipe refresh layout is that is part of android support library and user are used to see it.

Android layout design: composite Relative vs fragments

I want a simple scrollable ListView. I have a custom adapter that makes each row display a picture and some text.
Underneath I want a few buttons.
I'm looking for a layout like this sample picture:
This seems like it should be trivial. I have a single main activity that extends ListActivity:
public class ShufflerMain extends ListActivity {
private ArrayList<ListEntry> init = new ArrayList<ListEntry>();
private Button btnAddEntry;
private Button btnShuffle;
private Button btnClearAll;
private InitiativeArrayAdapter initAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initViews();
loadDummyData();
listAdapter = new MyCustomArrayAdapter(this, init);
setListAdapter(initAdapter);
}
This is fine and dandy, but I wanted to have a few buttons underneath this list. (to do things like add to the list, clear the list, etc). My main XML looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.myapp.ShufflerFragment" >
<ScrollView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ScrollView>
<Button
android:id="#+id/clearAll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/clearAll"/>
<Button
android:id="#+id/add_new_entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/clearAll"
android:text="#string/addEntry"/>
<Button
android:id="#+id/shuffle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/add_new_entry"
android:text="#string/shuffle"/>
</RelativeLayout>
I can run the app just fine, but the Graphical layout preview in Eclipse looks right and I see my 3 buttons. When I run it on my phone, all I see is my ListView populated with my dummy data. I added calls in the Java to pull down the buttons and add listeners, and findViewById is returning null on them.
Have I completely set up this project incorrectly? Do I need to have a different Activity as my "Main" separate from the ListView in order to have any capability other than a full-screen list?
Thanks.
Your scroll view is empty. If you want to have multiple items scrollable in it put a viewgroup layout inside in example a linear layout and then put button inside.
Your views are returning null mostlikly because you didn't setcontentview() in the on create method. Rather than extending listActivity extend normal activity and supply layout that have listView

Can't remove or hide a view in Android

I'm trying to remove a progress indicator after loading my data in a fragment involving a ListView. Here is my completion handler:
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
dataSource = (ArrayList<Map<String, Object>>) task.getResult();
PostAdapter adapter = new PostAdapter(getActivity(), dataSource);
ListView list = (ListView) rootView.findViewById(R.id.listView);
list.setAdapter(adapter);
View indicator = getActivity().findViewById(R.id.indicator);
RelativeLayout layout = (RelativeLayout) getActivity().findViewById(R.id.layout);
layout.removeView(indicator);
}
});
The last 3 lines of code is the relevant part. Everything is called correctly, nothing is null etc. in debug everything works perfectly. The adapter also works correctly, populating my list, but the indicator is still on the screen. I've also tried setting it's visibility to GONE or HIDDEN but they also don't seem to hide it either. I've seen Android - Can't hide progress bar but it's answers involve setEmptyView() which I'm not using anyway. I am using the same fragment (of course, a different instance) in another tab, and it works correctly.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:background="#ffffffff"
android:id="#+id/layout">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_margin="0dp"
android:padding="0dp" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/indicator"
android:indeterminate="true"
android:layout_centerInParent="true" />
</RelativeLayout>
What am I doing wrong?
I've found out the problem (thanks to all the commenters). I was calling activity's methods to find and remove the view. The problem is that, I have multiple instances of the same fragment in the same activity, under different tabs. I've used my fragment's root view to find and remove the indicator, instead of the activity, and it worked.
Change the codes of last three lines in onClick(...) as:
View indicator = (ProgressBar)rootView.findViewById(R.id.indicator);
and use:
indicator.setVisibility(ProgressBar.GONE);
or codes:
RelativeLayout layout = (RelativeLayout) rootView.findViewById(R.id.layout);
layout.removeView(indicator);

Categories

Resources