我试图采用以下的Searchview功能: github.com/Wrdlbrnft/Searchable-RecyclerView-Demo(不能发布超过2个链接,抱歉)
我试图将它添加到项目中。但是,当我这样做时,我的结果会被过滤,但在用户修改或退格时不会被添加回来。
代码几乎相同。我没有用适配器改变任何东西。
CustomAdapter.java 公共类CustomAdapter扩展了RecyclerView.Adapter {
// Dataset = list of players and their attributes
private final ArrayList<PlayerData> dataSet;
private final List<PlayerData> filteredList;
private ArrayList<PlayerData> originalItems;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewPos;
ImageView imageViewFace;
ImageView imageTeamLogo;
TextView textViewTeam;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewPos = (TextView) itemView.findViewById(R.id.textViewPos);
this.imageViewFace = (ImageView) itemView.findViewById(R.id.imageViewFace);
this.imageTeamLogo = (ImageView) itemView.findViewById(R.id.ImgTeamLogo);
this.textViewTeam = (TextView) itemView.findViewById(R.id.textViewTeam);
}
}
//dataset = data from Main Activity
public CustomAdapter(ArrayList<PlayerData> data) {
this.dataSet = data;
this.filteredList = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int Position) {
// called everytime user scrolls up or down
TextView textViewName = holder.textViewName;
TextView textViewPos = holder.textViewPos;
ImageView imageViewFace = holder.imageViewFace;
ImageView imageViewLogo = holder.imageTeamLogo;
TextView textViewTeam = holder.textViewTeam;
textViewName.setText(dataSet.get(Position).getName());
textViewPos.setText(dataSet.get(Position).getPos());
textViewTeam.setText(dataSet.get(Position).getTeam());
Context context = imageViewFace.getContext();
Context context2 = imageViewLogo.getContext();
Picasso.with(context)
.load(dataSet.get(Position).getFace())
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_3d_rotation)
.into(holder.imageViewFace);
Glide.with(context2).load(dataSet.get(Position).getTeamLogo()).into(holder.imageTeamLogo);
}
@Override
public int getItemCount() {
return dataSet.size();
}
public void animateTo(ArrayList<PlayerData> players) {
//checks what query found and adds / removes results
// problem is, it is not adding back results
applyAndAnimateRemovals(players);
applyAndAnimateAdditions(players);
applyAndAnimateMovedItems(players);
}
private void applyAndAnimateRemovals(ArrayList<PlayerData> newPlayers) {
for (int i = dataSet.size() - 1; i >= 0; i--) {
final PlayerData player = dataSet.get(i);
if (!newPlayers.contains(player)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(ArrayList<PlayerData> newPlayers) {
for (int i = 0, count = newPlayers.size(); i < count; i++) {
final PlayerData player = newPlayers.get(i);
if (!dataSet.contains(player)) {
addItem(i, player);
}
}
}
private void applyAndAnimateMovedItems(ArrayList<PlayerData> newPlayers) {
for (int toPosition = newPlayers.size() - 1; toPosition >= 0; toPosition--) {
final PlayerData player = newPlayers.get(toPosition);
final int fromPosition = dataSet.indexOf(player);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public PlayerData removeItem(int position) {
final PlayerData player = dataSet.remove(position);
notifyItemRemoved(position);
return player;
}
public void addItem(int position, PlayerData player) {
dataSet.add(position, player);
// this code is not working for whatever reason)
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final PlayerData player = dataSet.remove(fromPosition);
dataSet.add(toPosition, player);
notifyItemMoved(fromPosition, toPosition);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
private static RecyclerView recyclerView;
private static CustomAdapter adapter;
private static ArrayList<PlayerData> data;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<PlayerData>();
addPlayers();
adapter = new CustomAdapter(data);
recyclerView.setAdapter(adapter);
}
public interface Constants {
String LOG = "com.vogella.testapp";
}
private void addPlayers() {
PlayerData player = new PlayerData("Chris Smith", "#", R.drawable.auser, R.drawable.aquestion, "First", 0);
data.add(0,player);
player = new PlayerData("Bobby Richars","#", R.drawable.auser, R.drawable.aquestion, "Second", 9);
data.add(player);
player = new PlayerData("Steven Williams", "#", R.drawable.auser, R.drawable.aquestion, "Third", 1);
data.add(player);
player = new PlayerData("Tony Lloyd", "#", R.drawable.auser, R.drawable.aquestion, "Fourth", 2);
data.add(player);
player = new PlayerData("Henry Gates", "#", R.drawable.auser,R.drawable.aquestion, "Fifth", 3);
data.add(player);
player = new PlayerData("Robert Marks", "#", R.drawable.auser, R.drawable.aquestion, "Sixth", 4);
data.add(player);
player = new PlayerData("Edward Jackson", "#", R.drawable.auser, R.drawable.aquestion, "SEVENTH", 5);
data.add(player);
player = new PlayerData("Zack Jordan", "#", R.drawable.auser, R.drawable.aquestion, "Eighth", 6);
data.add(player);
player = new PlayerData("Christopher Thomas", "#", R.drawable.auser, R.drawable.aquestion, "Ninth", 7);
data.add(player);
player = new PlayerData("Bruce Allen", "#", R.drawable.auser, R.drawable.aquestion, "Tenth", 8);
data.add(player);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextChange(String query) {
final ArrayList<PlayerData> filteredModelList = filter(data, query);
adapter.animateTo(filteredModelList);
recyclerView.scrollToPosition(0);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private ArrayList<PlayerData> filter(ArrayList<PlayerData> players, String query) {
query = query.toLowerCase();
final ArrayList<PlayerData> filteredModelList = new ArrayList<>();
for (PlayerData player : players) {
final String text = player.getName().toLowerCase() + player.getPos().toLowerCase()+ player.getTeam().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(player);
}
}
return filteredModelList;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
return true;
}
}
答案 0 :(得分:0)
事实证明这是一个小错误。
自定义适配器
public CustomAdapter(ArrayList<PlayerData> data) {
this.dataSet = data;
this.filteredList = data;
}
进入
public CustomAdapter(ArrayList<PlayerData> data) {
this.dataSet = new ArrayList<>(data);
刚删除它,它的工作原理。