我有我的可繪製選擇器。RecycleView選擇器問題
<?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"/>
這我RecycleView適配器
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);
}
});
}
}
}
我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>
我RecycleView ONCLICK類
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);
}
}
我的問題與選擇,當我選擇的項目,我必須點擊兩次,使互動,我加入clickable
和focusable
和focusableTouchMode
我Layout
之前,第一次點擊後產生的作用但是選擇器快速消失並且沒有集中,我想讓它專注於我選擇的項目,但只需點擊一次而不是兩次。
感謝提前:)
對所選項目使用布爾值。將它們存儲在一個數組中。當您滾動時,Recyclerview會記住它們。使用selectitembackground(忘記)。它應該工作 –
@ Log.d我在我的夢中看到這個問題:D – CODE1010101010101
很久以前,它應該工作。谷歌例如 –