如何正确突出显示回收视图中选定的项目?

我试图使用一个 RecyclerView作为一个水平的 ListView。我试图找出如何突出选定的项目。当我点击其中一个项目时,它会被选中并被正确地突出显示,但是当我点击另一个项目时,第二个项目会被突出显示。

下面是我的 onClick 函数:

@Override
public void onClick(View view) {


if(selectedListItem!=null){
Log.d(TAG, "selectedListItem " + getPosition() + " " + item);
selectedListItem.setBackgroundColor(Color.RED);
}
Log.d(TAG, "onClick " + getPosition() + " " + item);
viewHolderListener.onIndexChanged(getPosition());
selectedPosition = getPosition();
view.setBackgroundColor(Color.CYAN);
selectedListItem = view;
}

以下是 onBindViewHolder:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.setItem(fruitsData[position]);
if(selectedPosition == position)
viewHolder.itemView.setBackgroundColor(Color.CYAN);
else
viewHolder.itemView.setBackgroundColor(Color.RED);


}
247490 次浏览

如前所述,应该在 onCreateViewHolder 中为 viewHolder 设置侦听器。也就是说,下面的实现最初是针对多重选择的,但是我在代码片段中进行了修改,强制执行单一选择。(* 1)

// an array of selected items (Integer indices)
private final ArrayList<Integer> selected = new ArrayList<>();


// items coming into view
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// each time an item comes into view, its position is checked
// against "selected" indices
if (!selected.contains(position)){
// view not selected
holder.parent.setBackgroundColor(Color.LTGRAY);
}
else
// view is selected
holder.parent.setBackgroundColor(Color.CYAN);
}


// selecting items
@Override
public boolean onLongClick(View v) {
        

// select (set color) immediately.
v.setBackgroundColor(Color.CYAN);


// (*1)
// forcing single selection here...
if (selected.isEmpty()){
selected.add(position); // (done - see note)
}else {
int oldSelected = selected.get(0);
selected.clear(); // (*1)... and here.
selected.add(position);
// note: We do not notify that an item has been selected
// because that work is done here.  We instead send
// notifications for items which have been deselected.
notifyItemChanged(oldSelected);
}
return false;
}

看看我的解决方案。我认为你应该设置在持有人选择的位置,并通过它作为标签的看法。 视图应该在 onCreateViewHolder (...)方法中设置。还有为视图(如 OnClickListener 或 LongClickListener)设置侦听器的正确位置。

请查看下面的示例并阅读代码注释。

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
//Here is current selection position
private int mSelectedPosition = 0;
private OnMyListItemClick mOnMainMenuClickListener = OnMyListItemClick.NULL;


...


// constructor, method which allow to set list yourObjectList


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//here you prepare your view
// inflate it
// set listener for it
final ViewHolder result = new ViewHolder(view);
final View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.your_view_layout, parent, false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//here you set your current position from holder of clicked view
mSelectedPosition = result.getAdapterPosition();


//here you pass object from your list - item value which you clicked
mOnMainMenuClickListener.onMyListItemClick(yourObjectList.get(mSelectedPosition));


//here you inform view that something was change - view will be invalidated
notifyDataSetChanged();
}
});
return result;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final YourObject yourObject = yourObjectList.get(position);


holder.bind(yourObject);
if(mSelectedPosition == position)
holder.itemView.setBackgroundColor(Color.CYAN);
else
holder.itemView.setBackgroundColor(Color.RED);
}


// you can create your own listener which you set for adapter
public void setOnMainMenuClickListener(OnMyListItemClick onMyListItemClick) {
mOnMainMenuClickListener = onMyListItemClick == null ? OnMyListItemClick.NULL : onMyListItemClick;
}


static class ViewHolder extends RecyclerView.ViewHolder {




ViewHolder(View view) {
super(view);
}


private void bind(YourObject object){
//bind view with yourObject
}
}


