如何制作水平可滚动视图?

How to make a horizontal scrollable view?

我有一个包含线性布局的水平滚动视图。 像这样:

<HorizontalScrollView
    android:fillViewport="true"
    android:id="@+id/product_hsv"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:measureAllChildren="false"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/product_container_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal" />
</HorizontalScrollView>

What I want to do is to add n number of the same layout to display product collection in a Horizontal scroll view.

下面的图片会让你了解我到目前为止所做的事情。

为此,我已将产品集合视图添加到 R.id.product_container_layout

String[] productCollectionArr = new String[]{"Shirt", "Jewellary", "Moto X Play", "Ellis Flat"};

for (int i = 0; i < productCollectionArr.length; i++) {
    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.product_container_layout);
    View child = getLayoutInflater().inflate(R.layout.item_product, null);//child.xml
    parentLayout.addView(child);
}

for (int i = 0; i < productCollectionArr.length; i++) {
    LinearLayout eachProductLayout = (LinearLayout) findViewById(R.id.product_container_layout);
    ((TextView) eachProductLayout.findViewById(R.id.product_name_tv)).setText(productCollectionArr[i]);
}

但是 问题是传递值。由于添加到 product_container_layout 的每个视图都具有相同的 ID。因此只有第一个元素从产品集合数组中获取值。

我能做的是为每个视图及其元素生成视图 ID,并将这些 ID 映射到特定名称,以便我可以通过 ID 访问它们。

这是正确的方法还是我应该做其他事情?

You can use **DevSmart** library for horizontal listview which is similar to **ListView**.

    https://github.com/dinocore1/DevsmartLib-Android

因此,您需要在添加到容器或保存指向所有项目的链接之前填充您的视图。它看起来像这样:

    LinearLayout parentLayout=(LinearLayout)findViewById(R.id.linearLayout1);
for (int i = 0; i < items.size(); i ++) {
    View child = getLayoutInflater().inflate(R.layout.item_grid, null);//child.xml
((TextView) child.findViewById(R.id.name)).setText(items.get(i).getName(); // where R.id.name is a textView in your child.xml layout.
    parentLayout.addView(child);
}

毕竟,您可以通过索引访问您的元素,如果您想更改它:

parentLayout.getChildAt(index)

但是如果你有很多物品,这不是个好主意。在这种情况下,您将不会回收您的视图,并且会消耗大量内存。 我建议使用支持库中的 recyclerview

You need to add this  first :

 <HorizontalScrollView
                android:id="@+id/galleryImages"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/chim_image_view_divider"
                android:layout_marginTop="@dimen/twenty"
                android:focusable="false"
                android:layout_centerInParent="true"
                android:focusableInTouchMode="false"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/Images_scroller_view_linear_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal" >
                </LinearLayout>
            </HorizontalScrollView>

ImageScrollerViewLinearLayout = (LinearLayout) findViewById(R.id.Images_scroller_view_linear_layout);
        ImageScrollerViewLinearLayout.removeAllViews();

 - addImagesTo list:

void addImages() {
        ImageScrollerViewLinearLayout.removeAllViews();

        int i = 0;
        final float scale = this.getResources().getDisplayMetrics().density;
        int pixels = 0;
        if (myImagesList != null && myImagesList.size() > 0) {
            for (i = 0; i < myImagesList.size(); i++) {

                ImageView imageView = new ImageView(this);

                pixels = (int) (60 * scale + 0.5f);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pixels, pixels);
                params.setMargins(5, 0, 0, 0);
                imageView.setLayoutParams(params);
                imageView.setScaleType(ScaleType.CENTER_CROP);
                imageView.setOnClickListener(new SelectNewSingleImageClickListener());
                imageView.setImageResource(myImagesList.get(i));
                ImageScrollerViewLinearLayout.addView(imageView, i);

            }

        } 
    }
add code in xml file
<org.lucasr.twowayview.TwoWayView
                    android:id="@+id/lvItems"
                    style="@style/TwoWayView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_toLeftOf="@+id/linearLayout3"
                    android:cacheColorHint="@android:color/transparent"
                    android:divider="@android:color/transparent"
                    android:drawSelectorOnTop="false"
                    android:padding="10dp" >
                </org.lucasr.twowayview.TwoWayView>