RecyclerView GridLayoutManager:跨度计数默认为1

时间:2017-03-08 05:52:58

标签: android android-recyclerview gridlayoutmanager

虽然我将gridlayoutmanager span count定义为2 ..但只有一列是模拟器的结果。我尝试用XML改变卡的宽度,但没有好处.. 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");
    }
}

网格布局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

1 个答案:

答案 0 :(得分:2)

使用代码并设置Adapter for recyclerview,并在Adapter中扩充该gridlayout.xml。

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

并且适配器使用您的布局

    @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);
}