public interface OnMyListItemClick {
OnMyListItemClick NULL = new OnMyListItemClick() {
@Override
public void onMyListItemClick(YourObject item) {


}
};


void onMyListItemClick(YourObject item);
}
}

我编写了一个基本适配器类,用于自动处理项目选择和回收视图。只需从中派生适配器,并使用 state _ select 的可绘制状态列表,就像使用列表视图一样。

我有一个 在这里发布博客关于它,但这里的代码:

public abstract class TrackSelectionAdapter<VH extends TrackSelectionAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
// Start with first item selected
private int focusedItem = 0;


@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;
}
});
}


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;
}


@Override
public void onBindViewHolder(VH viewHolder, int i) {
// Set selected state; use a state list drawable to style the view
viewHolder.itemView.setSelected(focusedItem == i);
}


public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);


// Handle item click and set the selection
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Redraw the old selection and the new
notifyItemChanged(focusedItem);
focusedItem = getLayoutPosition();
notifyItemChanged(focusedItem);
}
});
}
}
}

这个方法很简单。

在珊瑚适配器类中有一个 private int selectedPos = RecyclerView.NO_POSITION;,在 onBindViewHolder 方法下尝试:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.itemView.setSelected(selectedPos == position);


}

并在 OnClick 事件中修改:

@Override
public void onClick(View view) {
notifyItemChanged(selectedPos);
selectedPos = getLayoutPosition();
notifyItemChanged(selectedPos);
}

对于导航抽屉和其他回收视图项目适配器来说,它非常有用。

注意: 一定要使用背景颜色在你的布局使用选择器,如 colabug 澄清:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pressed_color" android:state_pressed="true"/>
<item android:drawable="@color/selected_color" android:state_selected="true"/>
<item android:drawable="@color/focused_color" android:state_focused="true"/>
</selector>

否则 setSelected (. .)将什么也不做,从而使该解决方案无用。

我认为,我已经找到了最好的教程关于如何使用回收视图的所有基本功能,我们需要(单 + 多选择,突出显示,涟漪,点击和删除在多选择等。.).

就是这里—— > http://enoent.fr/blog/2015/01/18/recyclerview-basics/

在此基础上,我创建了一个库“ FlexibleAdapter”,它扩展了一个 SelectableAdapter。 我认为这一定是 Adapter 的责任,实际上您不需要每次都重写 Adapter 的基本功能,让一个库来做,这样您就可以重用相同的实现。

这个适配器是非常快的,它工作的框(你不需要扩展它) ; 你自定义的项目为每个视图类型你需要; ViewHolder 是预定义的: 公共事件已经实现: 单击和长点击; 它保持状态后旋转和 远远不止

请有一个期待和感觉自由实施它在您的项目。

Https://github.com/davideas/flexibleadapter

还可以使用 Wiki。

回收视图中没有像 ListView 和 GridView 这样的选择器,但是你可以尝试下面的东西,它对我很有用

创建如下所示的可绘制选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@color/blue" />
</shape>
</item>


<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>

然后将此绘图工具设置为回收视图行布局的背景,如下所示

android:background="@drawable/selector"

使用接口和回调的决策。 创建具有选择和取消选择状态的接口:

public interface ItemTouchHelperViewHolder {
/**
* Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped.
* Implementations should update the item view to indicate it's active state.
*/
void onItemSelected();




/**
* Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item
* state should be cleared.
*/
void onItemClear();
}

在 ViewHolder 中实现界面:

   public static class ItemViewHolder extends RecyclerView.ViewHolder implements
ItemTouchHelperViewHolder {


public LinearLayout container;
public PositionCardView content;


public ItemViewHolder(View itemView) {
super(itemView);
container = (LinearLayout) itemView;
content = (PositionCardView) itemView.findViewById(R.id.content);


}


@Override
public void onItemSelected() {
/**
* Here change of item
*/
container.setBackgroundColor(Color.LTGRAY);
}


@Override
public void onItemClear() {
/**
* Here change of item
*/
container.setBackgroundColor(Color.WHITE);
}
}

