滚动浏览Android网格视图的顺序和对齐搞乱?

时间:2016-02-07 18:26:06

标签: java android gridview

我正在尝试在网格视图上显示图像和文本,但每次滚动时,图块的顺序和对齐都会混乱。
该应用程序如下,

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>


当我运行应用程序时输出正常但每次滚动时文本的顺序和图像的对齐都会混乱。
请为此建议解决方法。

1 个答案:

答案 0 :(得分:1)

更改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模式的信息。