gpt4 book ai didi

android - RecyclerView Drawer - 设置选择

转载 作者:行者123 更新时间:2023-11-30 02:10:46 27 4
gpt4 key购买 nike

我正在尝试编写我自己的 recyclerview 选择器算法。到目前为止,除了处理第一个选择之外,我已经成功地编写了部分代码。基本上我想将所选项目的背景设置为蓝色,其他地方设置为白色(与 ListView 的 setSelection 相同)。

所以,思路是:

  1. 在 Adapter 的 onCreateViewHolder 方法中为第一个元素设置蓝色背景。
  2. 在ActivityMain中,定义一个实例View变量navMenuSelection来存储当前选择
  3. 在recyclerView的onclick监听器中,将点击 View 的背景设置为蓝色,将navMenuSelection的背景设置为白色,并将navMenuSelection更新为点击 View

一切正常,除了:1.无法用第一个 View 初始化navMenuSelection。尝试在 onPostCreate 方法中使用 mDrawerLayout.getChildAt(0),但返回 null

  1. 如何在 savedInstanceState 包中传递 View navMenuSelection?

任何想法将不胜感激。

public class ActivityMain extends AppCompatActivity {
private View navMenuSelection = null; // Select current view

protected void onCreate(Bundle savedInstanceState) {
// ALL THE CODES TO DEFINE RECYCLERVIEW

mRecyclerView.addOnItemTouchListener(new RecycleTouchListener(this, new ClickListner() {
@Override
public void onClick(View view, int position) {
if(view != navMenuSelection){
setNavItemSelected(view);
removeNavItemSelected(navMenuSelection);
navMenuSelection = view;
mDrawerLayout.closeDrawers();
}
}
}));
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
navMenuSelection = mDrawerLayout.getChildAt(0);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
//savedInstanceState.put???("currselection", navMenuSelection); // HOW TO DO THAT
super.onSaveInstanceState(savedInstanceState);
}

public void setNavItemSelected(View v){
if(v!= null) {
v.setBackgroundColor(R.color.BLUE));
}
}

public void removeNavItemSelected(View v){
if(v!= null) {
v.setBackgroundColor(R.color.WHITE));
}
}
}

适配器类(将 onClick 事件移动到适配器后)

public class NavDrawerAdapter extends RecyclerView.Adapter<NavDrawerAdapter.ViewHolder> {

private String[] mNavTitles; // stores title
private int[] mIcons; // stores icon

private Context context;
private int oldpostion = 0;

public NavDrawerAdapter(Context context, String Titles[], int[] Icons){
this.context = context;
mNavTitles = Titles;
mIcons = Icons;
}

public class ViewHolder extends RecyclerView.ViewHolder {

TextView textView;
ImageView imageView;

public ViewHolder (View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.title);
imageView = (ImageView) itemView.findViewById(R.id.icon);
}
}

@Override
public NavDrawerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navdrawer_item,parent,false);
return new ViewHolder(v,viewType);
}

@Override
public void onBindViewHolder(NavDrawerAdapter.ViewHolder holder, final int position) {
holder.textView.setText(mNavTitles[position]);
holder.imageView.setImageResource(mIcons[position]);
if(position == 0) {
setNavItemSelected(holder.itemView);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position != oldpostion){
setNavItemSelected(v);
//removeNavItemSelected(OLD VIEW);
oldpostion = position;
}
}
});
}

@Override
public int getItemCount() {
return mNavTitles.length;
}

@Override
public int getItemViewType(int position) {
return 1;
}

public void setNavItemSelected(View v){
if(v!= null) {
v.setBackgroundColor(context.getResources().getColor(R.color.navdrawer_item_selected_bg));
TextView tview = (TextView) v.findViewById(R.id.title);
tview.setTextColor(context.getResources().getColor(R.color.navdrawer_item_selected_text));
}
}

public void removeNavItemSelected(View v){
if(v!= null) {
v.setBackgroundResource(R.drawable.list_selector_nav_drawer);
TextView tview = (TextView) v.findViewById(R.id.title);
tview.setTextColor(context.getResources().getColorStateList(R.color.list_selector_nav_drawer_text));
}
}
}

最佳答案

关于您的问题:

  1. 要在 RecyclerView 中获得您需要的 View ,您可以使用 mRecyclerView.findViewHolderForAdapterPosition(position)
  2. 我可能是错的,但我认为你只需要保存 View 的位置,而不是 View 本身,它只是 int

我之前使用的另一种模式是向您的 RecyclerView.Adapter 添加一个 bool 数组,该数组保存有关应激活哪些 View (与数据集合中的位置相关联)的信息在此刻。基于这个数组,我在 onBindViewHolder() 中更改了 View 的背景。

如果回收站 View 足够短,可以在单个屏幕上看到,则您当前的解决方案是可行的。但是一旦它开始回收它的旧 View 持有者,您就会在未选定的 View 上重用“选定的”背景 - 所有这些都是因为您记住了 View ,而不是与它们相关的数据位置。

编辑

我将在此处放置适配器中的一些相关代码,以说明我对实现的想法。在我的方法中,我为 activated 状态使用了带有“选定”背景的可绘制状态列表,但您可以通过手动设置背景来完成所有相同的操作。

public class RecyclerAdapter
extends RecyclerView.Adapter<RecyclerAdapter.YourViewHolder> {

private final List<...> data;
private final RecyclerAdapterCallbacks listener; //e.g. for activity callbacks
private final List<Boolean> activation;

public RecyclerAdapter(List<...> data, RecyclerAdapterCallbacks listener) {
this.data = data;
this.listener = listener;
this.activation = new ArrayList<>(data.size());
fillActivationList();
}

private void fillActivationList() {
int size = data.size();
for (int i = 0; i < size; i++) activation.add(false);
}

//------YourViewHolder implementation here------

public interface RecyclerAdapterCallbacks { //Callbacks interface if needed
void onRowSelected(... dataPiece);

void onRowDeselected();
}

@Override
public WordsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//your code
}

@Override
public void onBindViewHolder(YourViewHolder holder, final int position) {
holder.field.setText(data.get(position).getString());

holder.itemView.setActivated(activation.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean previousState = activation.get(position);
deactivateAll();
activation.set(position, !previousState);
notifyDataSetChanged();
if (!previousState) {
listener.onRowSelected(data.get(position));
} else {
listener.onRowDeselected();
}
}
});
}

private void deactivateAll() {
Collections.fill(activation, false);
}

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

public void clearResults() {
notifyItemRangeRemoved(0, data.size());
data.clear();
activation.clear();
}

public void add(Word item) {
data.add(item);
notifyItemInserted(data.size() - 1);
activation.add(false);
}

关于android - RecyclerView Drawer - 设置选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30135877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com