使用addFooterView时不显示进度条

时间:2014-12-19 17:26:21

标签: android android-listfragment android-progressbar

我尝试在列表视图的底部显示进度条。

当我滚动到列表底部时,会加载更多项目并显示进度条,加载完成后会隐藏进度条。

但此刻我仍然坚持显示进度条(不关心显示或隐藏它,因为它们很容易处理)。我在StackOverflow上应用了很多解决方案,但它们没有用。

这是我在列表片段中添加页脚视图的方法:

public class ItemListFragment extends ListFragment {
    protected View footerView;

    public ItemListFragment() {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final LayoutInflater li = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.footerView = li.inflate(R.layout.progress_bar, null);
        getListView().addFooterView(this.footerView);
    }
}

我的progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我的fragment_item_list.xml是Android Studio创建的默认文件(我发布以备不时之需)

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

如果您有解决方案,请发布您的答案。谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我通过切换到使用Fragment而不是ListFragment解决了这个问题。

我的片段:

public class TestFragment extends Fragment {
    static final String[] numbers = new String[] { "one", "two", "three",
            "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
            "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
            "seventeen", "eighteen", "nineteen", "twenty", "twenty one",
            "twenty two" };

    public TestFragment() {

    }

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

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.test_list_view, container, false);

        ListView listView = (ListView) rootView.findViewById(R.id.test_list_view);

        View footer = View.inflate(getActivity().getApplicationContext(), R.layout.test_footer, null);
        listView.addFooterView(footer);

        ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, numbers);

        listView.setAdapter(adapter);

        return rootView;
    }
}

test_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/test_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

test_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <ProgressBar
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

</LinearLayout>