[英]How to make a horizontal scrollable view?
I have a horizontal scroll view containing a linear layout. 我有一个包含线性布局的水平滚动视图。 Something like this:
像这样:
<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.
我想要做的是添加n个相同的布局以在水平滚动视图中显示产品集合。
The picture below will give you an idea of what I have done so far. 下图将使您大致了解我到目前为止所做的事情。
For this, I have added product collection view to R.id.product_container_layout
. 为此,我将产品集合视图添加到
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]);
}
But the problem is passing values. 但是问题在于传递价值。 Since each view added to product_container_layout has the same ids.
由于添加到product_container_layout的每个视图都具有相同的ID。 Therefore only first element gets the value from the product collection array.
因此,只有第一元素获得从产物收集阵列的值。
What I can do is generate view id for each view and its element and map these ids to certain name so that I could access them by id. 我可以做的是为每个视图及其元素生成视图ID,并将这些ID映射到特定名称,以便我可以通过ID访问它们。
Is this the correct methodology or should I do something else?. 这是正确的方法,或者我应该做些别的事情?
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);
}
}
}
So, you need to populate your view before adding to container or save links to all items. 因此,您需要在添加到容器或保存指向所有项目的链接之前填充视图。 It will looks like this:
它看起来像这样:
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);
}
after all, you can access to your elements by index, if you want to change it: 毕竟,如果要更改它,则可以按索引访问元素:
parentLayout.getChildAt(index)
But if you have a lot of items, it's not good idea. 但是,如果您有很多物品,那不是一个好主意。 In this case you will not recycle your views and will consume a lot of memory.
在这种情况下,您将不会回收视图,并且会占用大量内存。 I suggest to use recyclerview from support library.
我建议使用支持库中的recyclerview 。
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>
You can use **DevSmart** library for horizontal listview which is similar to **ListView**.
https://github.com/dinocore1/DevsmartLib-Android
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.