gpt4 book ai didi

java - 如何为 android recyclerview 网格布局设置边框。

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:29 26 4
gpt4 key购买 nike

我想要一个看起来像这样的 View ,一个在项目之间没有间距的边框。

enter image description here

目前我的 View 看起来像这样,我正在使用带有卡片 View 布局的 recyclerview。

enter image description here

下面是我对每个单独项目的代码

single_item_homepage xml:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_margin="8dp"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ImageView
android:id="@+id/imageViewCategory"
android:layout_width="match_parent"
android:layout_height="75dp"
/>

<TextView
android:text="Shirt"
android:id="@+id/textViewCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="15sp"/>


</LinearLayout>

</android.support.v7.widget.CardView>

主页 Activity :

public class HomepageActivity extends AppCompatActivity {


private RecyclerView recyclerView;
private ImageAdapter imageAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);

recyclerView = findViewById(R.id.recyclerView);
imageAdapter = new ImageAdapter(this);

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(HomepageActivity.this,3));
recyclerView.setAdapter(imageAdapter);

}
}

这是我的适配器类:

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {


private Context context;

public ImageAdapter(Context context) {
this.context = context;
}

private int[] resourceId = new int[] {R.drawable.shirt,R.drawable.sleeveless,R.drawable.outerwear,
R.drawable.sweater,R.drawable.pants,R.drawable.shorts,R.drawable.skirt,R.drawable.dresses,
R.drawable.shoes,R.drawable.bags,R.drawable.accessories,R.drawable.swimwear
};

private String[] names = new String[]{
"Shirts","Sleevless","Outerwear","Sweater","Pants","Shorts","Skirts","Dresses","Shoes","Bags","Accessories","Swimwear"
};



@NonNull
@Override
public ImageAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.single_item_homepage,parent,false);
return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ImageAdapter.ImageViewHolder holder, int position) {
String currName = names[position];
int currResource = resourceId[position];
holder.categoryName.setText(currName);
holder.categoryImage.setImageResource(currResource);

}

@Override
public int getItemCount() {
return names.length;
}

class ImageViewHolder extends RecyclerView.ViewHolder {

TextView categoryName;
ImageView categoryImage;


public ImageViewHolder(View itemView) {
super(itemView);

categoryName = itemView.findViewById(R.id.textViewCategory);
categoryImage = itemView.findViewById(R.id.imageViewCategory);
}
}
}

有没有办法改变 recyclerview 的边框?提前致谢:)

最佳答案

HomepageActivity.java

recyclerView = findViewById(R.id.recyclerView);
imageAdapter = new ImageAdapter(this);

recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.HORIZONTAL))
recyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL))
recyclerView.setLayoutManager(new GridLayoutManager(HomepageActivity.this,2));
recyclerView.setAdapter(imageAdapter);

single_item_homepage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:selectableItemBackground"
android:orientation="vertical"
android:padding="@dimen/fifteen_dp">

<ImageView
android:id="@+id/ivGraphItemThumb"
android:layout_width="match_parent"
android:layout_height="125dp"
tools:src="@drawable/ic_recent_exce" />

<TextView
android:id="@+id/tvMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/fifteen_sp"
tools:text="Item 1" />
</LinearLayout>

enter image description here

关于java - 如何为 android recyclerview 网格布局设置边框。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com