简体   繁体   中英

Programmatically create rows with linearlayouts in Android

I may being going about this the wrong way, but I pretty much have the code almost there so I don't think I am too far off. I am trying to create Horizontal Linearlayout with two vertical Linearlayouts inside programmatically. I have created it in the xml to test it, but as I load data from the db I need to dynamically create the rows.

Here is what my xml looks like and what I am trying to create programmatically:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginLeft="25dip"
            android:layout_marginRight="10dip"
            android:background="@drawable/rounded_edges_white">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:id="@+id/imageView3"
                android:contentDescription="personIcon"
                android:src="@drawable/person_icon"
                android:layout_gravity="center" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f1f1f1"
                android:layout_marginBottom="10dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Danielle"
                android:id="@+id/textView81"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="25dip"
            android:background="@drawable/rounded_edges_white">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:id="@+id/imageView2"
                android:contentDescription="personIcon"
                android:src="@drawable/person_icon"
                android:layout_gravity="center" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f1f1f1"
                android:layout_marginBottom="10dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Danielle"
                android:id="@+id/textView8"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
    </LinearLayout>

So i can just duplicate that xml to get the layout I want. Here is the code I've created:

    public void buildHome(){
        DatabaseHandler db = new DatabaseHandler(this);
        List<Person> people = db.getAllPeople();

        LinearLayout parentLayout = (LinearLayout) this.findViewById(R.id.personList);\
        float padding25 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics());
        float padding10 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
        float size120 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics());
        float size80 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
        float size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        float mBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());


        int count = 0;
        int totalCount = 1;
        LinearLayout rowLayout = null;
        LinearLayout childLayout = null;
        for (final Person peep : people) {
            if(count == 2) count = 0;
            Log.d("count", ""+count);
            if(count == 0) {
                Log.d("creating row", "true");
                //create row layout container
                rowLayout = new LinearLayout(this);
                rowLayout.setOrientation(LinearLayout.HORIZONTAL);
                LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                rowLayout.setTag(totalCount);
                rowLayout.setLayoutParams(rowParams);
                rowLayout.setBaselineAligned(false);
            }

            //create row layout container
            childLayout = new LinearLayout(this);
            childLayout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);

            //left or right col
            if(count == 0) layoutParams.setMargins(Math.round(padding25), Math.round(padding10), Math.round(padding10), 0);
            else layoutParams.setMargins(Math.round(padding10), Math.round(padding10), Math.round(padding25), 0);
            childLayout.setLayoutParams(layoutParams);

            childLayout.setTag(peep.getId());
            childLayout.setClickable(true);
            childLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Main.this);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("personID", "" + peep.getId());
                    editor.apply();

                    Intent intent = new Intent(Main.this, List.class);
                    startActivityForResult(intent, 1);
                }
            });
            childLayout.setBackgroundResource(R.drawable.rounded_edges_white);

            //set icon
            ImageView icon = new ImageView(this);
            LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(Math.round(size120), Math.round(size80));
            imageParams.gravity = Gravity.CENTER_HORIZONTAL;
            icon.setLayoutParams(imageParams);
            icon.setContentDescription("personIcon");
            icon.setImageResource(R.drawable.person_icon);
            childLayout.addView(icon);

            //horizontal line
            View horizontalLine = new View(this);
            horizontalLine.setBackgroundColor(Color.LTGRAY);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, Math.round(size));
            params.setMargins(0, 0 , 0, Math.round(mBottom));
            horizontalLine.setLayoutParams(params);
            childLayout.addView(horizontalLine);

            //Set name title
            TextView titleView = new TextView(this);
            titleView.setText(peep.getName());
            LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            titleParams.gravity = Gravity.CENTER_HORIZONTAL;
            titleView.setLayoutParams(titleParams);
            childLayout.addView(titleView);

            //add to row
            rowLayout.addView(childLayout);

            //add to parent view
            if(count == 1){
                Log.d("add to parent", "true");
                parentLayout.addView(rowLayout);
            }
            count++;
            totalCount++;
        }

        Log.d("size", ""+people.size());
        if(people.size() % 2 != 0) {
            Log.d("only 1", "true");
            parentLayout.addView(rowLayout);
        }
    }

Here is what the log says:

10-21 17:48:41.586 24022-24022/com.example.app D/count: 0
10-21 17:48:41.586 24022-24022/com.example.app D/creating row: true
10-21 17:48:41.587 24022-24022/com.example.app D/count: 1
10-21 17:48:41.598 24022-24022/com.example.app D/add to parent: true
10-21 17:48:41.598 24022-24022/com.example.app D/count: 0
10-21 17:48:41.598 24022-24022/com.example.app D/creating row: true
10-21 17:48:41.598 24022-24022/com.example.app D/after the first row: true
10-21 17:48:41.599 24022-24022/com.example.app D/count: 1
10-21 17:48:41.599 24022-24022/com.example.app D/add to parent: true
10-21 17:48:41.600 24022-24022/com.example.app D/size: 4

SO its hitting all the right places and saying its creating row and adding to parent. This is the closest post to what I am doing linear layout inside linear layout is adding but is not visible

but he isn't really dynamically adding the extra rows. He has layout2 and 3 if he goes over.

This is what i am aiming for(repeating as many times as needed):

            |---------------|   |---------------|
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |---------------|   |---------------|

            |---------------|   |---------------|
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |               |   |               |
            |---------------|   |---------------|

I have 4 items in the database but only row 1 with 2 items shows.

Thanks

it has to do with your code

//add to parent view
if(count == 1){
  Log.d("add to parent", "true");
     parentLayout.addView(rowLayout);
  }
 count++;
 totalCount++;

you should change it to

if(count % 2 == 1) {

this will add the row every 2 items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM