0

I am trying a calendar application. I used Baseadapter and Gridview for work the calendar. I want, when an user clicks an item in calendar, clicked items change background color.

I found several methods for this. setSelection method, requestFocus method etc. Then I decided using the requestFocus.

My Problem

When I click an item in calendar, It sends requestFocus and everything is okay. Then I click another item and It works okay. Main problem starts here, When I click an item that I've already clicked on, It doesn't work. So an item works only once. I hope you can understand my question. Please suggest a solution to me.

thank you.

PlaceholderFragment.java

calendarAdapter = new CalendarAdapter( whole_list, getActivity() );

        calendarGridView = ( GridView ) rootView.findViewById( R.id.calendarGridView );
        calendarGridView.setAdapter( calendarAdapter );
        calendarGridView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick( AdapterView< ? > parent, View view, int position, long id ) {
                DaysSettings get_spesific = whole_list.get( position );

                RelativeLayout r = ( RelativeLayout ) view.findViewById( R.id.calendar_border );
                r.setFocusable( true );
                r.setFocusableInTouchMode( true );
                r.requestFocus();
            }
        } );

calendar_inflate.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >

    <RelativeLayout
        android:background="@drawable/selector"
        android:layout_marginTop="@dimen/calendar_rl_margin"
        android:layout_marginBottom="@dimen/calendar_rl_margin"
        android:layout_width="@dimen/calendar_circle_wh"
        android:layout_height="@dimen/calendar_circle_wh"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/calendar_border">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/calendarparentlayout"
            android:layout_margin="1dp"
            >
            <ImageView
                android:contentDescription="Cinsel ilişki olduğunda kalp işareti çıkması için kullanılır."
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/had_sex"
                android:src="@drawable/heart"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:background="@drawable/circle_white"
                android:padding="3dp"
                android:visibility="gone"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:text="0"
                android:id="@+id/calendarTextiew"
                android:textColor="@color/main_color_one"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/calendarTextiew"
                android:gravity="center_horizontal">

                <ImageView
                    android:contentDescription="İlaç hatırlatırıcı resmidir."
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/medicine_reminder"
                    android:src="@drawable/pill"
                    android:layout_marginRight="2dp"
                    android:layout_marginLeft="2dp"
                    android:visibility="gone"
                    />

                <ImageView
                    android:contentDescription="Note eklendi resmidir."
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/adding_note"
                    android:src="@drawable/plus"
                    android:layout_marginRight="2dp"
                    android:layout_marginLeft="2dp"
                    android:visibility="gone"
                    />
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
Community
  • 1
  • 1
Olkunmustafa
  • 3,103
  • 9
  • 42
  • 55
  • Sir i do not think you need to use focus method, you need to use selection method, when an item is clicked it does not mean it takes focus, buttons do not take focus but they fire click events, so use `setSelection(int i)` after call `requestFocus()` with that, your problem will most likely go – Elltz Jan 21 '15 at 09:25
  • Thanks your advice @Elltz. Your methods normally work but there is a problem related to my appliaction. Please attention to my codes. When Item is clicked through setOnItemClickListener, It returns a view object and I change background color of some layouts views with this object. example = ( RelativeLayout r = ( RelativeLayout ) view.findViewById( R.id.calendar_border ); r.setFocusable( true ); r.setFocusableInTouchMode( true ); r.requestFocus(); ) – Olkunmustafa Jan 21 '15 at 09:50

0 Answers0