简体   繁体   中英

Show two item in ViewPager android

I'm developing sample tutorials from this website .

On this tutorial, it's only showing one item per ViewPager , but I want to show two items. After i have some customized, it is showing two items in one pager. But both views are the same. I don't want it. I want to show different views on screen.

Thanks.

TestingActivity.java

public class TestingActivity extends ActionBarActivity {
    // Declare Variables
    ViewPager viewPager;
    PagerAdapter adapter;
    String[] rank;
    String[] country;
    String[] population;
    int[] flag;
    CirclePageIndicator mIndicator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from viewpager_main.xml
        setContentView(R.layout.viewpager_main);

        // Locate the ViewPager in viewpager_main.xml
        viewPager = (ViewPager) findViewById(R.id.pager);
        // Pass results to ViewPagerAdapter Class
        adapter = new ViewPagerAdapter(TestingActivity.this, rank, country,
                population, flag);
        // Binds the Adapter to the ViewPager
        viewPager.setAdapter(adapter);

        // ViewPager Indicator
        mIndicator = (CirclePageIndicator) findViewById(R.id.indicator_pager);
       //mIndicator.setFades(false);
        mIndicator.setViewPager(viewPager);

    }
}

viewpager_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator_pager"
        android:layout_width="fill_parent"
        android:layout_height="5dp" />

</LinearLayout>

I finally found the solution. But it scroll one items. Like this List1, List2| List2, List3| List3, List4|.

@Override
public float getPageWidth(int position) {
    return(0.5f);
}

I did something like this and it worked:

viewPager = findViewById<ViewPager> (R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter (this, listOfImages));
viewPager.setOffscreenPageLimit(6);
viewPager.setPageMargin(3);
viewPager.setPageMarginDrawable (new ColorDrawable(Color.parseColor("#000000")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM