我使用一个fragment活动来保存一个呈现一堆产品的列表片段:

public class ProductsListActivity extends SherlockFragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Sherlock___Theme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_products_list);
        // setFastScrollEnabled(true) ?
    }
}


...

<!-- fragment_products_list.xml -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.example.fragment.ProductsListFragment"
    android:id="@+id/list_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


...

public class ProductsListFragment extends SherlockListFragment {

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // setFastScrollEnabled(true) ?
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ...
    }
}


我想在enable fast scrolling中找到产品列表,并找到了另一篇描述how to do this for a list activity的帖子。但是如何激活片段的快速滚动? define this in XML或通过代码都可以。

最佳答案

SherlockListFragment还具有从ListFragment继承的getListView()方法。

getListView().setFastScrollEnabled(true);


这就是ListFragment内部的样子:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ArrayList<String> strings = new ArrayList<String>();

    for(int i = 0; i < 300; i++) strings.add("Item " + i);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, strings);
    setListAdapter(adapter);

    getListView().setFastScrollEnabled(true);
}

07-24 09:31