amar
amar

Reputation: 481

RecyclerView GridLayoutManager: span count is defaulted to 1

Although I'm defining the gridlayoutmanager span count as 2.. only one column is the result of emulator. I tried changing the width of card in XML but no good there.. MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pb = (ProgressBar) findViewById(R.id.pb1);
    pause = (ImageButton) findViewById(R.id.pause);
    refresh = (ImageButton) findViewById(R.id.refresh);
    sb = (SeekBar) findViewById(R.id.seekBar);
    pause.setVisibility(pause.INVISIBLE);
    sb.setVisibility(sb.INVISIBLE);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   lmanager = new LinearLayoutManager(this);
     gmanager = new GridLayoutManager(this,2);

    rv = (RecyclerView) findViewById(R.id.rv1);
   //initializeRecyclerView();
    rv.setLayoutManager(lmanager);
    pb.setVisibility(pb.VISIBLE);
    if(isconnected()) {
        new Podcast_async(this).execute("https://www.npr.org/rss/podcast.php?id=510298");
    }
}

Grid layout XML:

<android.support.v7.widget.CardView
    android:layout_below="@+id/tool_bar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_height="150dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:layout_width="150dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="115dp"

        android:id="@+id/imageView4" />

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="118dp"

        android:background="@null"
        app:srcCompat="@drawable/playcircle"
        android:id="@+id/imageButton2" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_marginTop="120dp"

        android:id="@+id/textView4"
        android:layout_height="30dp"
        android:gravity="center_horizontal" />
</android.support.v7.widget.CardView>

Emulator

Upvotes: 1

Views: 4416

Answers (1)

Vishal Gedia
Vishal Gedia

Reputation: 207

use the code and set Adapter for recyclerview and just inflate that gridlayout.xml in Adapter.

RecyclerView.LayoutManager layoutmanager = new GridLayoutManager(this, 2);
    recyclerview.setLayoutManager(layoutmanager);

and for adapter use your layout

    @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.GridLayout.xml, parent, false);
    return new Adapter.ViewHolder(view);
}

Upvotes: 2

Related Questions