我试图在网格视图上显示图像和文本,但是每次滚动时,瓦片的顺序和对齐都会变得混乱。该应用程序如下,MainActivity.java

package renish.gridview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {
    GridView gridView;
    String[] text = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomGrid adapter = new CustomGrid(MainActivity.this, text, imageId);
        gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(adapter);

    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="renish.gridview.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidthUniform"/>
</RelativeLayout>



CustomGrid.java

package renish.gridview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class CustomGrid extends BaseAdapter {
    private Context context;
    private String[] text;
    private int[] imageId;

    public CustomGrid(Context context, String[] text, int[] imageId){
        this.context = context;
        this.text = text;
        this.imageId = imageId;
    }
    @Override
    public int getCount() {
        return text.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View grid;
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            grid = inflater.inflate(R.layout.grid_single, null);
            TextView textView = (TextView) grid.findViewById(R.id.textView);
            ImageView imageView = (ImageView) grid.findViewById(R.id.imageView);
            textView.setText(text[position]);
            Picasso.with(context)
                    .load("http://www.funsundivetravel.com/wp-content/uploads/2015/02/200x300.gif")
                    .into(imageView);
        } else {
            grid = (View) convertView;
        }
        return grid;
    }
}


grid_single.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="103dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="renish.gridview">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


当我运行该应用程序时,输出很好,但是每次滚动时文本的顺序和图像的对齐都会变得混乱。请为此提出解决方案。

最佳答案

像这样在getView()类中更改CustomGrid方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        grid = inflater.inflate(R.layout.grid_single, null);
    } else {
        grid = (View) convertView;
    }
    TextView textView = (TextView) grid.findViewById(R.id.textView);
    ImageView imageView = (ImageView) grid.findViewById(R.id.imageView);
    textView.setText(text[position]);
    Picasso.with(context)
                .load("http://www.funsundivetravel.com/wp-content/uploads/2015/02/200x300.gif")
                .into(imageView);
    return grid;
}


除此之外,您还可以更好地了解View Holder模式。

关于java - Android Grid View的顺序和对齐方式会在滚动时弄乱吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35257327/

10-13 03:18