Java GridView在滚动时迷失方向

Java GridView在滚动时迷失方向,java,android,android-studio,gridview,android-arrayadapter,Java,Android,Android Studio,Gridview,Android Arrayadapter,gridView中的项目有时会重叠或向上或向下移动一点。但在再次向上滚动时会得到修复。不幸的是,我无法提供图像,因为我是新来的。我不确定我是否在代码中执行了某些操作。但使用listView时没有问题 gridView的Xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app

gridView中的项目有时会重叠或向上或向下移动一点。但在再次向上滚动时会得到修复。不幸的是,我无法提供图像,因为我是新来的。我不确定我是否在代码中执行了某些操作。但使用listView时没有问题

gridView的Xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.gametalks.MainActivity"
    android:background="@color/card_white">

    <GridView
android:columnWidth="10dp"
                android:numColumns="2"
                android:id="@+id/listView"
                android:dividerHeight="0dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:verticalSpacing="30dp"
                ></GridView>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

下面是适配器:

[public class GameAdapter extends  ArrayAdapter<GameNews> {


    public GameAdapter(@NonNull Context context, List<GameNews> gameNews ){
        super(context, 0, gameNews);
        if(gameNews == null)
        {
            return;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        GameNews currentNews = getItem(position);

        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.grid_item, parent, false);
            final ViewHolder v = new ViewHolder();

            // Find the TextView in the grid_item.xml layout with the ID version_name

            v.Heading = (TextView) listItemView.findViewById(R.id.title);
            v.description = (TextView) listItemView.findViewById(R.id.description);
            v.newsImage = (ImageView) listItemView.findViewById(R.id.image) ;
            v.source = (TextView)  listItemView.findViewById(R.id.source);
            v.time = (TextView) listItemView.findViewById(R.id.time);
            v.menuButton = (Button) listItemView.findViewById(R.id.popup);
            listItemView.setTag(v);


            v.menuButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    PopupMenu popup = new PopupMenu(getContext(),view);
                    MenuInflater inflater = popup.getMenuInflater();
                    inflater.inflate(R.menu.popup_menu, popup.getMenu());
                    popup.show();

                }
            });
        }

ViewHolder viewHolder = (ViewHolder) listItemView.getTag();

        //Setting title to current news title
        viewHolder.Heading.setText(currentNews.getTitle());
        viewHolder.description.setText(currentNews.getDescription());
        viewHolder.newsImage.setTag(currentNews.getphotoUrl());
viewHolder.source.setText(currentNews.getSource());
       viewHolder.time.setText(currentNews.getTime());
      //  Picasso.with(getContext()).load(currentNews.getphotoUrl()).into(viewHolder.newsImage);


       if (URLUtil.isValidUrl(currentNews.getphotoUrl())) {
            Picasso.with(getContext())
                    .load(currentNews.getphotoUrl())
                    .resize(350,300)
                    .tag(tag)
                    .error(R.drawable.placeholder)
                    .placeholder(R.drawable.placeholder)
                    .into(viewHolder.newsImage);

        } else {
            Picasso.with(getContext())
                    .load(R.drawable.right_line)
                    .resize(350,300)
                    .tag(tag)
                    .noPlaceholder()
                    .into(viewHolder.newsImage);
        }

        return listItemView;
    }




    public static class ViewHolder {
         TextView Heading;
         TextView description;
        ImageView newsImage;
        TextView source;
TextView time;
Button menuButton;


    }
}][1]
[公共类游戏适配器扩展ArrayAdapter{
公共GameAdapter(@NonNull上下文,列出gameNews){
超级(上下文,0,游戏新闻);
如果(gameNews==null)
{
返回;
}
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
GameNews currentNews=getItem(位置);
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.grid_项,父项,false);
最终视图保持架v=新的视图保持架();
//在grid_item.xml布局中查找ID版本名称为的TextView
v、 Heading=(TextView)listItemView.findViewById(R.id.title);
v、 description=(TextView)listItemView.findViewById(R.id.description);
v、 newsImage=(ImageView)listItemView.findViewById(R.id.image);
v、 source=(TextView)listItemView.findViewById(R.id.source);
v、 time=(TextView)listItemView.findViewById(R.id.time);
v、 menuButton=(按钮)listItemView.findViewById(R.id.popup);
setTag(v);
v、 menuButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
PopupMenu popup=新建PopupMenu(getContext(),视图);
MenuInflater充气器=弹出窗口。getMenuInflater();
充气器。充气(R.menu.popup_菜单,popup.getMenu());
popup.show();
}
});
}
ViewHolder ViewHolder=(ViewHolder)listItemView.getTag();
//将标题设置为当前新闻标题
viewHolder.Heading.setText(currentNews.getTitle());
viewHolder.description.setText(currentNews.getDescription());
viewHolder.newsImage.setTag(currentNews.getphotoUrl());
viewHolder.source.setText(currentNews.getSource());
viewHolder.time.setText(currentNews.getTime());
//Picasso.with(getContext()).load(currentNews.getphotoUrl()).into(viewHolder.newsImage);
if(URLUtil.isValidUrl(currentNews.getphotoUrl())){
Picasso.with(getContext())
.load(currentNews.getphotoUrl())
.调整大小(350300)
.标签(tag)
.错误(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(viewHolder.newimage);
}否则{
Picasso.with(getContext())
.负载(右侧可牵引右U线)
.调整大小(350300)
.标签(tag)
.noPlaceholder()
.into(viewHolder.newimage);
}
返回listItemView;
}
公共静态类视图持有者{
文本视图标题;
文本视图描述;
图像查看新闻图像;
文本视图源;
文本查看时间;
按钮菜单按钮;
}
}][1]

如果您需要任何其他详细信息,请询问。

发生这种情况的原因是,我将gridView项目的大小设置为包装内容,因此不同的项目具有不同的大小。这导致gridView不稳定。我通过给gridView项目一个固定的dp大小来修复它。现在它没有问题。

您应该提供一些代码,或者一个要点的链接,这样我们就可以看到一些东西,因为这个描述是模糊的。