Diego Perez
Diego Perez

Reputation: 2864

Android: Creating RecyclerView programmatically won't work

I have a RecyclerView in the xml like this:

<LinearLayout
    android:id="@+id/llTestMenuMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:id="@+id/rv" />

</LinearLayout>

and using it to show a dynamic list of buttons like this:

int buttonFontSize = 20;
RecyclerView rv = findViewById(R.id.rv);

LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
adapter = new RVAdapter_ButtonList(tests, null, this, buttonFontSize);
rv.setAdapter(adapter);
rv.setVisibility(View.VISIBLE);

and all is working fine, but now all the buttons that are displayed vertically will be displayed horizontally and grouped into categories, so I need to create RecyclerViews dynamically, and I startd with a simple example of only one the next way with no success, as a blank screen is showing:

int buttonFontSize = 20;

RecyclerView rv = new RecyclerView(this);
RecyclerView.LayoutParams params = new
                RecyclerView.LayoutParams(
                RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.WRAP_CONTENT
        );
rv.setLayoutParams(params);

LinearLayout llTestMenuMain = findViewById(R.id.llTestMenuMain);
llTestMenuMain.addView(rv);

LinearLayoutManager llm = new LinearLayoutManager(this);rv.setLayoutManager(llm);
adapter = new RVAdapter_ButtonList(tests, null, this, buttonFontSize);
rv.setAdapter(adapter);
rv.setVisibility(View.VISIBLE);

Any help on why I cannot make the dynamically created RecyclerView working will be much appreciated.

Edit 1:

I don't know if it is of any help, but noticed that creating the RV dynamically the adapter "onBindViewHolder" is never called (and the itemCount > 0, of course), does it when the RV is created in XML. Cannot find a solution to this... :(

Edit 2:

Also tried

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });

Just in case, to no avail too.

Upvotes: 1

Views: 3083

Answers (2)

Diego Perez
Diego Perez

Reputation: 2864

I found that putting the programmatically created RecyclerView inside a ScrollView makes it "magically" visible.

Upvotes: -1

MajedEddin
MajedEddin

Reputation: 71

Try to replace your code with this one:

int buttonFontSize = 20;

LinearLayout llTestMenuMain = findViewById(R.id.llTestMenuMain);

RecyclerView rv = new RecyclerView(this);
RecyclerView.LayoutParams params = new
                RecyclerView.LayoutParams(
                RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.WRAP_CONTENT
        );
rv.setLayoutParams(params);
    
LinearLayoutManager llm = new LinearLayoutManager(this);
adapter = new RVAdapter_ButtonList(tests, null, this, buttonFontSize);
rv.setAdapter(adapter);
rv.setLayoutManager(llm);
rv.setVisibility(View.VISIBLE);

llTestMenuMain.addView(rv);

Upvotes: 5

Related Questions