如何在ViewPager Android中组合两个视图?

时间:2015-07-15 02:28:55

标签: android android-viewpager

我正在使用ViewPager创建CirclePageIndicator。内部有两个View。现在,它在一个页面中显示两个View。但当我滚动到下一个时,前一个显示。这意味着它显示像前一个+新。 目前看起来像:

  

ItemA,ItemB | ItemB,ItemC | ItemC,ItemD |

预计看起来像

  

ItemA,ItemB | ItemC,ItemD | ItemE,ItemF |

我该如何解决? 有没有办法将这两个ItemA和ItemB结合起来?

这是我的代码。

适配器

public class ViewPagerAdapter extends PagerAdapter  {
    // Declare Variables
    Context context;
    ViewPager pager;
    String[] domain;
    String[] title;
    int[] flag;
    LayoutInflater inflater;

    public ViewPagerAdapter(ViewPager pager,Context context, String[] domain,
                            String[] title, int[] flag) {
        this.pager= pager;
        this.context = context;
        this.domain = domain;
        this.title = title;
        this.flag = flag;
    }


    @Override
    public int getCount() {
    //Show the CirclePageIndicator for only half of array.
        return (int) Math.ceil((double)title.length/2);
//      return title.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }



    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.related_article_item, container,
                false);

        // Locate the TextViews in viewpager_item.xml
        final Button btnSave = (Button) itemView.findViewById(R.id.btn_save);
        final Button btnLike = (Button) itemView.findViewById(R.id.btn_like);
        final TextView tv_domain = (TextView) itemView.findViewById(R.id.domain_text);
        TextView tv_title = (TextView) itemView.findViewById(R.id.title_text);
        ResizableImageView imageView = (ResizableImageView) itemView.findViewById(R.id.imageViewDynamic);
        final ProgressBar progressBar = (ProgressBar) itemView.findViewById(R.id.loading);

        // Capture position and set to the TextViews
        tv_domain.setText(domain[position]);
        tv_title.setText(title[position]);

        // Locate the ImageView in viewpager_item.xml
        imageView.setImageResource(flag[position]);

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        return itemView;

    }

    //This is the code to show two view inside one page.
    @Override
    public float getPageWidth(int position) {
        return (0.5f);
    }
//        return (super.getPageWidth(position)  / 2);


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }

}

MainActivity

public class TestingActivity extends ActionBarActivity {
        // Declare Variables
        ViewPager viewPagerRelated;
        PagerAdapter adapter;
        String[] title;
        String[] domain;
        int[] flag;
        ViewPager pager1;
        CirclePageIndicator mIndicator;

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

            title = new String[]{"Sample1", "Sample2", "Sample3",
                "Sample4", "Sample5", "Sample6", "Sample7", "Sample8",
                "Sample9", "Sample10"};

            domain = new String[]{"text1", "text1",
                "text1", "text1", "text1", "text1",
                "text1", "text1", "text1", "text1"};

            flag = new int[]{R.drawable.arsenal, R.drawable.aston_villa,
                R.drawable.manchester_city, R.drawable.liverpool,
                R.drawable.chelsea, R.drawable.manchester_united, R.drawable.swansea,
                R.drawable.liverpool, R.drawable.west_brom, R.drawable.west_ham};

            // Locate the ViewPager in viewpager_main.xml
            viewPagerRelated = (ViewPager) findViewById(R.id.pager2);
            // Pass results to ViewPagerAdapter Class
            adapter = new ViewPagerAdapter(pager1, ArticleViewActivityV2.this, domain,
                title, flag);
            // Binds the Adapter to the ViewPager
            viewPagerRelated.setAdapter(adapter);
            // viewPagerRelated.setPageMargin(3);
            // ViewPager Indicator
            mIndicator = (CirclePageIndicator) findViewById(R.id.indicator_pager);
            mIndicator.setViewPager(viewPagerRelated);    
        }
    }

XML布局

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

    <include
        layout="@layout/related_article_item"
        android:id="@+id/article_1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <include
        layout="@layout/related_article_item"
        android:id="@+id/article_2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

项目XML

<?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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:background="@drawable/gridview_item_selector"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp">

        <RelativeLayout
            android:id="@+id/imageLayout"
            android:layout_width="200dp"
            android:layout_height="100dp">

            <com.bindez.news.utils.ResizableImageView
                android:id="@+id/imageViewDynamic"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:src="@drawable/app_icon" />

            <ProgressBar
                android:id="@+id/loading"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:padding="10dp"
                android:visibility="visible" />
        </RelativeLayout>

        <TextView
            android:id="@+id/title_text"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/imageLayout"
            android:layout_marginTop="7dp"
            android:maxLines="10"
            android:paddingLeft="7dp"
            android:paddingRight="7dp"
            android:paddingTop="7dp"
            android:text="Title Text"
            android:textColor="@color/tile_text"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/domain_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title_text"
            android:layout_marginBottom="3dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:paddingBottom="7dp"
            android:paddingLeft="7dp"
            android:paddingRight="7dp"
            android:text=""
            android:textColor="@color/text_gray"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/seperator"
            android:layout_width="fill_parent"
            android:layout_height="1px"
            android:layout_below="@+id/domain_text"
            android:background="@color/light_gray_seperator" />


        <Button
            android:id="@+id/btn_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/seperator"
            android:background="@android:color/transparent"
            android:drawableRight="@drawable/ic_unsave"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:paddingBottom="5dp"
            android:paddingRight="7dp"
            android:paddingTop="5dp" />

        <Button
            android:id="@+id/btn_like"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/seperator"
            android:background="@android:color/transparent"
            android:drawableLeft="@drawable/ic_unlike"
            android:drawablePadding="5dp"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:paddingBottom="5dp"
            android:paddingLeft="7dp"
            android:paddingRight="7dp"
            android:paddingTop="5dp"
            android:text=""
            android:textColor="@color/btn_green"
            android:textSize="14sp" />

    </RelativeLayout>


</RelativeLayout>

0 个答案:

没有答案