java - ViewPager 无法正常显示

标签 java android

我正在尝试这样做,

enter image description here

我所做的是,

enter image description here

我在ListView中尝试过Horizo​​ntalScrollView 但无法正确获取。

任何帮助将不胜感激。

这是我的 xml

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

                    <com.android.ViewPagerContainer
                        android:id="@+id/container"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent">

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

                    </com.android.ViewPagerContainer>
                </LinearLayout>

我的代码是

  private class ViewAdapter extends PagerAdapter {

    private int count = values.size();

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

    public void setCount(int count) {
        this.count = count;
    }

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

    @Override
    public LinearLayout instantiateItem(ViewGroup container, int position) {
        LinearLayout llViewOuter = new LinearLayout(getActivity());
        llViewOuter.setOrientation(LinearLayout.VERTICAL);

        ImageView thumb_image = new ImageView(getActivity());
        thumb_image.setImageResource(R.drawable.optionnor);
        thumb_image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        llViewOuter.addView(thumb_image);

        TextView tmpT = new TextView(getActivity());
        tmpT.setText(values.get(position));
        tmpT.setGravity(Gravity.CENTER);
        llViewOuter.addView(tmpT);

        final ImageView checkBox = new ImageView(getActivity());
        checkBox.setImageResource(R.drawable.optionnor);
        checkBox.setAdjustViewBounds(true);
        checkBox.setScaleType(ImageView.ScaleType.FIT_CENTER);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //checkbox2.setImageResource(R.drawable.option);
                checkBox.setImageResource(R.drawable.optionselected);
            }
        });
        llViewOuter.addView(checkBox);


        ((ViewPager) container).addView(llViewOuter);

        return llViewOuter;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        ((ViewPager) container).removeView((LinearLayout) object);
    }

}

最佳答案

您可以使用此水平ListView库https://github.com/EmirWeb/parchment .

public class ListView<ADAPTER extends Adapter> extends AbstractAdapterView<ADAPTER,View>         
implements OnLongClickListener, OnClickListener,
OnSelectedListener, AdapterViewHandler {

public ListView(final Context context) {
    super(context);
}

public ListView(final Context context, final AttributeSet attributeSet) {
    super(context, attributeSet);
}

public ListView(final Context context, final AttributeSet attributeSet, final int defStyle) {
    super(context, attributeSet, defStyle);
}

@Override
protected AdapterViewInitializer<View> getAdapterViewInitializer(final Context context, final AttributeSet attributeSet) {
    final Attributes attributes = new Attributes(context, attributeSet);

    final boolean isViewPager = attributes.isViewPager();
    final boolean isVerticalScroll = attributes.isVertical();
    final int cellSpacing = (int) attributes.getCellSpacing();
    final boolean isCircularScroll = attributes.isCircularScroll();
    final boolean snapToPosition = attributes.isSnapToPosition();
    final int viewPagerInterval = attributes.getViewPagerInterval();
    final SnapPosition snapPosition = attributes.getSnapPosition();
    final boolean selectOnSnap = attributes.selectOnSnap();
    final boolean selectWhileScrolling = attributes.selectWhileScrolling();
    final LayoutManagerAttributes layoutManagerAttributes = new LayoutManagerAttributes(isCircularScroll, snapToPosition, isViewPager, viewPagerInterval, snapPosition, cellSpacing, selectOnSnap, selectWhileScrolling, isVerticalScroll);

    final AdapterViewManager adapterViewManager = new AdapterViewManager();
    final ListLayoutManager listLayoutManager = new ListLayoutManager(this, this, adapterViewManager, layoutManagerAttributes);

    final AdapterViewInitializer<View> adapterViewAdapterViewInitializer = createAdapterViewInitializer(context, isViewPager, adapterViewManager, listLayoutManager, isVerticalScroll);
    return adapterViewAdapterViewInitializer;
}

}

希望你喜欢。

关于java - ViewPager 无法正常显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27948220/

相关文章:

java - 在 Eclipse 中停止 vertx verticle

android - 这发生在什么时候?为什么? Android App Activity 正在关闭 2.3.3 模拟器并出现错误

javascript - ajax 函数成功事件时关闭 webview

android - 当我选择了 View 并向下滑动状态栏时,当我再次将其抬起时未选择 View

java - 在不启动新线程的情况下调用 run 方法

java - java中获取资源(jar)路径

java - 当我单击带有滑动菜单的菜单图标时崩溃

android - 错误 : Error: No resource found that matches the given name: attr 'colorAccent'

android - 人脸识别使用Opencv4Android SDK教程?

java - 如何设置转换为字母的符号列表?