我正面临着一个非常奇怪的问题。这里我总共有8张Gridview风格的图片,每张图片的大小正好是392×500。但当我运行app时,在最后8张图片的下方总是留有一小块地方。
以下是我的代码
public class BooksPage extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
int images[] = {R.drawable.phy,R.drawable.chem,R.drawable.drawable_logy,R.drawable.drawable_geology,R.drawable.bbs,R.drawable.drawable_chartered,R.drawable.bachelor,R.drawable.bachelor};
String stream[]= {"MSC Physics","MSC Chemistry","MSC Biology",
"MSC Geology","BBS|BBA","CA","Bachelor","+2 Science"};
TextView navName,navEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("पुस्तक किनबेच");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View navHeaderView = navigationView.getHeaderView(0);
navName = (TextView)navHeaderView.findViewById(R.id.nav_name);
navEmail = (TextView)navHeaderView.findViewById(R.id.nav_mail);
navigationView.setNavigationItemSelectedListener(this);
RecyclerView favPlaces = (RecyclerView) findViewById(R.id.favPlaces);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
favPlaces.setLayoutManager(layoutManager);
favPlaces.setHasFixedSize(true);
ArrayList<BookDetails> placeList = getPlaces();
if (!isNetworkStatusAvialable(this)) {
Toast.makeText(this, "No Internet!", Toast.LENGTH_SHORT).show();
}
StaggeredAdapter staggeredAdapter = new StaggeredAdapter(this,placeList);
favPlaces.setAdapter(staggeredAdapter);
navName.setText(PreferenceManager.getDefaultSharedPreferences(this).getString("name","null"));
navEmail.setText(PreferenceManager.getDefaultSharedPreferences(this).getString("email","null"));
}
private ArrayList<BookDetails> getPlaces() {
ArrayList<BookDetails> details = new ArrayList<>();
for (int index=0; index<images.length;index++){
details.add(new BookDetails(images[index],stream[index]));
}
return details;
}
}
我的适配器的xml代码是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimaryDark">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/placePic"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/placeName"
android:textStyle="bold"
android:textColor="#ffffff"
android:fontFamily="sans-serif-condensed"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:gravity="center"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
然后我虽然可能会增加图像的大小,但无论我将图像大小设置为50×50还是1000×1000,图像的大小总是相同的,空间也是恒定的。
这是屏幕截图

同样,另一个图像也是如此
但有趣的是,当我在第八个位置使用相同的第七个位置的图像时,also.....Then空间消失器和图像完全吻合。
这是屏幕截图

这里的问题是什么?我怎样才能修复它呢?感谢您的帮助
我正在使用photoshop通过保存为Web格式来更改图像的宽度和高度。
发布于 2018-09-02 10:17:23
下面的代码可以解决这个问题。但可能需要很少的编辑。
在fragment/Activity onCreate/ ocreateView中添加以下代码。在这里,您只需要项目装饰器为您的回收商缩放项目
mRecycler.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(2), true));添加创建方法dpToPx
/**
* Converting dp to pixel
*/
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}现在,在您的活动/片段中为GridSpacingItemDecoration创建一个类
/**
* RecyclerView item decoration - give equal margin around grid item
*/
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}https://stackoverflow.com/questions/52133420
复制相似问题