ViewPager
拡張子の を使用していPagerAdapter
ます。関係者はいませんFragments
。私はViewPager
定期的に使用していViews
ます。ビューをスワイプすると、ビューがエンドツーエンドで接続/接続されたままにならないことに気付きました。まるで無限大のマージンです。(スワイプ中の動的な状況のみで、開始または終了の状況に問題はありません。)
次のページをすぐにスワイプするにはどうすればよいですか?
明確にするために:私は見ています
代わりに、次のことを確認したいと思います。
タブ付きのアクション バー、FragmentPagerAdapter、そして ViewPager を Fragments と共に使用すると、この正しい動作が行われることに気付きました。それはまた時々ギャップを残しますが。
これが私のコードです:
HelloTabWidget.java :
package com.example.hellotabwidget;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class HelloTabWidget extends Activity {
private TabHost mTabHost;
TabsAdapter mTabsAdapter;
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_tab_widget);
findViews();
}
/**
*
*/
protected void findViews() {
mViewPager = (ViewPager) findViewById(R.id.realtabcontent);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
HorizontalScrollView mScrollView = (HorizontalScrollView) findViewById(R.id.sv);
TabWidget mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
mTabsAdapter = new TabsAdapter(this, mViewPager, mTabHost, mTabWidget, mScrollView);
TabHost.TabSpec tab;
tab = mTabHost.newTabSpec("tab_test1")
.setIndicator("TABLATURE_1_IT_IS").setContent(R.id.textview1);
mTabsAdapter.addTab(tab, R.id.textview1, "tab_test1", null);
tab = mTabHost.newTabSpec("tab_test2")
.setIndicator("TABLATURE_2_IT_IS").setContent(R.id.textview2);
mTabsAdapter.addTab(tab, R.id.textview2, "tab_test2", null);
tab = mTabHost.newTabSpec("tab_test3")
.setIndicator("TABLATURE_3_IT_IS").setContent(R.id.textview3);
mTabsAdapter.addTab(tab, R.id.textview3, "tab_test3", null);
tab = mTabHost.newTabSpec("tab_test4")
.setIndicator("TABLATURE_4_IT_IS").setContent(R.id.textview4);
mTabsAdapter.addTab(tab, R.id.textview4, "tab_test4", null);
tab = mTabHost.newTabSpec("tab_test5")
.setIndicator("TABLATURE_5_IT_IS").setContent(R.id.textview5);
mTabsAdapter.addTab(tab, R.id.textview5, "tab_test5", null);
mTabsAdapter.finalizeTabs();
mTabHost.setCurrentTab(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_hello_tab_widget, menu);
return true;
}
/**
* This is a helper class that implements the management of tabs and all
* details of connecting a ViewPager with associated TabHost. It relies on a
* trick. Normally a tab host has a simple API for supplying a View or
* Intent that each tab will show. This is not sufficient for switching
* between pages. So instead we make the content part of the tab host 0dp
* high (it is not shown) and the TabsAdapter supplies its own dummy view to
* show as the tab content. It listens to changes in tabs, and takes care of
* switch to the correct paged in the ViewPager whenever the selected tab
* changes.
*/
public static class TabsAdapter extends PagerAdapter implements
ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener {
public static final class TabInfo {
private final Bundle args;
private final String tag;
private final int tabContentViewId;
TabInfo(Bundle args, String tag, int tabContentViewId) {
this.args = args;
this.tag = tag;
this.tabContentViewId = tabContentViewId;
}
public String getTag() {
return tag;
}
public int getTabContentViewId() {
return tabContentViewId;
}
}
private Activity mContext;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final ViewPager mViewPager;
private final TabHost mTabHost;
private final TabWidget mTabWidget;
private final HorizontalScrollView mScrollView;
private Runnable mTabSelector;
public TabsAdapter(Activity context, ViewPager pager, TabHost tabHost, TabWidget tabWidget, HorizontalScrollView scrollView) {
super();
mContext = context;
mTabHost = tabHost;
mTabHost.setOnTabChangedListener(this);
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
mTabWidget = tabWidget;
mScrollView = scrollView;
}
public void finalizeTabs() {
for (int i = 0; i < getCount(); i++) {
//mTabWidget.getChildAt(i).setFocusableInTouchMode(true);//TODO
}
//Keep everything in memory.
mViewPager.setOffscreenPageLimit(getCount());
}
public void addTab(TabHost.TabSpec tab, int tabContentViewId,
String tag, Bundle args) {
TabInfo info = new TabInfo(args, tag, tabContentViewId);
mTabs.add(info);
mTabHost.addTab(tab);
notifyDataSetChanged();
}
/**
* Determines whether a page View is associated with a specific key
* object as returned by instantiateItem(ViewGroup, int). This method is
* required for a PagerAdapter to function properly.
*
* @param view
* Page View to check for association with object
* @param object
* Object to check for association with view
* @return
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return ((object instanceof View) && (view.getId() == ((View)object).getId()))
|| (view == object);
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageSelected(int position) {
mTabHost.setCurrentTab(position);
animateToTab(position);
}
@Override
public void onTabChanged(String tabId) {
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i).getTag().equals(tabId)) {
mViewPager.setCurrentItem(i);
animateToTab(i);
}
}
}
public void destruct() {
mContext = null;
}
/**
* Remove a page for the given position. The adapter is responsible for
* removing the view from its container, although it only must ensure
* this is done by the time it returns from
* {@link #finishUpdate(android.view.ViewGroup)}.
*
* @param collection
* The containing View from which the page will be removed.
* @param position
* The page position to be removed.
* @param view
* The same object that was returned by
* {@link #instantiateItem(android.view.View, int)}.
*/
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((TextView) view);
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate(android.view.ViewGroup)}.
*
* @param collection
* The containing View in which the page will be shown.
* @param position
* The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the
* page.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = mContext.findViewById(mTabs.get(position)
.getTabContentViewId());
if(view==null) return null;//Might happen if ViewPager.setOffscreenPageLimit() is not big enough
ViewGroup formerParent = (ViewGroup)view.getParent();
if(formerParent!=null && formerParent.getId()==container.getId()){
return view;
}
if(formerParent!=null){
formerParent.removeView(view);
}
container.addView(view, 0, new ViewPager.LayoutParams());
return view;
}
/**
* Called when the a change in the shown pages has been completed. At
* this point you must ensure that all of the pages have actually been
* added or removed from the container as appropriate.
*
* @param arg0
* The containing View which is displaying this adapter's
* page views.
*/
@Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
}
private void animateToTab(final int position) {
final View tabView = mTabWidget.getChildAt(position);
if (mTabSelector != null) {
mScrollView.removeCallbacks(mTabSelector);
}
mTabSelector = new Runnable() {
public void run() {
final int scrollPos = tabView.getLeft()
- (mScrollView.getWidth() - tabView.getWidth()) / 2;
mScrollView.smoothScrollTo(scrollPos, 0);
mTabSelector = null;
}
};
mScrollView.post(mTabSelector);
}
}
}
activity_hello_tab_widget.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview50"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="some text" />
<TextView
android:id="@+id/textview60"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="some text" />
<TextView
android:id="@+id/textview70"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="some text" />
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|fill_vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:fadingEdge="horizontal"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" >
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
<TextView
android:id="@+id/textview4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a fourth tab" />
<TextView
android:id="@+id/textview5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a fifth tab" />
</FrameLayout>
<android.support.v4.view.ViewPager
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</TabHost>
</LinearLayout>