问题描述
我有5列的girdview,我要的颜色整行,这意味着5个细胞的点击。点击工作正常,但是当我滚动他们在点击色设置两个或三排以后的细胞,因为它改变视图位置。
I have a girdview with 5 columns and i want to color the whole row that means 5 cells on clicking. click works fine but when i scroll them on clicking color is set two or three row later cells as it changes the view position.
这是我的GridView:
this is my 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>
有custonm布局:
which has custonm layout:
<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" />
和我的$ C $下GridView的适配器:
and my code for gridview adapter:
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);
}
});
起初,它工作正常,但滚动后的颜色设置为不同的细胞比点击的另一个?我该怎么办??我不想使用基本适配器....
Initially it works fine but after scrolling color is set to different cells other than the clicked one?? what should i do?? i dont wanna use base adapter....
推荐答案
试试这个,让我知道它是如何工作的。
Try this and let me know how it works.
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;
}
}
这篇关于滚动后gridview的细胞观察位置的变化...色被设定为不同的细胞比点击一个其它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!