Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ListItem {
- private String title;
- public ListItem(String title) {
- this.title = title;
- }
- public String getTitle() {
- return title;
- }
- }
- public class MainActivity extends AppCompatActivity {
- private RecyclerView recyclerView;
- private RecyclerView.Adapter adapter;
- private List<ListItem> SensorItems;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- recyclerView = (RecyclerView) findViewById(R.id.sensorsRV);
- SensorItems = new ArrayList<ListItem>();
- SensorManager sensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
- List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
- for(int i = 0; i < sensorList.size(); i++){
- ListItem currentSensor = new ListItem(((Sensor)(sensorList.get(i))).getName());
- SensorItems.add(currentSensor);
- }
- adapter = new MyAdapter(SensorItems);
- recyclerView.setAdapter(adapter);
- }
- }
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
- List <ListItem> listItems;
- private Context context;
- public MyAdapter(List<ListItem> listItems) {
- this.listItems = listItems;
- }
- //creates whenever our view holder is created
- @Override
- public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
- return new ViewHolder(view);
- }
- //this method is called after onCreateViewHolder to bind data to view holder to show actuall data to recycler view
- @Override
- public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
- ListItem listItem = listItems.get(position);
- holder.headingTextView.setText(listItem.getTitle());
- }
- @Override
- public int getItemCount() {
- return listItems.size();
- }
- public class ViewHolder extends RecyclerView.ViewHolder{
- public TextView headingTextView;
- Context context;
- public LinearLayout myLayout;
- public ViewHolder(View itemView) {
- super(itemView);
- myLayout = (LinearLayout) itemView;
- headingTextView = (TextView) itemView.findViewById(R.id.textVeiwHead);
- context = itemView.getContext();
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.sahar.devicesensors.MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_margin="30dp"
- android:text="List of Device's Sensors"
- android:textSize="16dp"
- android:textStyle="bold"
- android:textColor="@android:color/holo_blue_dark"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/sensorsRV"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="20dp" />
- </LinearLayout>
- </LinearLayout>
- </android.support.constraint.ConstraintLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:padding="20dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textVeiwHead"
- android:text="Heading"
- android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </android.support.constraint.ConstraintLayout>
- recyclerView.setAdapter(adapter);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
- recyclerView.setLayoutManager(layoutManager);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- recyclerView.setAdapter(adapter);
- <android.support.v7.widget.RecyclerView
- xmlns:app="http://schemas.android.com/apk/res-auto"
- app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
- //To show vertical scrolling list
- LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
- recyclerView.setLayoutManager(linearLayoutManager);
- //To show horizontal scrolling list
- LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
- recyclerView.setLayoutManager(linearLayoutManager);
- adapter = new MyAdapter(SensorItems);
- RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
- recyclerView.setLayoutManager(mLayoutManager);
- recyclerView.setItemAnimator(new DefaultItemAnimator());
- recyclerView.setAdapter(adapter);
Add Comment
Please, Sign In to add comment