单活动中的Android多视图寻呼机未显示第二个视图寻呼机

时间:2016-08-08 06:59:49

标签: android

我在活动中有2个视图寻呼机

 <android.support.v4.view.ViewPager
        android:id="@+id/latestProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

 <android.support.v4.view.ViewPager
        android:id="@+id/popularProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

我创建了一个适配器

public class ProductSwipePageAdapter extends FragmentPagerAdapter {

List<Product> products;

public ProductSwipePageAdapter(FragmentManager fm) {
    super(fm);
}

public ProductSwipePageAdapter(FragmentManager fm, List<Product> products) {
    super(fm);
    this.products = products;
}


@Override
public Fragment getItem(int position) {
    return ProductCardFragment.newInstance(products.get(position));
}

@Override
public int getCount() {
    return products.size();
}}

我设置了寻呼机

  productSwipePageAdapter1 = new ProductSwipePageAdapter(getFragmentManager(), allProducts);
    latestProductSwipeCard.setAdapter(productSwipePageAdapter1);

productSwipePageAdapter2 = new ProductSwipePageAdapter(getFragmentManager(), allProducts1);
    popularProductSwipeCard.setAdapter(productSwipePageAdapter2);

结果它只显示一个视图寻呼机

enter image description here

2 个答案:

答案 0 :(得分:2)

如果未设置View Viewr的特定高度,则ViewPager访问所有高度

因此,如果您同时显示ViewPager而不是在XMl中设置Weight属性

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

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

答案 1 :(得分:0)

请参阅此示例为我工作

activity_main.xml中

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

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

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

fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment"/>

</RelativeLayout>

MainActivity.java

package com.stackoverflowtesting;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        ViewPager mViewPager1 = (ViewPager) findViewById(R.id.latestProductSwipeCard);
        mViewPager1.setAdapter(mSectionsPagerAdapter);


        // Set up the ViewPager with the sections adapter.
        ViewPager mViewPager2 = (ViewPager) findViewById(R.id.popularProductSwipeCard);
        mViewPager2.setAdapter(mSectionsPagerAdapter);
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}