I'm using a template and I replaced the placeholder data with a Retrofit 2 request that parses a JSON file of the data from an URL and puts it into an object list. I ran into a problem where my retrofit call gets executed and returns items = 0 at first, then the code executes further ahead using the value items=0 and after quite a few steps in the debugger, it comes back to the call and shows the correct response (items=12) but the Adapter/RecycleView operations seem to have already been executed and they do not use the late-arrived parsed data.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private View parent_view;
List<Task> items = new List<Task>() {...}
private RecyclerView recyclerView;
private AdapterTasks mAdapter;
private int animation_type = ItemAnimation.BOTTOM_UP;
@Override
protected void onCreate(Bundle savedInstanceState) {
items = DataGenerator.getTasksData();
items.addAll(DataGenerator.getTasksData());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_animation);
parent_view = findViewById(android.R.id.content);
initComponent();
}
private void initComponent() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
animation_type = ItemAnimation.FADE_IN;
setAdapter();
}
private void setAdapter() {
//set data and list adapter
mAdapter = new AdapterTasks(this, items, animation_type);
recyclerView.setAdapter(mAdapter);
// on item list clicked
mAdapter.setOnItemClickListener(new AdapterTasks.OnItemClickListener() {
@Override
public void onItemClick(View view, Task obj, int position) {
//open task view
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list_animation, menu);
return true;
}
Retrofit request
public static List<Task> getTasksData() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
Retrofit retrofit= new Retrofit.Builder()
.baseUrl("http://10.0.2.2/index.php/")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
TasksApi taskAPI =retrofit.create(TasksApi.class);
Call<List<Task>> call = taskAPI.getAllTasks();
call.enqueue(new Callback<List<Task>>() {
@Override
public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {
items = response.body();
}
@Override
public void onFailure(Call<List<Task>> call, Throwable t) {
}
});
return items;
}
Adapter
public class AdapterTasks extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Task> items;
private Context ctx;
private OnItemClickListener mOnItemClickListener;
private int animation_type = 0;
public interface OnItemClickListener {
void onItemClick(View view, Task obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
public AdapterTasks(Context context, List<Task> items, int animation_type) {
this.items = DataGenerator.getTasksData();
this.items.addAll(DataGenerator.getTasksData());
ctx = context;
this.animation_type = animation_type;
}
public class OriginalViewHolder extends RecyclerView.ViewHolder {
public View lyt_parent;
public TextView laikas;
public TextView klientas;
public TextView darbas;
public TextView komentaras;
public Button atlikta;
public OriginalViewHolder(View v) {
super(v);
laikas = (TextView) v.findViewById(R.id.laikas);
klientas = (TextView) v.findViewById(R.id.name);
lyt_parent = (View) v.findViewById(R.id.lyt_parent);
darbas = (TextView) v.findViewById(R.id.darbas);
komentaras = (TextView) v.findViewById(R.id.komentaras);
atlikta = (Button) v.findViewById(R.id.atlikta);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task, parent, false);
vh = new OriginalViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Log.e("onBindViewHolder", "onBindViewHolder : " + position);
if (holder instanceof OriginalViewHolder) {
OriginalViewHolder view = (OriginalViewHolder) holder;
Task t = items.get(position);
view.laikas.setText((CharSequence) t.AtlikData);
view.klientas.setText(t.KlientasID);
view.darbas.setText(t.VeiksmoID);
view.komentaras.setText(t.Komentaras);
view.lyt_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view, items.get(position), position);
}
}
});
setAnimation(view.itemView, position);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
on_attach = false;
super.onScrollStateChanged(recyclerView, newState);
}
});
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return items.size();
}
private int lastPosition = -1;
private boolean on_attach = true;
private void setAnimation(View view, int position) {
if (position > lastPosition) {
ItemAnimation.animate(view, on_attach ? position : -1, animation_type);
lastPosition = position;
}
}
}