我发现许多人回答了这个问题,并且为他们工作了很多,例如holder.setIsRecyclable(true);。但是他们都没有为我工作。我似乎找不到原因……如果有人可以帮助我,我将不胜感激。我认为这与我的过滤器或适配器有关。我不太确定。
这是我的活动
public class GenreActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener{
private RecyclerView mRecyclerView;
private AdapterRecyclerView mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<DifferentGenres> differentGenres;
@Override
protected void onCreate(Bundle savedInstanceStat) {
super.onCreate(savedInstanceStat);
setContentView(R.layout.activity_genre)
//toolbar
Toolbar toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Genre");
differentGenreList();
RecyclerLayoutAdapter();
onClickGenre();
}
//methhod for clicking genre
public void onClickGenre(){
mAdapter.setmOnItemClickListener(new
AdapterRecyclerView.OnItemClickListener() {
@Override
public void onItemClick(int position) {
GenreActivity.this.mAdapter.updateList(differentGenres);
switch (position) {
case 0:
}
Toast.makeText(getApplicationContext(),""+
differentGenres.get(position).getmGenre(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(GenreActivity.this,
SelectedGenre.class);
startActivity(intent);
}
});
}
//building recycler view
public void RecyclerLayoutAdapter(){
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new AdapterRecyclerView(differentGenres);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
//arraylist for genres
public void differentGenreList(){
differentGenres = new ArrayList<>();
differentGenres.add(new DifferentGenres("Action"));
differentGenres.add(new DifferentGenres("Adventure"));
differentGenres.add(new DifferentGenres("Animation"));
differentGenres.add(new DifferentGenres("Biography"));
differentGenres.add(new DifferentGenres("Comedy"));
differentGenres.add(new DifferentGenres("Crime"));
differentGenres.add(new DifferentGenres("Drama"));
differentGenres.add(new DifferentGenres("Family"));
differentGenres.add(new DifferentGenres("Fantasy"));
differentGenres.add(new DifferentGenres("History"));
differentGenres.add(new DifferentGenres("Horror"));
differentGenres.add(new DifferentGenres("Music"));
differentGenres.add(new DifferentGenres("Musical"));
differentGenres.add(new DifferentGenres("Mystery"));
differentGenres.add(new DifferentGenres("Romance"));
differentGenres.add(new DifferentGenres("Sport"));
differentGenres.add(new DifferentGenres("Superhero"));
differentGenres.add(new DifferentGenres("Thriller"));
differentGenres.add(new DifferentGenres("War"));
}
//search icon
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
MenuItem menuItem = menu.findItem(R.id.searchGenre);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
//search filter
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String input = newText.toLowerCase();
ArrayList<DifferentGenres> filtered = new ArrayList<>();
for(DifferentGenres differentGenres : differentGenres){
if(differentGenres.getmGenre().toLowerCase().contains(
input.toLowerCase())){
filtered.add(differentGenres);
}
}
mAdapter.notifyDataSetChanged();
mAdapter.updateList(filtered);
return true;
}
}
这是我的适配器
public class AdapterRecyclerView extends
RecyclerView.Adapter<AdapterRecyclerView.GenreViewHolder> {
private ArrayList<DifferentGenres> mGenreItems;
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setmOnItemClickListener(OnItemClickListener
listener) {
mOnItemClickListener = listener;
}
public static class GenreViewHolder extends
RecyclerView.ViewHolder {
public TextView mTextView;
public GenreViewHolder(View itemView, final
OnItemClickListener listener) {
super(itemView);
mTextView = itemView.findViewById(R.id.genreText);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
listener.onItemClick(position);
}
}
});
}
}
public AdapterRecyclerView(ArrayList<DifferentGenres>
genresItems) {
mGenreItems = genresItems;
}
@NonNull
@Override
public GenreViewHolder onCreateViewHolder(@NonNull ViewGroup
parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_item,
parent, false);
GenreViewHolder genreViewHolder = new GenreViewHolder(view,
mOnItemClickListener);
return genreViewHolder;
}
@Override
public void onBindViewHolder(@NonNull GenreViewHolder holder,
int position) {
DifferentGenres currentItems = mGenreItems.get(position);
holder.mTextView.setText(currentItems.getmGenre());
}
@Override
public int getItemCount() {
return mGenreItems.size();
}
//filter searchview
public void updateList(ArrayList updated) {
mGenreItems = new ArrayList<>();
mGenreItems.addAll(updated);
notifyDataSetChanged();
}
public DifferentGenres getItem(int position){
return mGenreItems.get(position);
}
}
答案 0 :(得分:0)
onclicklistener使用来自活动的未经过滤的数据。因此适配器和活动的位置不同。
您可以通过传递对象而不是位置来解决它
public interface OnItemClickListener {
void onItemClick( DifferentGeners object );
}