gpt4 book ai didi

Android StaggeredGrid RecyclerView 自动移动项目

转载 作者:行者123 更新时间:2023-12-02 17:11:48 25 4
gpt4 key购买 nike

我正在尝试使用 Staggered Grid 显示任何图片,但也出现了很多问题。

首先,当我拉到顶部时,它会移动项目,例如从左到右,或者交换左右列。

参见下面的.gif:

enter image description here

我已经通过 Google 搜索了此问题的解决方案。我在我的项目中使用此代码:

StaggeredGridLayoutManager Layoutmanager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL);
manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
recyclerView.setLayoutManager(manager);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
((StaggeredGridLayoutManager)recyclerView.getLayoutManager()).invalidateSpanAssignments();
}
});

不幸的是,当我向上滚动时仍然出现严重问题。该项目仍然会移动,并且即使我只是滚动一小会儿,它也会滚动很远。

参见下面的.gif:

enter image description here

我该如何处理这些问题,或者以其他方式达到这个效果?

主要 Activity :

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;

private photos[] photoses = {
new photos("Tags:",R.drawable.timg),
new photos("Tags:",R.drawable.timg1),
new photos("Tags:",R.drawable.timg2),
new photos("Tags:",R.drawable.timg3),
new photos("Tags:",R.drawable.timg4),
new photos("Tags:",R.drawable.timg5),
new photos("Tags:",R.drawable.timg6),
new photos("Tags:",R.drawable.timg7),
new photos("Tags:",R.drawable.timg8),
new photos("Tags:",R.drawable.timg9),
new photos("Tags:",R.drawable.timg10),
new photos("Tags:",R.drawable.timg11),
new photos("Tags:",R.drawable.timg12),
};

private List<photos> photosList = new ArrayList<>();

private photoAdapter adapter;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar,menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.edit_bar:
Toast.makeText(this,"edit",Toast.LENGTH_SHORT).show();
break;
case R.id.search_bar:
Toast.makeText(this,"search",Toast.LENGTH_SHORT).show();
break;
default:break;
}
return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBar actionBar = getSupportActionBar();
if (actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.homemenu);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view,"Hello World",Snackbar.LENGTH_SHORT).show();
}
});
initPhotos();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new photoAdapter(photosList);
recyclerView.setAdapter(adapter);
}

private void initPhotos() {
photosList.clear();
for (int i = 0;i<100;i++){
Random random = new Random();
int index = random.nextInt(photoses.length);
photosList.add(photoses[index]);
}
}
}

照片适配器:

public class photoAdapter extends RecyclerView.Adapter<photoAdapter.ViewHolder> {

private Context mcontext;

private List<Integer> mHeights;

private List<photos> mPhotoList;

static class ViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
ImageView photoView;
TextView photoTag;

public ViewHolder(View view){
super(view);
cardView = (CardView) view;
photoView = (ImageView) view.findViewById(R.id.photo_view);
photoTag = (TextView) view.findViewById(R.id.photo_tag);
}
}

public photoAdapter(List<photos> photoList){
mPhotoList = photoList;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mcontext==null){
mcontext = parent.getContext();
}
View view = LayoutInflater.from(mcontext).inflate(R.layout.photo_item,parent,false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
photos photo = mPhotoList.get(position);
holder.photoTag.setText(photo.getTag());
Glide.with(mcontext).load(photo.getImageId()).into(holder.photoView);
}

@Override
public int getItemCount() {
return mPhotoList.size();
}
}

最佳答案

来自 Android 文档

Staggered grids are likely to have gaps at the edges of the layout. Toavoid these gaps, StaggeredGridLayoutManager can offset spansindependently or move items between spans. You can control thisbehavior via setGapStrategy(int)

引用: https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

https://stackoverflow.com/a/34274327/3701433

关于Android StaggeredGrid RecyclerView 自动移动项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521663/

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