滚动GridView并更改项目网格

时间:2016-01-23 01:24:16

标签: android gridview

我用100个项目填充我的gridView(每个项目包含三个textView),显然它需要滚动。如果我单击某个项目,其背景将变为红色。我的问题是,当我使用滚动时,我看到另一个项目也变成了红色!!!为什么呢?

在我的活动中,我有String [] subg,String [] dEstancias,String [] limp。 (每个包含100个字符串)。在我的活动中:

final CustomGrid adapter = new CustomGrid(this, subg, dEstancias, limp, previousSelectedPosition);

// Getting a reference to gridview of MainActivity
final GridView gridView = (GridView) findViewById(R.id.gridview);

// Setting an adapter containing images to the gridview
gridView.setAdapter(adapter);

gridView.setOnItemClickListener(new OnItemClickListener() {


       public void onItemClick(AdapterView parent, View v,int position, long id) 
       {             

            previousSelectedPosition = position;
            v.setBackgroundColor(Color.RED);

       }

});

在我的适配器中,我将getView作为:

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_item, parent, false);

        } else {
            grid = (View) convertView;
        }

        TextView textView = (TextView) grid.findViewById(R.id.sgob);
        TextView textView2 = (TextView) grid.findViewById(R.id.item);
        TextView textView3 = (TextView) grid.findViewById(R.id.limp);

        textView.setText(subg[position]);
        textView2.setText(estancias[position]);
        textView3.setText(limp[position]);

        return grid;
    }

我的网格项目是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:padding="40dp"
    android:layout_gravity="center"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/sgob"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:background="#2cc546"
        android:gravity="center_horizontal"
    />
    <TextView
        android:id="@+id/item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:background="#2cc546"
        android:gravity="center_horizontal"
    />

    <TextView
        android:id="@+id/limp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:background="#2cc546"
        android:gravity="center_horizontal"
    />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试使用ViewHolder模式。

在您的适配器中编辑getView,如下所示

public View getView(int position, View convertView, ViewGroup parent) {
    final MyViewHolder viewHolder;

    if (convertView == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(R.layout.grid_item, parent, false);

        viewHolder = new MyViewHolder();

        viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
        viewHolder.textView = (TextView) convertView.findViewById(R.id.sgob);
        viewHolder.textView2 = (TextView) convertView.findViewById(R.id.item);
        viewHolder.textView3 = (TextView) convertView.findViewById(R.id.limp);

        convertView.setTag(grid);
    } else {
        viewHolder = (MyViewHolder) convertView.getTag();
    }

    viewHolder.textView.setText(subg[position]);
    viewHolder.textView2.setText(estancias[position]);
    viewHolder.textView3.setText(limp[position]);

    viewHolder.layout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            v.setBackgroundColor(Color.RED);
        }
    });

    return convertView;
}

由于在ViewHolder的onClickListener中更改了背景颜色,因此您无需在onItemClickListener中再次更改背景颜色。

在网格xml文件中的LinearLayout中添加ID。 然后创建MyViewHolder.java

public class MyViewHolder {
    LinearLayout layout;
    TextView textView;
    TextView textView2;
    TextView textView3;
}