-2

I want to design a view like this

enter image description here

its the same view repeating multiple times,i.e. when clicked on add item , it should add same view(on the beginning there should be only one row). I don't understand how to make one, seems I can't make such view using RecyclerView. Maybe I can make one using LayoutInflater. but how?

Mo Faizan Shaikh
  • 307
  • 1
  • 4
  • 14
  • Possible duplicate of [How to use layoutinflator to add views at runtime?](https://stackoverflow.com/questions/4735847/how-to-use-layoutinflator-to-add-views-at-runtime) – ADM Dec 28 '17 at 05:42
  • If your views are limited then just use a `LinearLayout` as container and use `LayoutInflater` to add view . If Views can be unlimited then you should use `RecyclerView`. – ADM Dec 28 '17 at 05:44
  • 1
    Possible duplicate of [Dynamic form with repeating form](https://stackoverflow.com/questions/47975286/dynamic-form-with-repeating-form) – Ratilal Chopda Dec 28 '17 at 05:44
  • @ADM I want to add this view on click event of "add item" button. what should I use?, layout inflator or recyclerview, bcoz I only need to add one row on one click of button. – Mo Faizan Shaikh Dec 28 '17 at 05:47
  • Exact answer... https://stackoverflow.com/questions/47975286/dynamic-form-with-repeating-form?answertab=active#tab-top – Nikhil Borad Dec 28 '17 at 05:49
  • Thats what is said it depends upon max items can be added . if There is no limit on max item then go with `RecyclerView` of if there is a limit lets say 10 or 20 then a `LinearLayout` will work . – ADM Dec 28 '17 at 05:50
  • @NikhilBorad its not exact answer, bcoz number of items in recyclervie is dependent on number of usernames, etc in that example. In my view, there are spinners and spinners of those has nothing to do with number of times views will repeat. views will repeat based on number of times user click on "add more" button – Mo Faizan Shaikh Dec 28 '17 at 05:53
  • then I prefer to use the LayoutInflater to create a view based on your layout template, and then inject it into the view where ever you want. – Nikhil Borad Dec 28 '17 at 05:55
  • check my answer if it helps – Abubakker Moallim Dec 28 '17 at 05:56

2 Answers2

0

Yes you can add it by using Layout Inflator:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>

In Activity:

LinearLayout linearLayout = findViewById(R.id.main_container);
                LinearLayout childLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.child_layout, null, false);
                childLayout.setTag(linearLayout.getChildCount() + 1); // set some Id
                linearLayout.addView(childLayout); // Adding for first time
                for(int i=0; i<5; i++) {
                    childLayout.setTag(linearLayout.getChildCount() + 1);
                    linearLayout.addView(childLayout); //child_layout is your row layout design
                }

To Retrive :

 JSONObject jsonObject = new JSONObject();
            for (int i = 0; i < linearLayout.getChildCount(); i++) {
                View childLayout = linearLayout.getChildAt(i);
                if (childLayout instanceof LinearLayout) {
                    //get SubViews of you child_layout and get thos value 
                    // for id int id = (int) childLayout.getTag()
                    //add the values to jsonObject
                }
            }
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
0

Well Create a Model class :

 public  class Product{
     int product_id;
     int product_size;
     int product_qty;

        //provide all the getter setters and constructor
         public Product(int product_id,int product_size,int product_qty){
                this.product_id = product_id;
                this.product_size = product_size;
                this.product_qty = product_qty;
         }
       }

Now use this arraylist inside Recyclerview Adapter :

ArrayList<Product> productList = new ArrayList<Product>

and while clicking on add button just add dummy values in this list like :

   productList.add(new Product(0,0,0));

And inside your recyclerview adapter provide the layout of your choice

Because later if you want to set values you can easily do it with position and also while clicking on submit you can just take whole list

Abubakker Moallim
  • 1,610
  • 10
  • 20
  • `class` are all small letters and Java has a dedicated naming conventions (please avoid underscores in variable names). – Enzokie Dec 28 '17 at 05:59