在回调中运行状态更改:

public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {


private final ItemTouchHelperAdapter mAdapter;


public ItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
this.mAdapter = adapter;
}


@Override
public boolean isLongPressDragEnabled() {
return true;
}


@Override
public boolean isItemViewSwipeEnabled() {
return true;
}


@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}


@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
...
}


@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
...
}


@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
ItemTouchHelperViewHolder itemViewHolder =
(ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}


@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
if (viewHolder instanceof ItemTouchHelperViewHolder) {
ItemTouchHelperViewHolder itemViewHolder =
(ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}
}

使用回调创建回收视图(示例) :

mAdapter = new BuyItemsRecyclerListAdapter(MainActivity.this, positionsList, new ArrayList<BuyItem>());
positionsList.setAdapter(mAdapter);
positionsList.setLayoutManager(new LinearLayoutManager(this));
ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mAdapter);
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(positionsList);

更多内容见 iPaulPro: https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-6a6f0c422efd#.6gh29uaaz

更新[2017年7月26日] :

正如 帕万在关于 IDE 的注释中提到的,关于不要使用 IDE 的警告 固定位置,我刚刚修改了我的代码如下 听众被移动到 ViewHolder,在那里我得到的位置 使用 getAdapterPosition()方法

int selected_position = 0; // You have to set this globally in the Adapter class


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Item item = items.get(position);


// Here I am just highlighting the background
holder.itemView.setBackgroundColor(selected_position == position ? Color.GREEN : Color.TRANSPARENT);
}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}


@Override
public void onClick(View v) {
// Below line is just like a safety check, because sometimes holder could be null,
// in that case, getAdapterPosition() will return RecyclerView.NO_POSITION
if (getAdapterPosition() == RecyclerView.NO_POSITION) return;


// Updating old as well as new positions
notifyItemChanged(selected_position);
selected_position = getAdapterPosition();
notifyItemChanged(selected_position);


// Do your another stuff for your onClick
}
}

希望这个能帮上忙。

我也有同样的问题,我用以下方法解决:

用于在 createViewholder 中创建 Row 的 xml 文件,只需添加以下代码行:

 android:clickable="true"
android:focusableInTouchMode="true"
android:background="?attr/selectableItemBackgroundBorderless"

或者,如果您使用 frameLayout 作为行项的父项,那么:

android:clickable="true"
android:focusableInTouchMode="true"
android:foreground="?attr/selectableItemBackgroundBorderless"

在 java 代码中,视图持有者中添加了单击侦听器:

@Override
public void onClick(View v) {


//ur other code here
v.setPressed(true);
}

这是我的解决方案,您可以设置一个项目(或组) ,并取消选择它与另一次点击:

 private final ArrayList<Integer> seleccionados = new ArrayList<>();
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
viewHolder.san.setText(android_versions.get(i).getAndroid_version_name());
if (!seleccionados.contains(i)){
viewHolder.inside.setCardBackgroundColor(Color.LTGRAY);
}
else {
viewHolder.inside.setCardBackgroundColor(Color.BLUE);
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (seleccionados.contains(i)){
seleccionados.remove(seleccionados.indexOf(i));
viewHolder.inside.setCardBackgroundColor(Color.LTGRAY);
} else {
seleccionados.add(i);
viewHolder.inside.setCardBackgroundColor(Color.BLUE);
}
}
});
}

设置 private int selected_position = -1;以防止在开始时选择任何项目。

 @Override
