在查看来自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" />
忽略这一切的一些混乱,它有点匆忙。
非常感谢任何关于它为什么不滚动的帮助。
答案 0 :(得分:1)
我正在构建一个应用程序,它做了同样的事情....但它有很多额外的东西你需要解决它...你想要代码吗?
答案 1 :(得分:1)
适配器的代码
package sourcecode.jazzplayer;
import java.util.List;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
public class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
private static String[] titles = new String[] {"Songs", "My Playlists", "Artists","Albums"};
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public String getPageTitle( int position )
{
return titles[ position ];
}
}
创建片段的活动的代码
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MyMusic extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mymusic);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, FragmentSongs.class.getName()));
fragments.add(Fragment.instantiate(this, FragmentArtists.class.getName()));
fragments.add(Fragment.instantiate(this, FragmentPlaylists.class.getName()));
fragments.add(Fragment.instantiate(this, FragmentAlbums.class.getName()));
ViewPagerAdapter adapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(adapter);
pager.setOffscreenPageLimit(4);
pager.setCurrentItem(0);
}
}
答案 2 :(得分:1)
其中一个片段......这些片段必须与您创建的每个片段无关.......并记下我的适配器可能与您的不同我在适配器中进行了更改我不必一次又一次地使用“背景”的东西......
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentPlaylists extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
}