数据集更改时,项目不显示动画

时间:2017-08-11 10:20:33

标签: java android android-recyclerview

我已经修复了我的最后一个问题,即如何在不重新调整适配器的情况下刷新recyclelerview,只需调用notifyDatasetChanged()即可。现在我看到另一个问题了。问题是我的项目在调用notifyDatasetChanged()时没有显示动画。如果我重置适配器,该项目将显示动画。 当我致电notifyDatasetChanged()时,我做错了什么?

这是我的适配器代码:

public class TableAdapter extends    RecyclerView.Adapter<TableAdapter.ViewHolder>
{
private int lastPosition = -1;
private List<Table> tables;
private Context context;

public TableAdapter(Context context, List<Table> tables)
{
    this.tables = tables;
    this.context = context;
}

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

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
    holder.tvTableName.setText(tables.get(holder.getAdapterPosition()).getName());
    setAnimation(holder.itemView, holder.getAdapterPosition());
}

@Override
public int getItemCount()
{
    return tables == null ? 0 : tables.size();
}

class ViewHolder extends RecyclerView.ViewHolder
{
    private TextView tvTableName;

    ViewHolder(View itemView)
    {
        super(itemView);
        tvTableName = (TextView) itemView.findViewById(R.id.tvTableName);
    }
}

private void setAnimation(View viewToAnimate, int position)
{
    // If the bound view wasn't previously displayed on screen, it's animated
    if (position > lastPosition)
    {
        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
        animation.setDuration(500);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}

}

我打电话给notifyDatasetChanged()就像这样:

public class TableFragment extends Fragment
{
private RecyclerView rvTable;
private ListView lvFloor;
private TableState state;
private List<Table> tables= new ArrayList<>();
private TableAdapter tableAdapter;

public TableFragment()
{
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    View v = inflater.inflate(R.layout.fragment_table, container, false);
    initializeComponents(v);
    return v;
}

private void initializeComponents(View v)
{
    lvFloor = (ListView) v.findViewById(R.id.lvFloor);
    rvTable = (RecyclerView) v.findViewById(R.id.rvTable);
    tableAdapter= new TableAdapter(getActivity(),tables);
    rvTable.setAdapter(tableAdapter);
    setFloor();
}

private void setFloor()
{
    IFloorApi api = ApiClient.getClientWithAuthorization(App.getContext()).create(IFloorApi.class);
    Call<List<Floor>> call = api.getFloors();
    call.enqueue(new Callback<List<Floor>>()
    {
        @Override
        public void onResponse(@NonNull Call<List<Floor>> call, @NonNull Response<List<Floor>> response)
        {
            if (response.isSuccessful())
            {
                lvFloor.setAdapter(new FloorListAdapter(getActivity(), response.body()));
                lvFloor.setOnItemClickListener(this::floorClicked);
                //noinspection ConstantConditions
                setTable(response.body().get(0));
            }
        }

        private void floorClicked(AdapterView<?> adapterView, View view, int i, long l)
        {
            Floor floor = ((FloorListAdapter) adapterView.getAdapter()).getItem(i);
            setTable(floor);
        }

        @Override
        public void onFailure(@NonNull Call<List<Floor>> call, @NonNull Throwable t)
        {
            call.cancel();
        }
    });
}

private void setTable(Floor floor)
{
    ITableApi api = ApiClient.getClientWithAuthorization(App.getContext()).create(ITableApi.class);
    rvTable.setLayoutManager(new GridLayoutManager(getActivity(), 5));
    Call<List<Table>> call = api.getTables(floor.getId());

    call.enqueue(new Callback<List<Table>>()
    {
        @Override
        public void onResponse(@NonNull Call<List<Table>> call, @NonNull Response<List<Table>> response)
        {
            if (response.isSuccessful())
            {
                tables.clear();
                //noinspection ConstantConditions
                tables.addAll(response.body());
                tableAdapter.notifyItemRangeChanged(0,tables.size());
                tableAdapter.notifyDataSetChanged();
            }
            else
            {
                tables.clear();
                tableAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onFailure(@NonNull Call<List<Table>> call, @NonNull Throwable t)
        {
            call.cancel();
        }
    });
}

}

希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

从代码中删除以下行;

  tableAdapter.notifyItemRangeChanged(0,tables.size());