public void onBindViewHolder(final OrdersHolder holder, final int position) {
final Order order = orders.get(position);
holder.bind(order);
if(selected_position == position){
//changes background color of selected item in RecyclerView
holder.itemView.setBackgroundColor(Color.GREEN);
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
//this updated an order property by status in DB
order.setProductStatus("0");
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//status switch and DB update
if (order.getProductStatus().equals("0")) {
order.setProductStatus("1");
notifyItemChanged(selected_position);
selected_position = position;
notifyItemChanged(selected_position);
} else {
if (order.getProductStatus().equals("1")){
//calls for interface implementation in
//MainActivity which opens a new fragment with
//selected item details
listener.onOrderSelected(order);
}
}
}
});
}

如果没有背景颜色,只需添加 android:background="?attr/selectableItemBackgroundBorderless"就可以了,但是不要忘记使用 setSelected 方法。如果你有不同的背景颜色,我只是使用这个(我使用数据绑定) ;

在 onClick 函数中设置 isSelected

b.setIsSelected(true);

并将其添加到 xml;

android:background="@{ isSelected ? @color/{color selected} : @color/{color not selected} }"

我在网上找不到解决这个问题的好办法,于是我自己解决了。很多人都有这个问题。因此,我想在这里分享我的解决方案。

在滚动时,行被回收。因此,选中的复选框和突出显示的行不能正常工作。我通过编写下面的适配器类解决了这个问题。

我还实施了一个完整的项目。在这个项目中,您可以选择多个复选框。包括选中复选框的行突出显示。更重要的是,它们在滚动时不会丢失。你可透过以下连结下载:

Https://www.dropbox.com/s/ssm58w62gw32i29/recyclerview_checkbox_highlight.zip?dl=0

    public class RV_Adapter extends RecyclerView.Adapter<RV_Adapter.ViewHolder> {
public ArrayList<String> list;
boolean[] checkBoxState;
MainActivity mainActivity;
MyFragment myFragment;
View firstview;


private Context context;


FrameLayout framelayout;


public RV_Adapter() {


}


public RV_Adapter(Context context, MyFragment m, ArrayList<String> list ) {
this.list = list;
myFragment = m;
this.context = context;
mainActivity = (MainActivity) context;
checkBoxState = new boolean[list.size()];
// relativeLayoutState = new boolean[list.size()];
}


public class ViewHolder extends RecyclerView.ViewHolder  {
public TextView textView;
public CheckBox checkBox;
RelativeLayout relativeLayout;
MainActivity mainActivity;
MyFragment myFragment;
public ViewHolder(View v,MainActivity mainActivity,MyFragment m) {
super(v);
textView = (TextView) v.findViewById(R.id.tv_foodname);
/**/
checkBox= (CheckBox) v.findViewById(R.id.checkBox);
relativeLayout = (RelativeLayout)v.findViewById(R.id.relativelayout);
this.mainActivity = mainActivity;
this.myFragment = m;
framelayout = (FrameLayout) v.findViewById(R.id.framelayout);
framelayout.setOnLongClickListener(m);
}


}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
firstview = inflater.inflate(R.layout.row, parent, false);
return new ViewHolder(firstview,mainActivity, myFragment);
}


@Override
public void onBindViewHolder( final ViewHolder holder,  final int position) {


holder.textView.setText(list.get(position));


holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});


// When action mode is active, checkboxes are displayed on each row, handle views(move icons) on each row are disappered.
if(!myFragment.is_in_action_mode)
{


holder.checkBox.setVisibility(View.GONE);
}
else
{
holder.checkBox.setVisibility(View.VISIBLE);
holder.checkBox.setChecked(false);
}


holder.checkBox.setTag(position);


holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isPressed()) // ekrandan kaybolan checkbox'lar otomatik olarak state degistiriyordu ve bu listener method cagiriliyordu, bunu onlemek icin isPressed() method'u ile kullanici mi basmis diye kontrol ediyorum.
{
int getPosition = (Integer) compoundButton.getTag();  // Here we get the position that we have set for the checkbox using setTag.
checkBoxState[getPosition] = compoundButton.isChecked(); // Set the value of checkbox to maintain its state.
//relativeLayoutState[getPosition] = compoundButton.isChecked();


if(checkBoxState[getPosition] && getPosition == position )
holder.relativeLayout.setBackgroundResource(R.color.food_selected); /** Change background color of the selected items in list view  **/
else
holder.relativeLayout.setBackgroundResource(R.color.food_unselected); /** Change background color of the selected items in list view  **/
myFragment.prepareselection(compoundButton, getPosition, holder.relativeLayout);


}
}
});
holder.checkBox.setChecked(checkBoxState[position]);


