2013-04-29 57 views
0

我正在开发与GridView的Android应用程序。 gridView包含按钮。我已经使用按钮适配器定制了我的GridView。Gridview选定的项目保持突出显示,并确认图像

问题是:我需要保持选中的项目用边框突出显示。现在选择正在释放印刷机后消失。
我不是Android专家。因此,按下按钮后的下一步是显示一个云形状图像,其中显示“确认”。

This是我确切需要的。

+0

我曾尝试setSelection(true)。我没有这个概念,这就是为什么我不能做任何事情。请给我建议一个方法,我有可选文件夹中的选择器 – devian 2013-04-29 10:27:45

+0

github上的greenDroid有这样的东西。你应该试试这个。 https://github.com/cyrilmottier/GreenDroid。首先看到图像,然后尝试执行此 – 2013-04-29 10:53:01

+0

也去弹出窗口。 http://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/ – 2013-04-29 11:05:30

回答

3

在您的res文件夹中创建一个名为drawable的文件夹。现在在drawable文件夹中创建一个xml文件,将其命名为任何内容(以小型文件形式)并将此代码放入文件中。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_focused="true" android:state_pressed="true"><shape> 

      <!-- <solid android:color="#CCCCCC"/> --> 

      <gradient android:endColor="#67A7F8" android:startColor="#1067C8" /> 

      <stroke android:width="1dp" android:color="#000000" /> 

      <corners android:radius="8dp" /> 
     </shape></item> 
    <item android:state_focused="false" android:state_pressed="true"><shape> 

      <!-- <solid android:color="#07B107"/> --> 

      <gradient android:endColor="#67A7F8" android:startColor="#1067C8" /> 

      <stroke android:width="1dp" android:color="#000000" /> 

      <corners android:radius="8dp" /> 
     </shape></item> 
    <item android:state_focused="true" android:state_pressed="false"><shape> 
      <solid android:color="#FFFFFF" /> 

      <stroke android:width="1dp" android:color="#0055FF" /> 

      <corners android:radius="8dp" /> 
     </shape></item> 
    <item android:state_focused="false" android:state_pressed="false"><shape> 
      <gradient android:angle="270" android:centerColor="#FFFFFF" android:endColor="#FFFFFF" android:startColor="#F2F2F2" /> 

      <stroke android:width="0.8dp" android:color="#000000" /> 

      <corners android:radius="12dp" /> 
     </shape></item> 
    <item android:state_enabled="true"><shape> 
      <padding android:bottom="4dp" android:left="5dp" android:right="4dp" android:top="4dp" /> 
     </shape></item> 

</selector> 

现在在你的GridView代码中设置android:drawSelectorOnTop="true"android:listSelector属性,然后选择你刚才创建的drawable。它会解决你的问题。

网格视图代码看起来是这样的:

<GridView 
     android:id="@+id/lstFrames_available_frames" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="1" 
     android:animateLayoutChanges="true" 
     android:columnWidth="100dp" 
     android:gravity="center" 
     android:horizontalSpacing="3dp" 
     android:listSelector="@drawable/round_buttons" 
     android:numColumns="auto_fit" 
     android:drawSelectorOnTop="true" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="3dp" > 
    </GridView> 

UPDATE 您可以使用这样的事情在你的自定义适配器来实现你想要的:

public class AlbumCoverAdapter extends BaseAdapter { 

    private Activity activity; 

    private static LayoutInflater inflater = null; 

    private int mSelected; 

    public AlbumCoverAdapter(Activity a) { 
     activity = a; 

     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return 50; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public static class ViewHolder { 
     public TextView txtCaption; 

     public ImageView imgImage; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     ViewHolder holder; 
     if (convertView == null) { 
      vi = inflater.inflate(R.layout.grid_adapter, null); 
      holder = new ViewHolder(); 
      holder.txtCaption = (TextView)vi.findViewById(R.id.txtGridText); 
      holder.txtCaption.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        mSelected = (Integer)arg0.getTag(); 
        notifyDataSetChanged(); 
       } 
      }); 
      vi.setTag(holder); 
     } else 
      holder = (ViewHolder)vi.getTag(); 

     try { 
      holder.txtCaption.setTag(position); 

      if (position == mSelected) { 
       holder.txtCaption.setBackgroundResource(R.drawable.round_corner_background); 
      } else { 
       holder.txtCaption.setBackgroundDrawable(null); 

      } 

      holder.txtCaption.setText("Item: " + position); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return vi; 
    } 

} 
+0

对不起Vipul :(再次同样的问题,它消失的选择。不突出 – devian 2013-04-29 11:19:51

+0

好吧。我已更新我的代码。请检查新代码。 – 2013-04-29 11:21:54

+0

对不起Vipull .. :(没什么发生 – devian 2013-04-29 11:26:10

相关问题