【问题标题】:gridview cell view position changes after scrolling ...color is set to different cells other than the clicked one滚动后gridview单元格视图位置发生变化...颜色设置为单击的单元格以外的其他单元格
【发布时间】:2013-02-27 22:32:05
【问题描述】:

我有一个 5 列的 girdview,我想为整行着色,这意味着单击时有 5 个单元格。单击可以正常工作,但是当我在单击颜色时滚动它们时,会设置两行或三行稍后的单元格,因为它会更改视图位置。

这是我的 Gridview:

  <GridView
    android:id="@+id/gridView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/CornflowerBlue"
    android:gravity="center"
    android:horizontalSpacing="5dp"
    android:numColumns="5"
    android:verticalSpacing="5dp">
</GridView>  

具有自定义布局:

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/LightBlue"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="22sp" />

还有我的 gridview 适配器代码:

    public class Fragment2 extends Fragment {
     ............................


gridView2 = (GridView) getView().findViewById(R.id.gridView2);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
                getActivity(), R.layout.custom_layout, stg1); //stg1-array
        gridView2.setAdapter(adapter2);         
        gridView2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String message;

                int p = (int) Math.ceil(position / 5) * 5; //to color 5 cells from starting on click

                try{
            gridView2.getChildAt(p).setBackgroundColor(Color.GREEN);
            gridView2.getChildAt(p+1).setBackgroundColor(Color.GREEN);
            gridView2.getChildAt(p+2).setBackgroundColor(Color.GREEN);
            gridView2.getChildAt(p+3).setBackgroundColor(Color.GREEN);
            gridView2.getChildAt(p+4).setBackgroundColor(Color.GREEN);



            }
        });

最初它工作正常,但在滚动颜色设置为不同于单击的单元格后?我应该怎么办??我不想使用基本适配器....

【问题讨论】:

  • downvoter 说明原因??
  • 不只是通过检查位置来设置颜色,而是尝试创建gridview的元素列表并根据列表的索引来决定,这样位置在滚动时不会改变。
  • dude click 事件工作正常,即使我得到了点击项目的详细信息,我只是说视图的颜色,即滚动后单元格视图的位置会发生变化,因此颜色没有设置为正确的单元格......我不明白你写了什么???
  • 你对我的问题投了反对票吗???
  • 我没有投反对票。我的意思是把你改变颜色的位置保存到一些带有帮助类的arraylist。

标签: android gridview android-gridview


【解决方案1】:

试试这个,让我知道它是如何工作的。

public int p=-1;

 /*
...OTHER STUFF...

*/

gridView2 = (GridView) getView().findViewById(R.id.gridView2);

ListAdapter<String> adapter2 = new ListAdapter<String>(
            getActivity(), R.layout.custom_layout, stg1); //stg1-array
    gridView2.setAdapter(adapter2);         
    gridView2.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String message;

            int p = (int) Math.ceil(position / 5) * 5; //to color 5 cells from starting on click
        }
    });

/*
...OTHER STUFF...

*/

public class ListAdapter extends ArrayAdapter<String> {
private List<String> items;

public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}

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

View v = convertView;

if (v == null) {
    LayoutInflater vi;
    vi = LayoutInflater.from(getContext());
    v = vi.inflate(R.layout.custom_layout, null);
    if(position==p) v.setBackgroundColor(Color.GREEN);
    if(position==p+1) v.setBackgroundColor(Color.GREEN);
    if(position==p+2) v.setBackgroundColor(Color.GREEN);
    if(position==p+3) v.setBackgroundColor(Color.GREEN);
    if(position==p+4) v.setBackgroundColor(Color.GREEN);
}      

return v;

}
}

【讨论】:

  • 也尝试了一些修改,但它没有在网格中显示数组??即使你的努力+1 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 2023-03-14
  • 2018-10-04
  • 2013-01-03
  • 1970-01-01
  • 2013-03-23
相关资源
最近更新 更多