Load contents on demand in ViewPager
When i open a page in ViewPager, it instantiates the n开发者_运维问答eighboring pages. What I would like to achieve is
1.load the contents the page only when the page is on focus.
2.show a loading screen, while i populate the page layout which is on focus and replace the loading screen with page layout after that.
Can someone point me in the right direction to achieve these 2 features?
I'm not sure of a good way to solve your first question (I don't believe you can control which pages get loaded, and in which order), but I have something that can help for your second:
Here is an example of a view I created that is loaded in every view of my ViewPager:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:id="@+id/loading_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
style="@style/GenericProgressIndicator"/>
<TextView
android:id="@+id/loading_message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading_message_text"/>
</LinearLayout>
<WebView
android:id="@+id/webview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:fadingEdge="vertical"
android:visibility="gone"
/>
</RelativeLayout>
There are two elements nested in the RelativeLayout: a LinearLayout that represents my loading message (including a ProgressBar and a TextView) and a WebView that will hold my content once it is loaded. The WebView initially has its visibility set to GONE to completely hide it. In code, I inflate the view and start an AsyncTask to load my data. Once the data is loaded, I set the LinearLayout's visibility to GONE and the WebView's visibility to VISIBLE.
onPostExecute in my AsyncTask - after the data is loaded, hide the loading message and show the data
protected void onPostExecute(Void v)
{
LinearLayout loadingMessage = (LinearLayout)articleLayout.findViewById(R.loading_message);
WebView articleContent = (WebView)articleLayout.findViewById(R.id.webview);
[...]
loadingMessage.setVisibility(View.GONE);
articleContent.setVisibility(View.VISIBLE);
}
精彩评论