当前位置: 首页 > 知识库问答 >
问题:

Android GridView选择器问题

端木兴国
2023-03-14

我在我的应用程序中使用StaggeredGridView和我创建的自定义选择器,但我的选择器肯定出了很大的问题,因为当我在网格视图中选择一个项目时,所有项目都将其颜色更改为绿色。

下面是网格视图项的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/grid_item_selector">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ro.gebs.zonizbeacon.ui.custom.ScaledNetworkImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ro.gebs.zonizbeacon.ui.custom.AngledTextView
            android:id="@+id/expired_tv"
            android:layout_width="@dimen/size_favorite_angle_text"
            android:layout_height="@dimen/size_favorite_angle_text"
            app:angledText="@string/expired"
            app:backgroundColor="@android:color/holo_orange_dark"
            app:angledTextColor="@android:color/white"
            app:angledTextSize="@dimen/font_size_plus" />
    </RelativeLayout>


    <ro.gebs.zonizbeacon.ui.custom.fonts.CustomFontTextView
        android:id="@+id/store_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/default_padding"
        android:textSize="@dimen/font_size_normal"
        android:text="Zara"
        app:fontName="Bold" />

    <ro.gebs.zonizbeacon.ui.custom.fonts.CustomFontTextView
        android:id="@+id/offer_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fontName="Regular"
        android:paddingLeft="@dimen/default_padding"
        android:paddingRight="@dimen/default_padding"
        android:paddingBottom="@dimen/default_padding"
        android:textSize="@dimen/font_size_small"
        android:text="@string/info_about" />

</LinearLayout>

这是我的选择器:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@color/green_main_color" />
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@color/green_main_color" />
    <item android:state_enabled="true" android:state_selected="true" android:drawable="@color/green_main_color" />
    <item android:drawable="@android:color/white" />
</selector>

这是我的适配器类:

    public class FavoritesAdapter extends ArrayAdapter<Offer> {
    LayoutInflater mLayoutInflater;

    public FavoritesAdapter(Context context, List<Offer> objects) {
        super(context, 0, objects);
        mLayoutInflater = LayoutInflater.from(context);
    }

   /* @Override
    public long getItemId(int position) {
        return position;
    }*/

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

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_favorite_offer, null);
            holder = new ViewHolder();
            holder.imageView = (ScaledNetworkImageView) convertView.findViewById(R.id.imageView1);
            holder.offerText = (CustomFontTextView) convertView.findViewById(R.id.offer_text);
            holder.storeName = (CustomFontTextView) convertView.findViewById(R.id.store_name);
            holder.expiredTv = (AngledTextView) convertView.findViewById(R.id.expired_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //holder.offerText.setText("POSITION + "+position);
        final Offer offer = getItem(position);
        Date d = Calendar.getInstance().getTime();

        if (offer.getActiveUntil() < d.getTime()) {
            holder.expiredTv.setVisibility(View.VISIBLE);
        } else {
            holder.expiredTv.setVisibility(View.INVISIBLE);
        }


        ImageUtils.load(holder.imageView, offer.getPngLink(), R.drawable.image_placeholder, R.drawable.image_placeholder);
        holder.offerText.setText(offer.getDescription());
        holder.storeName.setText(offer.getStoreName());

        return convertView;
    }

    static class ViewHolder {
        AngledTextView expiredTv;
        ScaledNetworkImageView imageView;
        CustomFontTextView offerText;
        CustomFontTextView storeName;
    }

}

