I'm trying implement "pull to refresh" on a ListFragment but right now none of the drop in libraries seem to support it. There's no way to detect overscroll on the list fragment that I can see so I'm wondering if anyone has found a means to get this working?
--
Using Christian's tip I used the following for my onCreateView() method.
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
PullToRefreshListView listView = new PullToRefreshListView(getActivity());
mAdapter = new HomeTweetListAdapter(getActivity(), R.layout.tweet_list_item, tweets);
listView.setAdapter(mAdapter);
return listView;
}
Like Christian said you can only do this with a Fragment. Returning anything other than a ListView on a ListFragment errors out.
EDIT:
To clarify I am using Johan's PullToRefresh library
I actually make it work using fragments (not ListFragment). So it is basically the same, just return the PullToRefreshListView from your onCreateView method and that's it. It should also work with ListFragment; remember that you must return a ListView from onCreateView if you use ListFragment (you can return whatever you want if you use just Fragment).
Here is a Component that Johan has created that adds a pull down to refresh feature..
Pull-To-Refresh
Main logic is implemented in PullToRefreshListView that extends ListView.
n your layouts you simply add it like this.
<com.markupartist.android.widget.PullToRefreshListView
android:id="#+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
It is now fully documented how to use ActionBar-PullToRefresh together with ListFragment:
https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/ListFragment
Hope that helps!
Related
I have been trying for days to find a solution to my problem, but now I've decided to try to ask you. I'm a noob for programming Android, so please forgive me.
I have a main Activity with a Listview in it.
I am using a Simplecursoradapter to feed it with information from my database and a customized layout I made as an XML.
my problem is, I want to change some of the terms or the units that I used in this custom layout, which is not feed through the database.
But if I use settext to the TextViews in there it wont run, the app will crash, because my setContentView is set to another layout for this class, I guess. I have been looking at inflaters, trying to see if I could change the XML programmable, use a string from String.xml and change that. But as far as I can see these are not an option. Later I found this code on StackOverflow
Activity activity = (Activity)getContext();
TextView t = (TextView)activity.findViewById(R.id.txtDisponible);
t.setText("E-ticket validado");
But I can't get this to work because the first line isn't working for me. The Activity won't give me the getcontext method. And even if I did get this to work, I wouldn't know if this would work. Could you guide me in which direction I need to go? As of now, I don't have a class for my ListView, is that the way to go. I want to keep this as simple as I can.
Thank you for your help.
EDIT 1:
Okay i tried to put this in my code:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_list, null);
TextView t = (TextView) view.findViewById(R.id.kmListview);
t.setText("bok");
The app doesn't crash, but it doesn't change the textview either.
Activity extends ContextWrapper class and you should use this or getBaseContext() because there is no method like getContext() which extends ContextWrapper class.
So use this as Context or getBaseContext() in your Actitiy and I think your problem will be resolved.
I'am trying to use a custom listView in my application and i have some questions about its working principles.When i implemented a custom adapter,how its methods(especially the getView method)work without calling them from any other class ?
When you set the adapter to a view (e.g. ListView or GridView), that view at some point wants to have some items to show. So it calls getView in the adapter:
getView(int position, View convertView, ViewGroup parent).
The position is the position of the item in the list/grid/etc. The convertView is a recycled view that can be already inflated by a previous getView() call, or null when it's not inflated yet (see this answer about convertView and view recycling.
The parent is used to inflated the view, so the correct layout parameters can be computed in relation to the parent view.
Note that inflation is slow. That is why the convertView mechanism exists, to recycle views so the number of inflations is minimized. Next to that, finding views (findViewById()) is also relatively slow. To improve on that, check out the ViewHolder pattern, which keeps references to views in memory so they don't have to be searched for each time.
I think this link can help you. getview is a callback function which will be called automatically when you will display your listview on Activity. When you display your listview then you overrides getview and inflates your row from XML or dynamically creates your row. That row you return as a view which displays in your listview.
How does the getView() method work when creating your own custom adapter?
For each row getview will be called once. You create your layouts and return them as view. Those respective views displays in your lisview rows.
You are calling the custom adapter class from you activity class.Your customised adapter class extends a BaseAdapter which is an abstract class.The methods of an abstract will be used by the extended class(the methods like getView(), getItemId(), getItem(), and getCount()).These methods should not need a seperate call from your Class since you are calling the customised adapter classs .
In one of my Fragments I have a method I call from the parent Activity. The method is nothing special, it simply scrolls my ListView to a position.
public void scrollToSomething() {
mListView.smoothScrollToPosition(The position I supply);
}
When I call this method initially, it works just like I expect, but when I rotate my device and call it again, I throw a NullPointerException from my ListView. I'm very much lost as to why this is happening. I've tried calling setRetainInstance(true). I've tried checking to see if my ListView is null before I call the method and if it is, then I initialize it again, but this does not work either. I've tried changing my onCreateView in a few different ways, thinking maybe something is going wrong there.
onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* The View for the fragment's UI */
final ViewGroup mRootView = (ViewGroup)inflater.inflate(R.layout.fragment_list_base,
container, false);
/* Initialize our ListView */
mListView = (ListView)mRootView.findViewById(R.id.fragment_list_base);
return mRootView;
}
The Fragment is attached to a ViewPager using a simple FragmentStatePagerAdapter. I've tried carefully looking through the onDestroy methods of my Fragment and the parent Activity to see if I'm calling something that may cause the error, but nothing stands out. I'm not sure if what I'm experiencing is some sort of bug, or if I'm just overlooking something. Any help or advice would be huge. I'm not entirely sure what else to add because I'm just pretty lost as why this is happening, but if anyone has a question, I'll post any code you may see fit.
This may help you
If your android:targetSdkVersion="12" or less:
android:configChanges="orientation|keyboardHidden">
If your android:targetSdkVersion="13" or more:
android:configChanges="orientation|keyboardHidden|screensize">
I've created a custon preference screen for my Android application (Honeycomb), that contains a ListView.
public class XXXPreferences extends Preference
{
#Override
protected View onCreateView(ViewGroup parent)
{
ListView...
}
}
Everyting is shown as expected, but the ListView is not scrolling. It seems that it is because the Preference class has a build in ScrollView and the ListView does not supported to be embedded in a ScrollView.
So I would like to know if it is possible to remove the ScollView of the Preference class or if there is another trick to make things work !?
Thanks in advance for your response :)
Cheers
If the preference class has a scrollview, you shouldn't need your ListView. Just use a vertical LinearLayout and add your layouts to that as needed.
I've looked everywhere for a solution to this, but I can't figure out how to implement it. My OnItemClickListener was disabled somehow on my ListView rows, because I have an ImageButton in the row layout, which takes over the focus. There have been numerous questions I've found, but none of them have gotten me anywhere.
I've checked this question, but I couldn't really make heads or tails of it. I just need a way to get the rows clickable so that I can detect when a row is pressed. Long press and focus work fine.
Instead of an OnItemClickListener, add an OnClickListener to each of your views returned from your adapter. You'll need to use setItemsCanFocus setting up your list:
ListView list = (ListView) findViewById(R.id.myList);
list.setAdapter(new DoubleClickAdapter(this));
list.setItemsCanFocus(true);
and then in your Adapter's getView, this will yield a clickable row. The button is assumed to be in the inflated xml.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.cell, null);
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(context).setTitle("touched").show();
}
});
return view;
}
set your ImageButton's attribute:
android:focusable="false"
Because AbsListView.onTouchEvent check child.hasFocusable().
I've tested the following solution on SDK levels 8 and 16.
In getView()
setFocusable(false);
setClickable(false);
rather than setting them true in the Adapter's getView() does what I think the original question wanted, and means that an OnItemClickListener gets called, provided that an OnClickListener is not set in getView().
I'm assuming that anything you can do in an View's OnClickListener you can do just as easily in a ListView's OnItemClickListener.
(setOnClickListener on a View implicitly sets the view to be clickable, which prevents the ListView's corresponding OnItemClickListener getting called, apparently.)
The behaviour is as one would expect, in terms of the ImageButton's visual state when the item is pressed or rolled over.
The solution is a slight illusion, in that it is the list item that's being pressed not the ImageButton itself, so if the button doesn't occupy whole list item, clicking somewhere else in the item will still make the button's drawable state reflect the click. Same for focus. That might be a price worth paying.
This will definitely work. Add this to the layout definition.
android:descendantFocusability="blocksDescendants"
Found the solution here
One alternative to setting an OnClickListener for every view is to NOT use an ImageButton - use an ImageView instead. The ImageView can still send events to an OnClickListener and won't take over the focus.
best way to do is this:
android:focusable="false"
android:focusableInTouchMode="false"
set these properties for that Imagebutton and try.
I
For my version of this problem, the issue was that I had set my TextView object to android:inputType="textMultiLine". When I removed this line the issue of the list not being clickable was gone. Looks like a nasty little bug.
Also, I'm still able to use the android:minLines/android:maxLines properties with no problem, so it's not a big issue. Just not the solution I expected.
The following line solved the issue in my project:
<TextView ... android:textIsSelectable="false" />
As an alternative solution which worked for me you can try to extend your adapter from BaseAdapter (iso implementing ListAdapter interface)
Put This code ImageView nextpage= (ImageView)findViewById(R.id.btnEdit); instead of ImageButton . now the list item is active
I have sub-classed ImageButton and setFocusable="false" in layout definition didn't work for me. It solved calling setFocusable(false) in constructor of subclass.
Using a ScrollView can prevent the onItemClickListener from receiving the input.
Hope this helps anyone.