RecyclerView GridLayoutManager:跨度计数默认为 1
RecyclerView GridLayoutManager: span count is defaulted to 1
尽管我将 gridlayoutmanager 跨度计数定义为 2.. 只有一列是模拟器的结果。我尝试在 XML 中更改卡片的宽度,但效果不佳。
主要活动:
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
使用代码并为 recyclerview 设置适配器,然后在适配器中填充 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);
}
尽管我将 gridlayoutmanager 跨度计数定义为 2.. 只有一列是模拟器的结果。我尝试在 XML 中更改卡片的宽度,但效果不佳。 主要活动:
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
使用代码并为 recyclerview 设置适配器,然后在适配器中填充 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);
}