2016-05-09 48 views
-1

我想在选中时更改网格项目的文本颜色,并且一次只选择一个项目,这已经完成,但是如果gridview有很多项目,如果我选择任何项目和滚动gridview那么它将在网格中选择随机项目,并允许选择多个项目。 我尝试了很多答案,但没有找到任何单一的解决方案。 任何人都可以有任何想法?如果滚动,GridView更改其视图

我试着回答下面通过给出的链接,但它并没有解决我的问题

When the GridView scrolls, it changes its view's activated status

我会分享我的适配器getView方法和网格视图

的onItemSelected()方法的代码
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long arg3) { 

       view.setSelected(true); 
       TextView tv = (TextView) view;     // Get the current selected view as a TextView 
       tv.setTextColor(Color.parseColor("#50d1e4")); // Set the current selected item text color 
       TextView previousSelectedView = (TextView) gridview.getChildAt(previousPosition); // Get the last selected View from GridView 

       // If there is a previous selected view exists 
       if (previousPosition != -1 && previousPosition!=position) { 
        previousSelectedView.setSelected(false);      // Set the last selected View to deselect 
        previousSelectedView.setTextColor(Color.parseColor("#162750")); // Set the last selected View text color as deselected item 
       } 
       previousPosition = position; 

     } 
    }); 

这里previousPosition的初始值= -1(它是int的类型)

适配器的getView方法如下

public static class ViewHolder 
{ 
    public TextView txt_time_slot; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder view; 
    final Context context = parent.getContext(); 
    if (inflater == null) 
     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     view = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.grid_item, null); 
     view.txt_time_slot = (TextView) convertView.findViewById(R.id.txt_item_time_slot); 
     convertView.setTag(view); 
     convertView.setId(0); 
    } else { 
     view = (ViewHolder) convertView.getTag(); 
    } 

     if (listValues.get(position) != null) 
     { 
      view.txt_time_slot.setText(listValues.get(position)); 

     } 

    return convertView; 
} 

给出这里是grid_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/txt_item_time_slot" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="10:20 AM" 
     android:gravity="center_horizontal" 
     android:padding="3dp" 
     android:textColor="#162750" 
     android:textStyle="bold" 
     android:textSize="30sp"/> 
+0

其实这些都不是随机的物品,那些被重用被“选中”的项目(你叫的setSelected)。您应该在您的适配器中处理选择/取消选择状态。 – danypata

+0

谢谢@danypata我会尝试现在 –

回答

0

无需添加previousPosition变量。 在模型中添加一个参数。你有任何模型的列表,添加一个参数isSelected(布尔值)。当用户点击它时,将其设置为true。

在getView方法的适配器中,勾选isSelected项的值。您可以从列表中获取(listValue.get(position).isSelected())。根据返回值,您可以更改背景。它也可以帮助你获得选定的价值。

为前:

 Product product = mProcucts.get(position); 
    itemViewHolder.textView.setText(product.getProductName()); 
    if(product.isSelected()){ itemViewHolder.textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_button_checked)); 
    }else{ 
     itemViewHolder.textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_button_unchecked)); 
    } 
+0

@Sivivani .....它没有为我工作 –