3

我在 ListFragment 中实现了 OnScrollListener 接口,我想在列表的最后一个元素可见但它不起作用时更改文本。我没有找到类似问题的示例(ListFragment 中的 OnScrollListener)。我的例子:

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

}

我没有任何错误,它只是不起作用;)

我在 StackOverflow 中找到的 onScroll 方法的 PS 实现。

编辑:/添加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

编辑 2:我试过 findViewById 但它也不起作用....

4

2 回答 2

10

您已经实现了 OnScrollListener,但还没有将它附加到您的 ListView。

getListView().setOnScrollListener(this);

将此添加到您的 onCreateView 方法中。

于 2013-10-13T22:49:30.103 回答
6

您遇到此错误是因为您在尚未创建此列表视图时尝试访问您的列表视图。

原因:java.lang.IllegalStateException:尚未创建内容视图

您的 Listview 实际上是由 onCreateView 方法的最后一行创建的,该行通常如下所示:

return inflater.inflate(R.layout.fragment_list, container, false);

解决方案是从 onActivityCreated 方法访问您的列表,该方法在 onCreateView 方法之后执行。

    @Override
    public void onActivityCreated (Bundle savedInstanceState){
        // Always call the superclass so it can save the view hierarchy state
        super.onCreate(savedInstanceState);

        getListView().setOnScrollListener(this);
    }
于 2014-03-27T19:21:33.437 回答