所以在查看了来自 viewpagerindicator 的样本后,我意识到它们都是片段形式的。
我想在没有片段的情况下执行此操作,但在编译后它会显示第一个布局但不会滑动到其他视图。
我正在查看“SampleIconsDefault.java”(来自示例)并自己制作并认为我做对了,但我不知道为什么它不滑动。
活动和适配器:
package russellrargill.work.burghcoffeehouseapp;
import com.viewpagerindicator.IconPageIndicator;
import com.viewpagerindicator.IconPagerAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
//variables for the pager
ViewPager thePager;
IconPageIndicator theIconPageIndicator;
MyPagerAdapter theAdapter;
private Context cxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
//set the context
cxt = this;
theAdapter = new MyPagerAdapter();
thePager = (ViewPager)findViewById(R.id.the_pager);
thePager.setAdapter(theAdapter);
theIconPageIndicator = (IconPageIndicator)findViewById(R.id.icon_page_indicator);
theIconPageIndicator.setViewPager(thePager);
}//END onCreate()
class MyPagerAdapter extends PagerAdapter implements IconPagerAdapter {
String[] titles = new String[]{"Home","Menu","Stores","About Us","Contact"};
protected final int[] ICONS = new int[] {
R.drawable.perm_group_calendar,
R.drawable.perm_group_camera,
R.drawable.perm_group_device_alarms,
R.drawable.perm_group_location
};
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.home_layout, null);
collection.addView(layout,0);
//TODO add layouts for each page
layout = inflater.inflate(R.layout.pagelayoot, null);
collection.addView(layout,1);
layout = inflater.inflate(R.layout.page2layoot, null);
collection.addView(layout,2);
return layout;
}
private int mCount = titles.length;
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position % titles.length];
}
@Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0==arg1);
}
}
}//END CLASS
主要的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|top"
android:background="#000000"
android:clickable="true"
android:focusableInTouchMode="false"
android:padding="1dp"
tools:context=".HomeScreen" >
<LinearLayout
android:id="@+id/nav_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusableInTouchMode="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<com.viewpagerindicator.IconPageIndicator
android:id="@+id/icon_page_indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<android.support.v4.view.ViewPager
android:id="@+id/the_pager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nav_layout" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="banana\nbabababba\newfewfewfg\nwefewf"
android:textColor="#FFFFFF"
/>
</LinearLayout>
</ScrollView>
其他 2 个布局(几乎相同,所以我只发布一个)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:text="TEST"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_toLeftOf="@+id/textView4"
android:text="A"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/textView3"
android:text="IS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="98dp"
android:layout_toLeftOf="@+id/textView2"
android:text="THIS"
android:textAppearance="?android:attr/textAppearanceLarge" />
忽略这一切的一些混乱,它有点匆忙。
任何关于它为什么不滚动的帮助将不胜感激。