Selector problem in RecycleView

I have my drawable selector.

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

<item android:drawable="@drawable/selected" android:state_activated="true"/>
<item android:drawable="@drawable/selected" android:state_pressed="true"/>
<item android:drawable="@drawable/selected" android:state_checked="true"/>
<item android:drawable="@drawable/selected" android:state_focused="true"/>

<item android:drawable="@drawable/unselected"/>

, and this RecycleView adapter

public class AdapterMain extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
List<String> data = Collections.emptyList();
DataMain current;
int currentPos = 0;
List<Integer> pos;
List<String> mDataset = data;
private SparseBooleanArray selectedItems;
private int focusedItem = 0;


// create constructor to innitilize context and data sent from MainActivity
public AdapterMain(Context context, List<String> data) {
    this.context = context;

    this.data = data;
}

@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

    // Handle key up and key down and attempt to move selection
    recyclerView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

            // Return false if scrolled to the bounds and allow focus to move off the list
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    return tryMoveSelection(lm, 1);
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    return tryMoveSelection(lm, -1);
                }
            }

            return false;
        }
    });
}

// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {


    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.container_main, parent, false);
    MyHolder holder = new MyHolder(view);

    return holder;
}

private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {
    int tryFocusItem = focusedItem + direction;

    // If still within valid bounds, move the selection, notify to redraw, and scroll
    if (tryFocusItem >= 0 && tryFocusItem < getItemCount()) {
        notifyItemChanged(focusedItem);
        focusedItem = tryFocusItem;
        notifyItemChanged(focusedItem);
        lm.scrollToPosition(focusedItem);
        return true;
    }

    return false;
}

// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    //final String element = mDataset.get(position);

    // Get current position of item in recyclerview to bind data and assign values from list
    MyHolder myHolder = (MyHolder) holder;
    String current = data.get(position);
    myHolder.textTitle.setText(current);


    //myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

        // load image into imageview using glide
//        Glide.with(context).load("http://192.168.1.7/test/images/" + 

current.fishImage)
//                .placeholder(R.drawable.ic_img_error)
//                .error(R.drawable.ic_img_error)
//                .into(myHolder.ivFish);

}

// return total item from List
@Override
public int getItemCount() {
    return data.size();
}


private class MyHolder extends RecyclerView.ViewHolder  {

    TextView textTitle;
    ImageView ivAds;
    TextView textPrice;

    // create constructor to get widget reference
    public MyHolder(View itemView) {
        super(itemView);
        textTitle = (TextView) itemView.findViewById(R.id.itemMainJson);

        itemView.setClickable(true);
        itemView.hasFocusable();
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Redraw the old selection and the new
                notifyItemChanged(focusedItem);
                focusedItem = getLayoutPosition();
                notifyItemChanged(focusedItem);
            }
        });

    }

}
}

my container_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_selector"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/all_haraj"
        android:id="@+id/itemMainJson"
        android:textSize="@dimen/text_main"
        android:textColor="@color/black"
        android:paddingRight="@dimen/padding_for_frogs"
        android:paddingLeft="@dimen/padding_for_frogs"
        android:paddingTop="@dimen/fab_press_translation_z"
        android:paddingBottom="@dimen/fab_press_translation_z"/>

    <ImageView
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/horizontal_line"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"/>

</LinearLayout>

My RecycleView OnCLick Class

public class ItemClickSupport {
private final RecyclerView mRecyclerView;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
            mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
        }
    }
};
private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if (mOnItemLongClickListener != null) {
            RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
            return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
        }
        return false;
    }
};
private RecyclerView.OnChildAttachStateChangeListener mAttachListener
        = new RecyclerView.OnChildAttachStateChangeListener() {
    @Override
    public void onChildViewAttachedToWindow(View view) {
        if (mOnItemClickListener != null) {
            view.setOnClickListener(mOnClickListener);
        }
        if (mOnItemLongClickListener != null) {
            view.setOnLongClickListener(mOnLongClickListener);
        }
    }

    @Override
    public void onChildViewDetachedFromWindow(View view) {

    }
};

private ItemClickSupport(RecyclerView recyclerView) {
    mRecyclerView = recyclerView;
    mRecyclerView.setTag(R.id.item_click_support, this);
    mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
}

public static ItemClickSupport addTo(RecyclerView view) {
    ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
    if (support == null) {
        support = new ItemClickSupport(view);
    }
    return support;
}

public static ItemClickSupport removeFrom(RecyclerView view) {
    ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
    if (support != null) {
        support.detach(view);
    }
    return support;
}

public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
    mOnItemClickListener = listener;
    return this;
}

public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
    mOnItemLongClickListener = listener;
    return this;
}

private void detach(RecyclerView view) {
    view.removeOnChildAttachStateChangeListener(mAttachListener);
    view.setTag(R.id.item_click_support, null);
}

public interface OnItemClickListener {

    void onItemClicked(RecyclerView recyclerView, int position, View v);
}

public interface OnItemLongClickListener {

    boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
}
}

My problem with the selector, when I select an item, I need to double-click to make the interaction, and before I added clickable, and focusableand focusableTouchModeto my Layoutaction, generated after the first click, but with the selector disappear quickly and do not focus, I want to focus on the item I selected, but with one click not twice.

Thank you in advance:)

+6
source share
1 answer

, , , xml ( Java ).

, "" "" , "". :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/selected" android:state_activated="true"/>
    <item android:drawable="@drawable/selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/selected" android:state_selected="true"/>
    <item android:drawable="@drawable/unselected"/>
</selector>

, , , , , . , , . , item_view, .

    android:background="@drawable/item_selector"

( ). , .

+2

Source: https://habr.com/ru/post/1015899/


All Articles