我正在尝试制作一个如图所示的屏幕,因为您可以看到RecyclerView中的项目之间没有间距。
所需图像:
到目前为止,我对“回收者”视图中每个项目的 item_layout 是:
var chrt = '';
if(!this.chart){
chrt = this
}
else{
chrt = this.chart
}
我在 ColorShadesActivity 中的ItemDecoration如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="120dp"
android:layout_margin="0dp"
android:layout_height="190dp"
app:cardCornerRadius="0dp">
<LinearLayout
android:id="@+id/colorBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="@+id/colorNameBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="asd"
android:textColor="#fff"
android:textSize="16dp"
>
</TextView>
</LinearLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:0)
使用RecyclerView显示GridLayout您只需要执行几个步骤:
这里是简化的工作示例代码,可以实现您想要的目标。我正在动态更改项目的高度,但是您可以为此使用xml布局。
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random(System.currentTimeMillis());
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new RecyclerView.Adapter() {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false);
ViewGroup.LayoutParams layoutParams = inflate.getLayoutParams();
layoutParams.height = (int) (getResources().getDisplayMetrics().scaledDensity * 56);
inflate.setLayoutParams(layoutParams);
int randomColor = random.nextInt(Colors.COLORS.length);
inflate.setBackgroundColor(Color.parseColor(Colors.COLORS[randomColor]));
return new RecyclerViewViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((TextView) viewHolder.itemView.findViewById(android.R.id.text1)).setText(Cheeses.CHEESES[i]);
}
@Override
public int getItemCount() {
return Cheeses.CHEESES.length;
}
});
}
private class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
RecyclerViewViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}