带有文本字段的GridView在滚动时不能正常工作

时间:2017-06-29 20:11:46

标签: android android-layout android-gridview

我有一个非常简单的网格视图,其中每个项目包含两个文本字段。滚动时,网格视图项在卷轴页面下方的项目上采用随机值。我是新手,所以请帮忙! 我的适配器如下

public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] web;
private final String[] web1;

public CustomGrid(Context c,String[] web,String[] web1 ) {
    mContext = c;
    this.web1 = web1;
    this.web = web;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return web.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
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_single, null);
        TextView textView1 = (TextView) grid.findViewById(R.id.ItemType);
        TextView textView2 = (TextView) grid.findViewById(R.id.ItemOutput);
        textView1.setText(web[position]);
        textView2.setText(web1[position]);

    } else {
        grid = (View) convertView;
    }

    return grid;
}

}

我的mainactivity.java代码

public class MainActivity extends Activity {
GridView grid;
ArrayList<String> abc = new ArrayList<String>(Arrays.asList("Google",
        "Github",
        "Instagram",
        "Facebook",
        "Vine",
        "Pinterest",
        "Quora",
        "Twitter",
        "Vimeo",
        "WordPress",
        "Youtube",
        "Stumbleupon",
        "SoundCloud",
        "Reddit",
        "Blogger"));

ArrayList<String> abc1 = new ArrayList<String>(Arrays.asList("1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15"));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final String[] web = abc.toArray(new String[abc.size()]);
    final String[] web1 = abc1.toArray(new String[abc1.size()]);

    CustomGrid adapter = new CustomGrid(MainActivity.this, web1, web);
    grid=(GridView)findViewById(R.id.gridView);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

在getView(int position,View convertView,ViewGroup parent)

int actualPosition;    

    if(convertView != null) {
        actualPosition = ((GridView)parent).getPositionForView(convertView);
    }

它会返回实际位置,无论是什么也避免了我几次得到的零错误。

这是因为在滚动时,GridView会更新,但不会更新getView()中的位置变量。