在我的片段中:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_favorites, container, false);

        gridView = (StaggeredGridView) view.findViewById(R.id.staggeredGridView1);
        //gridView.setSelector(new ColorDrawable(getResources().getColor(R.color.green_main_color)));
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        emptyText = (CustomFontTextView) view.findViewById(R.id.empty_grid_view);

        int margin = getResources().getDimensionPixelSize(R.dimen.default_padding);
        int itemMargin = getResources().getDimensionPixelSize(R.dimen.padding_tell_button);

        gridView.setItemMargin(itemMargin); // set the GridView margin
        gridView.setPadding(margin, margin, margin, 0); // have the margin on the sides as well

        mFavoritesAdapter = new FavoritesAdapter(getActivity(), new ArrayList<Offer>());
        mFavoritesAdapter.setNotifyOnChange(false);

        gridView.setAdapter(mFavoritesAdapter);
        gridView.setOnItemClickListener(new StaggeredGridView.OnItemClickListener() {
            @Override
            public void onItemClick(StaggeredGridView parent, View view, int position, long id) {
                Offer o = mFavoritesAdapter.getItem(position);
                if (o != null) {
                    Intent intent = new Intent(getActivity(), WebViewActivity.class);
                    intent.putExtra(WebViewActivity.BUNDLE_HTML_LINK, o.getHtmlLink());
                    intent.putExtra(WebViewActivity.STORE_NAME, o.getStoreName());
                    startActivity(intent);
                }
                //getActivity().finish();
            }
        });

        gridView.setOnItemLongClickListener(mOnItemLongClickListener);


        //restartLoader(LOADER_FAVORITES);

        return view;
    }

知道为什么它可能是错的吗?

共有1个答案

柯昱
2023-03-14

也许您的列表项目获得了聚焦的状态(虽然不是故意的)。我总是使用这样的选择器构建:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
    <item android:state_selected="true" android:drawable="@color/green_bit_transparent"/>
    <item android:state_pressed="false" android:state_focused="true" android:state_selected="false" android:drawable="@color/green_bit_transparent" />
    <item android:state_pressed="true" android:state_selected="false" android:drawable="@color/green" />
    <item android:drawable="@android:color/transparent" />
</selector>
 类似资料:
  • 连接池维持一份连接清单,它决定节点在什么时候从活节点转变为死节点(或死节点转变为活节点)。然而连接池选择连接对象时是没有逻辑的,这份工作属于 Selector 类。 选择器(selector)的工作是从连接数组中返回一个连接。和连接池一样,也有几种选择器可供选择。 RoundRobinSelector(默认) 选择器通过轮询调度的方式来返回连接。例如在第一个请求中选择节点1,在第二请求中选择节点

  • 选择器 CasperJS大量使用选择器来处理DOM,并且可以透明地使用CSS3或XPath表达式。 接下来的例子都基于下面的HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <title>My page</title> </head> <body> <h1 class="page-title">Hell

  • 选择器 See the Pen FEND_Selectors by Li Xinyang (@li-xinyang) on CodePen. 选择器可被看做表达式,通过它可以选择相应的元素并应用不同的样式。 简单选择器 元素选择器 组合选择器 简单选择器 简单选择器可组合使用。 标签选择器 <div> <p>Sample Paragraph</p> <p>Sample Paragraph<

  • 选择器是jQuery的核心。一个选择器写出来类似$('#dom-id')。 为什么jQuery要发明选择器?回顾一下DOM操作中我们经常使用的代码: // 按ID查找: var a = document.getElementById('dom-id'); // 按tag查找: var divs = document.getElementsByTagName('div'); // 查找<p cl

  • 问题内容: 我有一个JTree,其节点由JLabel和JComboBox组成。我想要的是,当单击时,选定的JComboBox会展开,但似乎第一次单击是JTree本身(?)占用的,因此我必须单击两次。 这是我的代码: 问题答案: 这是一个简短的示例: 这对我有用:Bug ID:JDK-8023474第一次按下鼠标并没有开始在JTree中进行编辑

  • 问题内容: 我的CSS定位器有问题。我为父母有一个独特的标签,从那里我可以得到所需的孩子。 该类与所有下拉列表相似,因此唯一会改变的值是。另外,有时我需要取消选择它们。 为此,我有一个定位器。 如您所见,第一部分仍然相同。 所以我的问题是是否有可能编写一些方法(或任何其他方式)来更改定位器的最后一部分?我有600多个下拉菜单,并且为600个新定位器创建了一个目标,这使我发疯。 对我来说,执行类似操