尽管我将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
最佳答案
使用代码并为cyclerview设置Adapter,然后在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);
}
关于android - RecyclerView GridLayoutManager:跨度计数默认为1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42663952/