gpt4 book ai didi

android - 如何在 Android RecyclerView 适配器中过滤后获取原始项目位置?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:19 25 4
gpt4 key购买 nike

我在 android recyclerview 适配器中实现了过滤器。但问题是,当我过滤项目时,项目的位置发生了变化。我需要过滤后项目的原始位置?我怎样才能得到它?

这是我的代码:

public class SearchAdapter extends SelectableAdapter<SearchAdapter.ViewHolder> implements Filterable {

static ArrayList<SingleHadishDisplayModel> singleHadishDisplayModels;
ArrayList<SingleHadishDisplayModel> singleHadishDisplayModelsFilter;
public ViewHolder.ClickListener clickListener;

Context context;

public SearchAdapter(ArrayList<SingleHadishDisplayModel> singleHadishDisplayModels) {
this.singleHadishDisplayModelsFilter = singleHadishDisplayModels;
if (singleHadishDisplayModels != null) {
this.singleHadishDisplayModels = new ArrayList<>(singleHadishDisplayModels);
} else {
this.singleHadishDisplayModels = null;
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.search_adapter, parent, false);

ViewHolder vh = new ViewHolder(v, clickListener);
return vh;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.topic.setText(singleHadishDisplayModelsFilter.get(position).topic);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Contants.clickPos=position;
/*long id = SearchAdapter.getItemId(position);
Toast.makeText(context,Long.toString(id), Toast.LENGTH_SHORT).show();*/

Intent intent = new Intent(context, HadishDetails.class);
context.startActivity(intent);
}
});
}

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

public long getItemId(int position) {
int itemID;
// orig will be null only if we haven't filtered yet:
if (singleHadishDisplayModels == null) {
itemID = position;
} else {
itemID = singleHadishDisplayModels.indexOf(singleHadishDisplayModels.get(position));
}
return itemID;
}


@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();

if (constraint.length() == 0) {
filterResults.values = SearchAdapter.this.singleHadishDisplayModels;
return filterResults;
}

String searchString = constraint.toString().toLowerCase();

List<SingleHadishDisplayModel> filteredList = new ArrayList<>();
filterResults.values = filteredList;

for (SingleHadishDisplayModel tableSelectionDTO : SearchAdapter.this.singleHadishDisplayModels) {
if (tableSelectionDTO.topic.toLowerCase().contains(searchString)) {
filteredList.add(tableSelectionDTO);
}
}

return filterResults;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
SearchAdapter.this.singleHadishDisplayModelsFilter = (ArrayList<SingleHadishDisplayModel>) results.values;
SearchAdapter.this.notifyDataSetChanged();
}
};

return filter;
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
TextView topic;

private ClickListener listener;

public ViewHolder(View itemView, ClickListener clickListener) {
super(itemView);

this.listener = clickListener;
topic = (TextView) itemView.findViewById(R.id.topic_search);

itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}

@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClicked(getAdapterPosition());
}
}

@Override
public boolean onLongClick(View view) {
if (listener != null) {
return listener.onItemLongClicked(getAdapterPosition());
}
return false;
}

public interface ClickListener {
public void onItemClicked(int position);

public boolean onItemLongClicked(int position);
}

}
}

我的 Activity :

public class SearchActivity extends AppCompatActivity implements SearchAdapter.ViewHolder.ClickListener {
private RecyclerView mRecyclerView;
public SearchAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<SingleHadishDisplayModel> singleHadishDisplayModels;
SingleHadishDisplayModel singleHadishDisplayModel;
int positionClick;
public static final String POST_ID = "postID";

FloatingSearchView floatingSearchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//floatingSearchView= (FloatingSearchView) findViewById(R.id.floating_search_view);
singleHadishDisplayModels = new ArrayList<SingleHadishDisplayModel>();
singleHadishDisplayModels = Contants.singleHadishDisplayModelsSaved;

mRecyclerView = (RecyclerView) findViewById(R.id.search_reycler);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new SearchAdapter(singleHadishDisplayModels);
mRecyclerView.setAdapter(mAdapter);


/* floatingSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
@Override
public void onSearchTextChanged(String oldQuery, final String newQuery) {
mAdapter.clearSelection();
mAdapter.getFilter().filter(newQuery);
}
});*/

EditText et = (EditText) findViewById(R.id.search_et);
et.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
SearchActivity.this.mAdapter.getFilter().filter(cs);
Toast.makeText(SearchActivity.this, Integer.toString(arg1), Toast.LENGTH_SHORT).show();
}

public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub

}

public void afterTextChange(Editable arg0) {
// TODO Auto-generated method stub

}

public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});


}


@Override
public void onItemClicked(int position) {
positionClick = position;


// Intent intent =new Intent(SearchActivity.this,HadishDetails.class);
//startActivity(intent);

// Contants.clickPos=position;

}

@Override
public boolean onItemLongClicked(int position) {
return false;
}
}

最佳答案

使用此代码:

int pos = list.indexOf(arraylist1.get(getAdapterPosition()));

这里的 list 是原始列表,arrayList 是过滤后的项目列表。

关于android - 如何在 Android RecyclerView 适配器中过滤后获取原始项目位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599213/

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