Since the application will be displaying changes of financial stocks over time, it means that we will be working with data that's represented best as a list. For this, we will use RecyclerView.
First of all, add it to your activity_main.xml file:
<android.support.v7.widget.RecyclerView
android:id="@+id/stock_updates_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/hello_world_salute" />
Then, inject it into your code with the following:
@BindView(R.id.stock_updates_recycler_view)
RecyclerView recyclerView;
The next step is to initialize it in your activity:
private LinearLayoutManager layoutManager;
private StockDataAdapter stockDataAdapter;
...
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
stockDataAdapter = new StockDataAdapter();
recyclerView.setAdapter(stockDataAdapter);
The code for the StockDataAdapter is as illustrated:
public class StockDataAdapter extends RecyclerView.Adapter<StockUpdateViewHolder> {
private final List<String> data = new ArrayList<>();
@Override
public StockUpdateViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.stock_update_item, parent, false);
StockUpdateViewHolder vh = new StockUpdateViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(StockUpdateViewHolder holder, int
position) {
holder.stockSymbol.setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
public void add(String stockSymbol) {
this.data.add(stockSymbol);
notifyItemInserted(data.size() - 1);
}
}
Finally, we need the layout XML for the RecyclerView items and a ViewHolder for them:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp">
<TextView
android:id="@+id/stock_item_symbol"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="20dp" />
</android.support.v7.widget.CardView>
To fill in the data for this view, we will have to utilize ViewHolders from the RecyclerView class:
public class StockUpdateViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.stock_item_symbol)
TextView stockSymbol;
public StockUpdateViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}
The very last step is to populate it with the data. We will do that in a Reactive way:
Observable.just("APPL", "GOOGLE", "TWTR")
.subscribe(new Consumer<String>() {
@Override
public void accept(String stockSymbol) {
stockDataAdapter.add(stockSymbol);
}
});
This is rather simple code and I hope that most of the developers had no trouble following this. In case there were some not-so-clear bits, feel free to check out the code that's provided with this book. The examples there are fully working.