I am trying to use Chris Banes' library Actionbar-PullToRefresh. It can be found here.
I am using Tabs + ViewPager + Fragments in my app.
The problem I'm facing is that my fragment has a GridView and I cannot figure out how to use this library to work with it.
I read through the sample code. He says that all you have to do is, wrap your refreshable view in a PullToRefreshLayout like this:
<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Your content, here we're using a ScrollView -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ScrollView>
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
This works great for stuff like ListView, ScrollView, GridView, etc. However, apparently this will not work for Fragments (Tabs & ViewPagers). Now, in the sample code he has wrapped the refreshable fragment with a ScrollView INSTEAD of a PullToRefreshLayout.
I cannot do this because my Fragment 1 (under tab 1) has a GridView. Now I cannot add a GridView to a ScrollView because that just wouldn't make sense.
For example, if I put my GridView inside the ScrollView as shown below, it just doesn't make sense:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbarStyle="outsideInset" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF000" >
<!-- MY GRID VIEW -->
<GridView
...
/>
</RelativeLayout>
</ScrollView>
The above code works. Sort of. (I need to disable scrolling of my GridView and use an ExpandableGridView to get it working properly... which seems like overkill & I'm not even sure if that would work).
If I replace the ScrollView wrapper with anything else like PullToRefreshLayout or any other layout, the refreshing doesn't work.
What to do? How to get my layout to work with this library without wrapping a ScrollView around it?
I hope I was clear enough. Please let me know if you need any more info. I tried to explain it the best I could.
Thanks!
I had a similar issue trying to get it to work with a ListView that I had in one of my tabs.
I solved my issue by using the PullToRefreshAttacher instead of using the layout.
In your Activity that is controlling the ViewPager for the fragments, initialize a PullToRefreshAttacher in onCreate or an init method.
mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
Next make a public method that allows access to the attacher that you just initialized.
public PullToRefreshAttacher getPullToRefreshAttacher() {
return mPullToRefreshAttacher;
}
Then in the fragment you want the refresh functionality.
mPullToRefreshAttacher = ((MainTabActivity) getActivity())
.getPullToRefreshAttacher();
mPullToRefreshAttacher.addRefreshableView(activeListView, this);
Except in your case activeListView would be the reference to your GridView instead of a ListView.
Then make sure your fragment implements OnRefreshListener so you can handle the Refresh.
I have not actually tested this with a GridView so let me know if it works.
Good Luck!
Related
sorry for the long post, i tried to make it as readable as possible.
I have a decent knowledge of JAVA and trying to understand Android programming. Today i have been working on my knowledge of fragments.
I have read this article and got the basics of working with them.I managed to make fragments and replace them when needed.
I proceeded by taking this project, and thought it might be fun to try and add a fragment to it with a basic tictactoe game i had build for practising purposes.
Now i am having problems with replacing the fragment. Everything works fine until I add the TicTacToe view in the mix, which has a tablelayout. The fragment is not getting replaced, rather is it adding an extra fragment (or so it seems) and uses this to switch fragments in.
BoardField is simply a class I made which extends Button
The layout thats not working for me is this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0f0fdf">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"
android:onClick="selected"
/>
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"/>
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"/>
</TableRow>
</TableLayout>
I omitted the other rows for readability, they are similar.
So my question is, why is replacing the fragments not working when I use TableLayout ??
EDIT:
Eric's solution worked, i hope to find out more about why?
Why parent view is TableLayout? Try to changing it to a RelativeLayout with a child, the TableLayout (just for test purpose)
With a RelativeLayout as parent view, are you able to "replace" instead of "add" the Fragment?
I think your mistake is not here. Perhaps your can look in code where you create new fragment or code where you replace fragment. There is no sense TableLayout make a problem to replace fragment. Eric is right RelativeLayout is better then TableLayout, but this is not the problem. Can you put more of your code?
I'm building an app which I'd like to add a ListView to an activity not a listActivity but the activity also contains other widget controls. Iv tried doing it but it doesn't look good at all. Here is the xml that created the ListView in my activity:
<ListView
android:id="#+id/experienceList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="18dp"
android:layout_marginLeft="18dp" >
</ListView>
Unfortunately stackoverflow didn't allow me to post a image. But the ListView doesn't reveal all its contents. how can I make the ListView reveal all and just stretch it out, the root of the activity is a scrollView so it should probably work the way i want it.
Putting a scrollable (ListView) inside another one (ScrollView) will not work, for obvious reasons. Please refer to this question for more details, answered by Google's very own Romain Guy. Replace your ListView by a LinearLayout ...
I am creating the TabHost activity and taking a ListView in the TabHost. The ListView is bydefault scroll vertically wihtin TabHost.
Now i want to take ScrollView within the TabHost. Is it possible to do like that, because i have tried but it's not working.
Kindly help me out. please provide me any tutorial link related to this.
With Thanks
Vikash
you can use a scrollView within TabHost, But you need to take horizontal scrollView as main tag within that TabHost.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
I searched for similar questions but didn't get the proper answer, so posting it as a new question.
Can a LinearLayout have two TextViews and one list view? I learn that listview can be used to display array elements only if the class extends ListActivity. The Class in my app extends Activity and uses listView.
I am trying to build an RSS Reader based on IBM tutorial. On running the project I am getting parsed text in both the Text views but ListView is not displaying. I can post the code and Logcat if required.
Linear Layout can have any number of children.
ListActivity is an Activity that Android have added for convenience when dealing with activities comprised of list.
You can use ListView on a regular Activity and just implement an adapter that will populate the list items with data from your model.
Android has some ready-made adapters that can be used for simple use-cases.
of course if you need a more complicated beahviour you can extend them.
Have a look on BaseAdapter, ArrayAdapter and CursorAdapter to get a better understanding of how to use adapters.
You can have a ListView in any activity, using a ListActivity just makes your life easier (usually). See here for details.
You can have a Linear layout with different views and list view inside for example:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background_new_search_activity_1">
<TextView
android:id="#+id/list_item_amount_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
</LinearLayout>
And yes you can use the ListViewin all the activities, it is not necessary to extend ListActivity
Make sure the orientation of the ListView is set to vertical. Otherwise, it will try to display all items side by side, and those that fall outside the available view area won't be visible.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <!-- << orientation setting here -->
I need to create a layout with a title bar on the top and a list view with n sections. The
list header of every section has got two distinct Buttons that must be clickable and focusable independently from each other. This is the code of the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/title_bar" />
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
/>
</LinearLayout>
I tried this two solutions but I've found issues that I am not able to solve in both:
1) Using a sectioned adapter (source code can be found here). It works fine without the title bar but with the title bar the default focus algorithm doesn't work any more.
2) Using an ExpandableListAdapter (in this case obviously the ListView in the layout becomes an ExpandableListView). It works fine but if I press the dpad center button on group layout both buttons are clicked. I did not find a way to solve this.
Anyone can help?
Thanks very much!
How about trying Merge adapter https://github.com/commonsguy/cwac-merge it allows you to add multiple list and buttons !!