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;
public AdapterMain(Context context, List<String> data) {
this.context = context;
this.data = data;
}
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
recyclerView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
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;
}
});
}
@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 (tryFocusItem >= 0 && tryFocusItem < getItemCount()) {
notifyItemChanged(focusedItem);
focusedItem = tryFocusItem;
notifyItemChanged(focusedItem);
lm.scrollToPosition(focusedItem);
return true;
}
return false;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder = (MyHolder) holder;
String current = data.get(position);
myHolder.textTitle.setText(current);
current.fishImage)
}
@Override
public int getItemCount() {
return data.size();
}
private class MyHolder extends RecyclerView.ViewHolder {
TextView textTitle;
ImageView ivAds;
TextView textPrice;
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) {
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 focusable
and focusableTouchMode
to my Layout
action, 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:)