Guest User

Untitled

a guest
Feb 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. public class ListItem {
  2. private String title;
  3.  
  4. public ListItem(String title) {
  5. this.title = title;
  6. }
  7.  
  8. public String getTitle() {
  9. return title;
  10. }
  11. }
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14. private RecyclerView recyclerView;
  15. private RecyclerView.Adapter adapter;
  16. private List<ListItem> SensorItems;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. recyclerView = (RecyclerView) findViewById(R.id.sensorsRV);
  23. SensorItems = new ArrayList<ListItem>();
  24.  
  25. SensorManager sensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
  26. List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
  27.  
  28. for(int i = 0; i < sensorList.size(); i++){
  29. ListItem currentSensor = new ListItem(((Sensor)(sensorList.get(i))).getName());
  30. SensorItems.add(currentSensor);
  31. }
  32. adapter = new MyAdapter(SensorItems);
  33. recyclerView.setAdapter(adapter);
  34. }
  35.  
  36.  
  37. }
  38.  
  39. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  40. List <ListItem> listItems;
  41. private Context context;
  42.  
  43. public MyAdapter(List<ListItem> listItems) {
  44. this.listItems = listItems;
  45.  
  46. }
  47.  
  48. //creates whenever our view holder is created
  49. @Override
  50. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  51. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
  52. return new ViewHolder(view);
  53.  
  54. }
  55.  
  56. //this method is called after onCreateViewHolder to bind data to view holder to show actuall data to recycler view
  57. @Override
  58. public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
  59. ListItem listItem = listItems.get(position);
  60. holder.headingTextView.setText(listItem.getTitle());
  61. }
  62.  
  63. @Override
  64. public int getItemCount() {
  65. return listItems.size();
  66. }
  67.  
  68. public class ViewHolder extends RecyclerView.ViewHolder{
  69. public TextView headingTextView;
  70. Context context;
  71. public LinearLayout myLayout;
  72.  
  73.  
  74. public ViewHolder(View itemView) {
  75. super(itemView);
  76. myLayout = (LinearLayout) itemView;
  77. headingTextView = (TextView) itemView.findViewById(R.id.textVeiwHead);
  78. context = itemView.getContext();
  79. }
  80. }
  81. }
  82.  
  83. <?xml version="1.0" encoding="utf-8"?>
  84. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  85. xmlns:app="http://schemas.android.com/apk/res-auto"
  86. xmlns:tools="http://schemas.android.com/tools"
  87. android:layout_width="match_parent"
  88. android:layout_height="match_parent"
  89. tools:context="com.example.sahar.devicesensors.MainActivity">
  90.  
  91. <LinearLayout
  92. android:layout_width="match_parent"
  93. android:layout_height="match_parent"
  94. android:orientation="vertical">
  95.  
  96. <TextView
  97. android:id="@+id/textView"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:layout_gravity="center"
  101. android:layout_margin="30dp"
  102. android:text="List of Device's Sensors"
  103. android:textSize="16dp"
  104. android:textStyle="bold"
  105. android:textColor="@android:color/holo_blue_dark"/>
  106.  
  107.  
  108. <LinearLayout
  109. android:layout_width="match_parent"
  110. android:layout_height="match_parent"
  111. android:orientation="vertical">
  112.  
  113. <android.support.v7.widget.RecyclerView
  114. android:id="@+id/sensorsRV"
  115. android:layout_width="match_parent"
  116. android:layout_height="match_parent"
  117. android:padding="20dp" />
  118. </LinearLayout>
  119. </LinearLayout>
  120. </android.support.constraint.ConstraintLayout>
  121.  
  122. <?xml version="1.0" encoding="utf-8"?>
  123. <android.support.constraint.ConstraintLayout
  124. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  125. android:layout_height="match_parent">
  126.  
  127. <LinearLayout
  128. android:padding="20dp"
  129. android:layout_width="match_parent"
  130. android:layout_height="wrap_content"
  131. android:orientation="vertical">
  132. <TextView
  133. android:id="@+id/textVeiwHead"
  134. android:text="Heading"
  135. android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
  136. android:layout_width="match_parent"
  137. android:layout_height="wrap_content" />
  138. </LinearLayout>
  139.  
  140. </android.support.constraint.ConstraintLayout>
  141.  
  142. recyclerView.setAdapter(adapter);
  143. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  144.  
  145. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
  146. recyclerView.setLayoutManager(layoutManager);
  147.  
  148. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  149. recyclerView.setAdapter(adapter);
  150.  
  151. <android.support.v7.widget.RecyclerView
  152. xmlns:app="http://schemas.android.com/apk/res-auto"
  153. app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
  154.  
  155. //To show vertical scrolling list
  156. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
  157. recyclerView.setLayoutManager(linearLayoutManager);
  158.  
  159. //To show horizontal scrolling list
  160. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
  161. recyclerView.setLayoutManager(linearLayoutManager);
  162.  
  163. adapter = new MyAdapter(SensorItems);
  164. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
  165. recyclerView.setLayoutManager(mLayoutManager);
  166. recyclerView.setItemAnimator(new DefaultItemAnimator());
  167. recyclerView.setAdapter(adapter);
Add Comment
Please, Sign In to add comment