横向滚动
一.修改子项布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
</LinearLayout>
将LinearLayout改为垂直方向排列,并把宽度设为100dp(防治水果文字长度不一致),图片文字水平居中,文字距父布局顶部10dp。
二.修改MainActivity中onCreate()方法中代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewFruitAdapter adapter = new RecyclerViewFruitAdapter(list);
recyclerView.setAdapter(adapter);
}
调用LinearLayoutManager的setOrientation()方法设置布局的排列方向,默认是纵向的,传入LinearLayoutManager.HORIZONTAL表示让布局横向排列。
除了LinearLayoutManager之外,RecyclerView还给我们提供了GirdLayoutManager(可以实现网格布局) 和 StaggeredGridLayoutManager(可以实现瀑布流布局)
瀑布式
一.修改子项布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"
/>
</LinearLayout>
将宽度改为match_parent,因为瀑布流布局的宽度是根据布局的列数自动适配的,而不是固定值。另外让子项之间留点间距。让文字居左对齐。
二.修改MainActivity中代码:
public class MainActivity extends AppCompatActivity {
private List<Fruit> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewFruitAdapter adapter = new RecyclerViewFruitAdapter(list);
recyclerView.setAdapter(adapter);
}
private void init() {
for (int i = 0; i < 10; i++) {
Fruit fruit = new Fruit(R.drawable.fruit, getRandomLengthName("apple"));
list.add(fruit);
Fruit fruit1 = new Fruit(R.drawable.fruit, getRandomLengthName("orange"));
list.add(fruit1);
Fruit fruit2 = new Fruit(R.drawable.fruit, getRandomLengthName("banana"));
list.add(fruit2);
}
}
private String getRandomLengthName(String name) {
Random random = new Random();
int length = random.nextInt(20) + 1;
StringBuilder sb = new StringBuilder();
for (int i = 0; i< length; i++) {
sb.append(name);
}
return sb.toString();
}
}
1.创建StaggeredGridLayoutManager的实例,构造函数接收两个参数,第一个用于指定布局列数,第二个用于指定布局的排列方向。
2.getRandomLengthName()方法用与随机指定名称长度
|