0

我目前正在尝试实现类似画廊的视图。由于它已被弃用,我一直在尝试寻找替代方案。我已经在下面的链接中使用 Dave Smith 的视图寻呼机实现成功地试验了 Universal Image Loader 的图像视图寻呼机。http://commonsware.com/blog/2012/08/20/multiple-view-viewpager-options.html结果是一个类似画廊的小部件。

编辑 我目前的问题是 onclicklistener 似乎只在焦点页面激活。我试图将 onclicklistener 设置为 viewPager,但问题仍然存在。任何帮助和建议表示赞赏。

这是我的代码片段:

(更新)我的活动:

public class PagerActivity extends Activity {

PagerContainer mContainer;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mContainer = (PagerContainer) findViewById(R.id.pager_container);

    ViewPager pager = mContainer.getViewPager();
    PagerAdapter adapter = new MyPagerAdapter();
    pager.setAdapter(adapter);
    //Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    pager.setOffscreenPageLimit(adapter.getCount());
    //A little space between pages
    pager.setPageMargin(15);

    //If hardware acceleration is enabled, you should also remove
    // clipping on the pager for its children.
    pager.setClipChildren(false);
}

//Nothing special about this adapter, just throwing up colored views for demo
private class MyPagerAdapter extends PagerAdapter {

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View page=getLayoutInflater().inflate(R.layout.page, container, false);
        TextView tv = (TextView)page.findViewById(R.id.pagetext);
        tv.setText("Item "+position);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));


        final int cPos = position;
        tv.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(PagerActivity.this,"Page at: "+String.valueOf(cPos) , Toast.LENGTH_LONG)
                   .show();
            }

        });

        container.addView(page);
        return page;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

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

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

布局文件

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

<com.example.pagercontainer.PagerContainer
    android:id="@+id/pager_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#CCC">
    <android.support.v4.view.ViewPager
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal" />
</com.example.pagercontainer.PagerContainer>

</RelativeLayout>
4

1 回答 1

0

对我有用的是这条线:

final int cPos = position;

我一直在试图弄清楚如何setOffscreenPageLimit()与获得重点项目位置相结合。因此,将此变量设为最终变量,然后将其传递给 onclicklistener 它将显示正确的项目。

于 2013-09-26T13:16:25.200 回答