I implemented OnScrollListener interface in ListFragment, and i want to change text when last element of list is visible but it doesn't work. I didn't find example of similar problem(OnScrollListener inside ListFragment). My example:
public class MyListFragment1 extends ListFragment implements OnScrollListener {
public View view;
public class MyListAdapter extends ArrayAdapter<String> {
Context myContext;
List<String> lista;
public MyListAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
myContext = context;
lista = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
LayoutInflater inflater =
(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.month);
label.setText(lista.get(position));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
//Customize your icon here
icon.setImageResource(R.drawable.ic_launcher);
return row;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
ListAdapter myListAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
month);
setListAdapter(myListAdapter);
*/
List<String> lista = new ArrayList<String>();
lista.add("January");
lista.add("February");
lista.add("March");
lista.add("April");
lista.add("May");
lista.add("June");
lista.add("July");
lista.add("August");
lista.add("September");
lista.add("October");
lista.add("November");
lista.add("December");
MyListAdapter myListAdapter =
new MyListAdapter(getActivity(), R.layout.row, lista);
setListAdapter(myListAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.listfragment1, container, false);
changeText("jestem tutaj");
return view;
}
private void changeText(String txt){
TextView nowy = (TextView)view.findViewById(R.id.article);
nowy.setText(txt);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(
getActivity(),
getListView().getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (++firstVisibleItem + visibleItemCount > totalItemCount) {
changeText("something");
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
I don't have any error, it just doesn't work ;)
P.S implementation of onScroll method i found in StackOverflow.
EDIT:/ adding logcat:
10-14 01:11:34.944: E/AndroidRuntime(817): FATAL EXCEPTION: main
10-14 01:11:34.944: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exercise.AndroidListFragment/com.exercise.AndroidListFragment.AndroidListFragmentActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.os.Looper.loop(Looper.java:137)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 01:11:34.944: E/AndroidRuntime(817): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 01:11:34.944: E/AndroidRuntime(817): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 01:11:34.944: E/AndroidRuntime(817): at dalvik.system.NativeStart.main(Native Method)
10-14 01:11:34.944: E/AndroidRuntime(817): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.setContentView(Activity.java:1881)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.exercise.AndroidListFragment.AndroidListFragmentActivity.onCreate(AndroidListFragmentActivity.java:11)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.performCreate(Activity.java:5104)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-14 01:11:34.944: E/AndroidRuntime(817): ... 11 more
10-14 01:11:34.944: E/AndroidRuntime(817): Caused by: java.lang.IllegalStateException: Content view not yet created
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ListFragment.ensureList(ListFragment.java:386)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ListFragment.getListView(ListFragment.java:280)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.exercise.AndroidListFragment.MyListFragment1.onCreateView(MyListFragment1.java:93)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Fragment.performCreateView(Fragment.java:1695)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.onCreateView(Activity.java:4717)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-14 01:11:34.944: E/AndroidRuntime(817): ... 21 more
EDIT 2: I have by tried findViewById but it doesn't work too....