Created
June 16, 2015 13:15
-
-
Save tsiougkosn/05ebb0e6809f58f85e29 to your computer and use it in GitHub Desktop.
RecyclerView Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
RecyclerView recyclerView = new RecyclerView(this); | |
setContentView(recyclerView); | |
final String[] dances = new String[]{ | |
"Greek folk dances", "Tango", "Salsa", "Rumba", "Hip Hop", "Break Dance", "Belly Dancing", "Pole Dancing", "Capoeira", "Polka", "Irish Step Dancing" | |
}; | |
recyclerView.setLayoutManager( new LinearLayoutManager(this) ); | |
recyclerView.setAdapter( new DanceAdapter(dances) ); | |
} | |
class DanceAdapter extends RecyclerView.Adapter<DanceAdapter.ItemVH> { | |
String[] data; | |
public DanceAdapter(String[] data) { | |
this.data = data; | |
} | |
@Override | |
public ItemVH onCreateViewHolder(ViewGroup parent, int i) { | |
View v = LayoutInflater.from(parent.getContext()) | |
.inflate(android.R.layout.simple_list_item_1, parent, false); | |
ItemVH vh = new ItemVH(v); | |
vh.text = (TextView) v; | |
return vh; | |
} | |
@Override | |
public void onBindViewHolder(ItemVH itemVH, int i) { | |
itemVH.text.setText( data[i] ); | |
} | |
@Override | |
public int getItemCount() { | |
return data.length; | |
} | |
class ItemVH extends RecyclerView.ViewHolder { | |
TextView text; | |
public ItemVH(View itemView) { | |
super(itemView); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment