android中的图库类型滚动视图

时间:2013-12-31 14:35:46

标签: android android-gallery

我正在尝试在下图中实现类似于show的滚动条。 enter image description here

我尝试过使用viewpager但它一次只显示一个项目。我需要展示其中5种不同尺寸。中间的那个需要更大。

每个Item都是一个包含ImageView和TexView的frameLayout,我在实现该部分时没有任何问题。问题是它需要是一个卷轴并且在滚动条中有许多项目,例如最多15个。但是在任何时候都应该只有5个项目可见,如下所示。我尝试了很多实现。请有人给我一个工作实例,因为我已经尝试了很多例子,但是没有一个能够完美地运作。我已经在这一周停了一个多星期。

1 个答案:

答案 0 :(得分:1)

您可以通过覆盖PagerFragmentAdapter中的getPageWidth()来控制它:

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

并确保图片尺寸不会太大,以便页面宽度适合多张图片。

enter image description here

以下是设置此步骤的所有步骤:

1)将片段容器添加到您的活动布局中,您将在其中加载PhotoPagerFragment:

<!-- PHOTO PAGER FRAGMENT -->
<FrameLayout
    android:id="@+id/photoPagerFragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:tag="sticky" 
    android:layout_gravity="center_horizontal" >
</FrameLayout>

2)在活动的onCreate()

中注入PhotoPagerFragment
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        //Insert the fragment
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.photoPagerFragmentContainer);

        if (fragment == null) {
            fragment = new PhotoPagerFragment();
            fm.beginTransaction()
                .add(R.id.photoPagerFragmentContainer, fragment)
                .commit();
        }
    }

3)为PhotoPagerFragment创建一个布局:

<?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:background="@android:color/black"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/photoPager"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_marginTop="2dp"/>

</LinearLayout>

4)创建PhotoPagerFragment:

public class PhotoPagerFragment extends Fragment {

    private ViewPager       mPhotoPager;
    private PagerAdapter    mPhotoAdapter;

    public static final String TAG = "PhotoPagerFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_pager, container, false);

        mPhotoAdapter = new PhotoPagerFragmentAdapter(getActivity().getSupportFragmentManager());

        mPhotoPager = (ViewPager) view.findViewById(R.id.photoPager);
        mPhotoPager.setAdapter(mPhotoAdapter);

        return view;
    }
}

5)适配器:

public class PhotoPagerFragmentAdapter extends FragmentPagerAdapter {

    private int[] Images = new int[] { 
            R.drawable.photo_1, R.drawable.photo_2,
            R.drawable.photo_3, R.drawable.photo_4,
            R.drawable.photo_5, R.drawable.photo_6
    };

    private int mCount = Images.length;


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

    @Override
    public Fragment getItem(int position) {
        return PhotoDetailsFragment.newInstance(Images[position]);
    }

    @Override
    public int getCount() {
        return mCount;
    }

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

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }
}

6)最后,你的PhotoDetailsFragment会显示每张图片:

public final class PhotoDetailsFragment extends Fragment {

    private int photoResId;

    private static final String TAG = "PhotoDetailsFragment";
    public  static final String EXTRA_PHOTO_ID = "com.sample.photo_res_id";


    public static PhotoDetailsFragment newInstance(int photoResId) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_PHOTO_ID, photoResId);

        PhotoDetailsFragment fragment = new PhotoDetailsFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        photoResId = (Integer)getArguments().getSerializable(EXTRA_PHOTO_ID);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final ImageView image = new ImageView(getActivity());
        image.setImageResource(photoResId);

        // Hook up the clicks on the thumbnail views
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(-1, -1));

        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;
    }
}