Posted by
sapan
on
- Get link
- Other Apps
public class DummyModel { //text String textData = "not set yet !!"; public DummyModel() { } /** * constructor to set the dummy text * @param data */ public DummyModel(String data) { textData = data; } static public List<DummyModel> getDummyModel(int length) { List<DummyModel> dummyModelsList = new ArrayList<>(); for (int i = 0; i < length; i++) { dummyModelsList.add(new DummyModel("This is the model "+i)); } return dummyModelsList; } /** * just a setter method !Not used! * @param text */ public void setText(String text) { this.textData = text; } }
public class DummyVH extends RecyclerView.ViewHolder { TextView textView; /** * Constructor to set the items * @param itemView */ public DummyVH(View itemView) { super(itemView); textView = (TextView)itemView.findViewById(R.id.textView); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(RecylerActivity.context, "yooo "+textView.getText(), Toast.LENGTH_SHORT).show(); } }); } /** * functiion to set the data * @param dummyModel */ public void bindVH(DummyModel dummyModel) { textView.setText(dummyModel.textData); } }
/** * THis class will be the data adapter for the recycler view * This class must exxtend the Recycler view adapter. */ public class RvDataAdpt2 extends RecyclerView.Adapter { List<DummyModel> dummyModelsList; public RvDataAdpt2(List<DummyModel> listDummyModels) { dummyModelsList = listDummyModels; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //lets populate our recyler view with the item created; //get the view from the layout inflator // third parameter is set to false to prevent viewgroup to attach to root View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false); return new DummyVH(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { //bind the view with the holder ((DummyVH)holder).bindVH(dummyModelsList.get(position)); } @Override public int getItemCount() { return dummyModelsList.size(); // to display the 100 items } }
public class RecylerActivity extends AppCompatActivity { public static Context context; //Below are the componets which are required by the Recycler view RecyclerView recyclerView; //this will hold the recycler view from the layout RecyclerView.Adapter mAdapter; //this will hold the adapter for the recycler view RecyclerView.LayoutManager mLayoutmanager; //holds the layout manager protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; setContentView(R.layout.activity_recyler); //layout which contain the recycler view //Find the Recycler view recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view); //got the recycler view from the layout //set the layout manager for the recycler view //standard layout managers( LinearLayoutManager or GridLayoutManager) can be used, or implement your own. //Layout Manager tells how the item are shown in your Recycler View mLayoutmanager = new LinearLayoutManager(this); recyclerView.setLayoutManager(mLayoutmanager); //set the data adapter //Adapter contain the Data Which need to be shown in the view // mAdapter = new RvDataAdpt(); mAdapter = new RvDataAdpt2(DummyModel.getDummyModel(100)); recyclerView.setAdapter(mAdapter); } }
Comments
Post a Comment