if(checkBoxState[position]  )
holder.relativeLayout.setBackgroundResource(R.color.food_selected); /** Change background color of the selected items in list view  **/
else
holder.relativeLayout.setBackgroundResource(R.color.food_unselected);
}






@Override
public int getItemCount() {
return list.size();
}


public void updateList(ArrayList<String> newList){
this.list = newList;
checkBoxState = new boolean[list.size()+1];
}


public void resetCheckBoxState(){
checkBoxState = null;
checkBoxState = new boolean[list.size()];
}


}

应用程序截图:

screen1 screen2 screen3 screen4

选择:

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

在列表项布局中将其设置为背景

   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/selector_attentions_list_item"
android:layout_width="match_parent"
android:layout_height="64dp">

在适配器中,将 OnClickListener 添加到视图(onBind 方法)

 @Suppress("UNCHECKED_CAST")
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


fun bindItems(item: T) {
initItemView(itemView, item)
itemView.tag = item
if (isClickable) {
itemView.setOnClickListener(onClickListener)
}
}
}

在 onClick 事件中激活视图:

 fun onItemClicked(view: View){
view.isActivated = true
}

@ zIronManBox 的回答完美无缺,虽然在回收视图中没有未选项和未选项的功能。

那么

与前面一样,添加一个私有的 int selectedPos = 召回视图.NO _ POSITION; 在召回视图适配器类中,并在 onBindViewHolder 方法下:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.itemView.setSelected(selectedPos == position);


}

还有在你的 OnClick 活动中:

@Override
public void onClick(View view) {
notifyItemChanged(selectedPos);
selectedPos = getLayoutPosition();
notifyItemChanged(selectedPos);
}

还要在布局中添加以下选择器(可绘制) ,其中包括 State _ select = “ false”具有透明颜色:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pressed_color" android:state_pressed="true"/>
<item android:drawable="@color/selected_color" android:state_selected="true"/>
<item android:drawable="@color/focused_color" android:state_focused="true"/>
<item android:drawable="@android:color/transparent" android:state_selected="false"/>
</selector>

否则 setSelected (. .)将什么也不做,从而使该解决方案无用。

以程序化的方式

注意: 不使用任何文件选择器。

涉及的步骤:

  1. 创建一个全局位置变量,即 int selectedPosition = -1;

整数:

-1默认没有选择

0用于默认的第一项选择

  1. 检查变量是否等于当前位置 position

    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {
    if(selectedPosition == position)
    holder.itemView.setBackgroundColor(Color.GREEN); //selected
    else
    holder.itemView.setBackgroundColor(Color.BLACK); //not selected
    ....
    }
    
  2. 使用 setOnClickListener方法在项目之间导航

    holder.itemView.setOnClickListener(v ->
    {
    if(selectedPosition == position)
    {
    selectedPosition = -1;
    notifyDataSetChanged();
    return;
    }
    
    
    selectedPosition = position;
    notifyDataSetChanged();
    
    
    // TODO: Put your itemview clicked functionality below
    });
    

注意

  • 使用此代码可以对项目创建切换效果:

          if(selectedPosition == position)
    {
    selectedPosition = -1;
    notifyDataSetChanged();
    return;
    }
    
    
    selectedPosition = position;
    notifyDataSetChanged();
    
  • 仅使用此代码创建列表的经典选择方式(一件一件来)

          selectedPosition = position;
    notifyDataSetChanged();