2

I want to show more images in horizontal scroll view from API and dynamically object creation, like:

enter image description here

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • use custom view for this – Krishna Kachhela Jul 29 '16 at 12:25
  • use RecyclerView with horizontal layout – Akarsh M Jul 29 '16 at 12:28
  • https://examples.javacodegeeks.com/android/core/ui/horizontalscrollview/android-horizontalscrollview-example/ – Amit Vaghela Jul 29 '16 at 12:37
  • You can refer to these topics for help: 1. [http://stackoverflow.com/questions/14618632/how-to-add-dynamic-image-with-horizontal-scrollview-listview](http://stackoverflow.com/questions/14618632/how-to-add-dynamic-image-with-horizontal-scrollview-listview) 2. [http://stackoverflow.com/questions/7186040/insert-a-view-dynamically-in-a-horizontalscrollview-in-android](http://stackoverflow.com/questions/7186040/insert-a-view-dynamically-in-a-horizontalscrollview-in-android) – Mor Paz Jul 29 '16 at 12:27

2 Answers2

0

You can use recyclerview for that,just try this way

http://blog.nkdroidsolutions.com/android-horizontal-vertical-recyclerview-example/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/vertical_recycler_view"
        android:background="#fff"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <View
        android:background="#787878"
        android:layout_width="match_parent"
        android:layout_height="3dp"/>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontal_recycler_view"
        android:background="#fff"
        android:layout_width="match_parent"
        android:layout_height="70dp"/>
</LinearLayout>

HorizontalAdapter

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {

        private List<String> horizontalList;

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView txtView;

            public MyViewHolder(View view) {
                super(view);
                txtView = (TextView) view.findViewById(R.id.txtView);

            }
        }


        public HorizontalAdapter(List<String> horizontalList) {
            this.horizontalList = horizontalList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.horizontal_item_view, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            holder.txtView.setText(horizontalList.get(position));

            holder.txtView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,holder.txtView.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return horizontalList.size();
        }
    }

OUTPUT

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

Use horizontalscrollview like this:

               <HorizontalScrollView
                android:id="@+id/scrollView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/addPhotoHeader"
                android:layout_marginBottom="12dp"

                android:layout_marginTop="12dp"
                android:layout_toLeftOf="@+id/rightArrow"

                android:layout_toRightOf="@+id/leftArrow">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:orientation="horizontal">

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


                    </LinearLayout>


                </LinearLayout>
            </HorizontalScrollView>

Use java code like this, where pictureParent is a linearLayout inside HorizontalScrollView

 final float scale = getResources().getDisplayMetrics().density;
        int pixels = (int) (58 * scale + 0.5f);
        ImageView image = new ImageView(this);
        int leftPadding = (int) (10 * scale + 0.5f);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(pixels, pixels);
        layoutParams.setMargins(0, 0, leftPadding, 0);
        image.setLayoutParams(layoutParams);


        // Adds the view to the layout
        taskPictures.add(mFileTemp);
        imagePlaceHolder.requestFocus();
        Picasso.with(this).load(Crop.getOutput(result))
                .skipMemoryCache()
                .resize(92, 92)
                .centerCrop().into(image);
        pictureParent.addView(image);
Abhinav Pawar
  • 421
  • 3
  • 12