Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Gridview所选项目保持高亮显示并确认图像_Android_Android Listview_Android Gridview - Fatal编程技术网

Android Gridview所选项目保持高亮显示并确认图像

Android Gridview所选项目保持高亮显示并确认图像,android,android-listview,android-gridview,Android,Android Listview,Android Gridview,我正在用GridView开发一个android应用程序。gridView包含按钮。我已经用按钮适配器定制了我的gridView 问题是:我需要用边框突出显示所选项目。现在,在发布新闻后,选择正在消失。我不是安卓专家。因此,按下按钮后的下一步是显示云形状的图像,显示“确认” 这正是我所需要的。在res文件夹中创建一个名为drawable的文件夹。现在在drawable文件夹中创建一个xml文件,将其命名为任意名称(用小写字母),并将此代码放入该文件中 <?xml version="1.0"

我正在用
GridView
开发一个android应用程序。gridView包含按钮。我已经用按钮适配器定制了我的gridView

问题是:我需要用边框突出显示所选项目。现在,在发布新闻后,选择正在消失。
我不是安卓专家。因此,按下按钮后的下一步是显示云形状的图像,显示“确认”


这正是我所需要的。

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>
更新 您可以在自定义适配器中使用类似以下内容来实现所需:

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;
    }

}

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>
更新 您可以在自定义适配器中使用类似以下内容来实现所需:

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;
    }

}

我已经尝试过setSelection(true)。我没有这个概念,这就是为什么我不能做任何其他事情的原因。你能建议一种方法吗?我在github上的drawable foldergreenDroid中有一个选择器,类似这样的东西。你应该试试这个。首先查看图像,然后尝试实现此功能,也可以使用弹出窗口@qadir我想你错了,当用户点击一个按钮时,我需要重新定位弹出窗口,弹出窗口应该在按下的按钮上方。我怀疑的是,当弹出窗口出现时,我们是否可以按该行中的另一个按钮?@qadir对不起,qadir。我知道你不理解我的评论。问题是假设我们有一排按钮。当用户按下第一个按钮时,弹出窗口应位于第一个按钮上方,按下第一个按钮后,一个用户决定按下第二个按钮。然后边框和弹出框应该出现在第二个按钮上。希望你能理解。我已经尝试过setSelection(true)。我没有这个概念,这就是为什么我不能做任何其他事情的原因。你能建议一种方法吗?我在github上的可绘图foldergreenDroid中有一个选择器,我有一个类似的东西。你应该试试这个。首先查看图像,然后尝试实现此功能,也可以使用弹出窗口@qadir我想你错了,当用户点击一个按钮时,我需要重新定位弹出窗口,弹出窗口应该在按下的按钮上方。我怀疑的是,当弹出窗口出现时,我们是否可以按该行中的另一个按钮?@qadir对不起,qadir。我知道你不理解我的评论。问题是假设我们有一排按钮。当用户按下第一个按钮时,弹出窗口应位于第一个按钮上方,按下第一个按钮后,一个用户决定按下第二个按钮。然后边框和弹出框应该出现在第二个按钮上。希望您能看到。抱歉Vipul:(再次出现相同的问题,它正在消失选择。没有高亮显示Orry Vipull:(没有发生任何事情)最后,您可以添加***android:drawSelectorOnTop=“true”***属性在您的网格视图中?因为这在我的代码中起作用。您能提供示例代码吗?然后它将对我非常有用。提前感谢代码中没有什么特殊之处。这些都是重要的部分。抱歉Vipul:(再次出现相同的问题,它正在消失选择。不是HighlightingSorry Vipull….:(没有发生任何事情OK最后你能在你的网格视图中添加***android:drawSelectorOnTop=“true”***属性吗?因为这在我的代码中起作用。你能给我提供示例代码吗?然后它对我会非常有用。提前感谢代码中没有什么特别之处。这些是重要的部分。