1

所以在查看了来自 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" />

忽略这一切的一些混乱,它有点匆忙。

任何关于它为什么不滚动的帮助将不胜感激。

4

3 回答 3

1

我现在正在构建一个应用程序,它做同样的事情......但它有很多额外的东西你必须整理出来......你想要那个代码吗?

于 2013-08-11T19:15:22.083 回答
1

适配器代码

包源代码.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);




    }


}
于 2013-08-11T19:21:28.690 回答
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);
}
}
于 2013-08-11T19:28:18.703 回答