gridview 如何 select android 中的一项

gridview how to select one item in android

我的 activity 表单中有网格视图,服务器渲染数据显示在网格视图中。如果我选择网格项目想要更改背景颜色,其余所有颜色都应该与我应用的颜色相同。

At a time how to slect one item in the gridview but it is selecting multiple items

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bg"
    android:id="@+id/contact_card"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/card_details">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/cards"
                android:padding="3dp"
                android:background="@color/bg">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/speclist"
                    android:layout_gravity="left"
                    android:gravity="left"
                    android:orientation="vertical">
<!--                    <TextView-->
<!--                        android:layout_width="wrap_content"-->
<!--                        android:layout_height="wrap_content"-->
<!--                        android:layout_gravity="center"-->
<!--                        android:gravity="center"-->
<!--                        android:id="@+id/datetime"-->
<!--                        android:padding="10dp"-->
<!--                        android:textColor="@color/black"-->
<!--                        android:text="7:30"></TextView>-->

                    <Button
                        android:id="@+id/datetime"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="10:00"
                        android:background="@drawable/rectangle_border_white"
                        android:textColor="@color/theme" />

                </LinearLayout>
            </LinearLayout>



        </LinearLayout>



    </FrameLayout>

</RelativeLayout>

Adapter.java:

public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView = null;
        final Button datetime;
        LinearLayout speclist;
        if (convertView == null) {
            gridView = new View(mContext);
            convertView=inflater.inflate(R.layout.datelist, null);
            datetime=convertView.findViewById(R.id.datetime);
            datetime.setText(getAppointmentDateModels.get(position).getStartTime());
            datetime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    row_index=position;
                    notifyDataSetChanged();
                    if(row_index==position){
                        datetime.setBackgroundResource(R.drawable.rectangle_border_theme);
                        datetime.setTextColor(Color.WHITE);
                    }else {
                        datetime.setBackgroundResource(R.drawable.rectangle_border_white);
                        datetime.setTextColor(Color.rgb(73,179,206));
                    }


                    clickListener.myItemClick(position);
                }
            });



        }
        return convertView;
    }

您可以跟踪上次单击的位置,并以这种方式设置正确的 UI。 在您的适配器中添加最后一个位置:

 int lastPosition = -1;

getView 中,如果当前位置等于 lastPositiononClick[,您将设置正确的颜色=19=] 您将更新新的点击位置:

    public View getView(final int position, View convertView, ViewGroup parent) {
    ...

    if (convertView == null) {
        ...
        if (position == lastPosition) {
            datetime.setBackgroundResource(R.drawable.rectangle_border_theme);
            datetime.setTextColor(Color.WHITE);
        } else {
            datetime.setBackgroundResource(R.drawable.rectangle_border_white);
            datetime.setTextColor(Color.rgb(73,179,206));
        }

        datetime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastPosition = position;
                notifyDataSetChanged();
            }
        });
    }
    return convertView;
}