Browse Source

first time RecyclerView works, doesn't look realy god but the app works

Simon Hammer 4 years ago
parent
commit
575f40403d

+ 5 - 4
app/build.gradle

@@ -40,10 +40,11 @@ dependencies {
     implementation 'androidx.appcompat:appcompat:1.2.0'
     implementation 'androidx.appcompat:appcompat:1.2.0'
     implementation 'com.google.android.material:material:1.3.0'
     implementation 'com.google.android.material:material:1.3.0'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
-    implementation 'androidx.navigation:navigation-fragment:2.2.2'
-    implementation 'androidx.navigation:navigation-ui:2.2.2'
-    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
-    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
+    implementation 'androidx.navigation:navigation-fragment:2.3.5'
+    implementation 'androidx.navigation:navigation-ui:2.3.5'
+    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
+    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
+    implementation 'androidx.recyclerview:recyclerview:1.2.0'
     testImplementation 'junit:junit:4.+'
     testImplementation 'junit:junit:4.+'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

+ 52 - 3
app/src/main/java/com/noahvogt/miniprojekt/MainActivity.java

@@ -1,12 +1,12 @@
 package com.noahvogt.miniprojekt;
 package com.noahvogt.miniprojekt;
 
 
 import android.os.Bundle;
 import android.os.Bundle;
-import android.view.View;
+import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.Menu;
+import android.view.View;
+import android.view.ViewGroup;
 
 
 
 
-import com.google.android.material.floatingactionbutton.FloatingActionButton;
-import com.google.android.material.snackbar.Snackbar;
 import com.google.android.material.navigation.NavigationView;
 import com.google.android.material.navigation.NavigationView;
 
 
 import androidx.navigation.NavController;
 import androidx.navigation.NavController;
@@ -17,10 +17,24 @@ import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.widget.Toolbar;
 import androidx.appcompat.widget.Toolbar;
 
 
+import com.noahvogt.miniprojekt.ui.home.CustomAdapter;
+import com.noahvogt.miniprojekt.ui.home.Data;
+
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+import androidx.recyclerview.widget.StaggeredGridLayoutManager;
+
+import java.util.ArrayList;
+
+
 public class MainActivity extends AppCompatActivity {
 public class MainActivity extends AppCompatActivity {
 
 
     private AppBarConfiguration mAppBarConfiguration;
     private AppBarConfiguration mAppBarConfiguration;
 
 
+    //imported by simon 2.may from RecyclerView Programm, changed to 23.may Simon to ArrayList<Data>...
+    protected ArrayList<Data> data;
+    //private static final int DATASET_COUNT = 60; //declares how far recyclerview count
+
     @Override
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         super.onCreate(savedInstanceState);
@@ -46,8 +60,36 @@ public class MainActivity extends AppCompatActivity {
         NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
         NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
         NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
         NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
         NavigationUI.setupWithNavController(navigationView, navController);
         NavigationUI.setupWithNavController(navigationView, navController);
+
+        //initDataset();
+        // Lookup the recyclerview in activity layou
+        RecyclerView recyclerView = findViewById(R.id.recyclerView);
+        // Initialize contacts
+        data = Data.createContactsList(20);
+        // Create adapter passing in the sample user data
+        CustomAdapter adapter = new CustomAdapter(data);
+        // Attach the adapter to the recyclerview to populate items
+        recyclerView.setAdapter(adapter);
+        // First param is number of columns and second param is orientation i.e Vertical or Horizontal
+        StaggeredGridLayoutManager gridLayoutManager =
+                new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
+        // Attach the layout manager to the recycler view
+        recyclerView.setLayoutManager(gridLayoutManager);
     }
     }
 
 
+    //@Override
+    //public View onCreateView(LayoutInflater inflater, ViewGroup container,
+        //                     Bundle savedInstanceState){
+      //  View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false);
+
+      //  RecyclerView recyclerView = findViewById(R.id.recyclerView);
+      //  CustomAdapter adapter = new CustomAdapter(data);
+      //  recyclerView.setAdapter(adapter);
+      //  recyclerView.setLayoutManager(new LinearLayoutManager(this));
+
+       // return rootView;
+    //}
+
     @Override
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         // Inflate the menu; this adds items to the action bar if it is present.
@@ -61,4 +103,11 @@ public class MainActivity extends AppCompatActivity {
         return NavigationUI.navigateUp(navController, mAppBarConfiguration)
         return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                 || super.onSupportNavigateUp();
                 || super.onSupportNavigateUp();
     }
     }
+
+    //private void initDataset() {
+      //  data = new String[DATASET_COUNT];
+       // for (int i = 0; i < DATASET_COUNT; i++) {
+         //   data[i] = "This is element #" + i;
+       // }
+    //}
 }
 }

+ 37 - 13
app/src/main/java/com/noahvogt/miniprojekt/ui/home/CustomAdapter.java

@@ -1,9 +1,11 @@
 package com.noahvogt.miniprojekt.ui.home;
 package com.noahvogt.miniprojekt.ui.home;
 
 
 
 
+import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewGroup;
+import android.widget.Button;
 import android.widget.TextView;
 import android.widget.TextView;
 
 
 
 
@@ -11,27 +13,31 @@ import androidx.recyclerview.widget.RecyclerView;
 
 
 import com.noahvogt.miniprojekt.R;
 import com.noahvogt.miniprojekt.R;
 
 
+import java.util.List;
+
 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
 
 
-    private String[] localDataSet;
+    private List<Data> localDataSet;
 
 
     /**
     /**
      * Provide a reference to the type of views that you are using
      * Provide a reference to the type of views that you are using
      * (custom ViewHolder).
      * (custom ViewHolder).
      */
      */
-    public static class ViewHolder extends RecyclerView.ViewHolder {
-        private final TextView textView;
+    public class ViewHolder extends RecyclerView.ViewHolder {
+        private TextView textView;
+        private Button messageButton;
 
 
         public ViewHolder(View view) {
         public ViewHolder(View view) {
             super(view);
             super(view);
             // Define click listener for the ViewHolder's View
             // Define click listener for the ViewHolder's View
 
 
             textView = (TextView) view.findViewById(R.id.textView);
             textView = (TextView) view.findViewById(R.id.textView);
+            messageButton = (Button) itemView.findViewById(R.id.message_button);
         }
         }
 
 
-        public TextView getTextView() {
-            return textView;
-        }
+        //public TextView getTextView() {
+            //return textView;
+        //}
     }
     }
 
 
     /**
     /**
@@ -40,7 +46,7 @@ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder
      * @param dataSet String[] containing the data to populate views to be used
      * @param dataSet String[] containing the data to populate views to be used
      * by RecyclerView.
      * by RecyclerView.
      */
      */
-    public CustomAdapter(String[] dataSet) {
+    public CustomAdapter(List<Data> dataSet) {
         localDataSet = dataSet;
         localDataSet = dataSet;
     }
     }
 
 
@@ -48,25 +54,43 @@ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder
     @Override
     @Override
     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
         // Create a new view, which defines the UI of the list item
         // Create a new view, which defines the UI of the list item
-        View view = LayoutInflater.from(viewGroup.getContext())
-                .inflate(R.layout.text_row_item, viewGroup, false);
+        //View view = LayoutInflater.from(viewGroup.getContext())
+                //.inflate(R.layout.text_row_item, viewGroup, false);
+
+        Context context = viewGroup.getContext();
+        LayoutInflater inflater = LayoutInflater.from(context);
 
 
-        return new ViewHolder(view);
+        // Inflate the custom layout
+        View contactView = inflater.inflate(R.layout.fragment_home, viewGroup, false); //fragment_home is just for no errors idk if it is the right file
+
+        // Return a new holder instance
+        ViewHolder view = new ViewHolder(contactView);
+        return view;
     }
     }
 
 
     // Replace the contents of a view (invoked by the layout manager)
     // Replace the contents of a view (invoked by the layout manager)
     @Override
     @Override
-    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
+    public void onBindViewHolder(ViewHolder viewHolder, int position) {
 
 
         // Get element from your dataset at this position and replace the
         // Get element from your dataset at this position and replace the
         // contents of the view with that element
         // contents of the view with that element
-        viewHolder.getTextView().setText(localDataSet[position]);
+        //viewHolder.getTextView().setText(localDataSet[position]);
+        // Get the data model based on position
+        Data contact = localDataSet.get(position);
+
+        // Set item views based on your views and data model
+        TextView textView = viewHolder.textView;
+        textView.setText(contact.getName());
+        Button button = viewHolder.messageButton;
+        button.setText(contact.isOnline() ? "Message" : "Offline");
+        button.setEnabled(contact.isOnline());
+
     }
     }
 
 
     // Return the size of your dataset (invoked by the layout manager)
     // Return the size of your dataset (invoked by the layout manager)
     @Override
     @Override
     public int getItemCount() {
     public int getItemCount() {
-        return localDataSet.length;
+        return localDataSet.size();
     }
     }
 }
 }
 
 

+ 33 - 0
app/src/main/java/com/noahvogt/miniprojekt/ui/home/Data.java

@@ -0,0 +1,33 @@
+package com.noahvogt.miniprojekt.ui.home;
+
+import java.util.ArrayList;
+
+public class Data {
+    private String mName;
+    private boolean mOnline;
+
+    public Data(String name, boolean online) {
+        mName = name;
+        mOnline = online;
+    }
+
+    public String getName() {
+        return mName;
+    }
+
+    public boolean isOnline() {
+        return mOnline;
+    }
+
+    private static int lastContactId = 0;
+
+    public static ArrayList<Data> createContactsList(int numContacts) {
+        ArrayList<Data> contacts = new ArrayList<Data>();
+
+        for (int i = 1; i <= numContacts; i++) {
+            contacts.add(new Data("Person " + ++lastContactId, i <= numContacts / 2));
+        }
+
+        return contacts;
+    }
+}

+ 1 - 1
app/src/main/java/com/noahvogt/miniprojekt/ui/home/HomeFragment.java

@@ -24,7 +24,7 @@ public class HomeFragment extends Fragment {
         homeViewModel =
         homeViewModel =
                 new ViewModelProvider(this).get(HomeViewModel.class);
                 new ViewModelProvider(this).get(HomeViewModel.class);
         View root = inflater.inflate(R.layout.fragment_home, container, false);
         View root = inflater.inflate(R.layout.fragment_home, container, false);
-        final TextView textView = root.findViewById(R.id.flower_text);
+        final TextView textView = root.findViewById(R.id.textView);
         homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
         homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
             @Override
             @Override
             public void onChanged(@Nullable String s) {
             public void onChanged(@Nullable String s) {

+ 7 - 13
app/src/main/res/layout/activity_main.xml

@@ -36,20 +36,14 @@
             android:text="Add Email" />
             android:text="Add Email" />
     </com.google.android.material.navigation.NavigationView>
     </com.google.android.material.navigation.NavigationView>
 
 
-    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
-        xmlns:app="http://schemas.android.com/apk/res-auto"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent">
-        <!--android:layout_marginLeft="@dimen/margin_medium"
-        android:layout_marginRight="@dimen/margin_medium"
-        android:gravity="center_vertical"-->
+
 
 
         <androidx.recyclerview.widget.RecyclerView
         <androidx.recyclerview.widget.RecyclerView
-            android:id="@+id/recycler_view"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
-        <!--android:text="@string/element_text"-->
-    </FrameLayout>
+            android:id="@+id/recyclerView"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager">
+
+        </androidx.recyclerview.widget.RecyclerView>
 
 
 </androidx.drawerlayout.widget.DrawerLayout>
 </androidx.drawerlayout.widget.DrawerLayout>

+ 13 - 7
app/src/main/res/layout/fragment_home.xml

@@ -9,26 +9,33 @@
 
 
     <!-- Add in TextView to display flower name   -->
     <!-- Add in TextView to display flower name   -->
     <TextView
     <TextView
-        android:id="@+id/flower_text"
+        android:id="@+id/textView"
         android:layout_column="0"
         android:layout_column="0"
         android:layout_row="0"
         android:layout_row="0"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:layout_width="wrap_content"
-        android:textSize="25dp"
-        android:text="@string/Sender"/>
+        android:textSize="25dp"/>
+
+    <!--Button
+        android:id="@+id/message_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:paddingLeft="16dp"
+        android:paddingRight="16dp"
+        android:textSize="10sp"
+        /-->
 
 
     <TextView
     <TextView
         android:layout_width="wrap_content"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
         android:layout_row="1"
         android:layout_row="1"
         android:layout_column="0"
         android:layout_column="0"
-        android:text="@string/Betreff"
         android:textSize="20sp"
         android:textSize="20sp"
         android:textAppearance="@style/TextAppearance.AppCompat.Body1"
         android:textAppearance="@style/TextAppearance.AppCompat.Body1"
         android:textColor="@color/colorBetreff" />
         android:textColor="@color/colorBetreff" />
 
 
-    <TextView
-        android:text="@string/Beginn"
+    <Button
+        android:id="@+id/message_button"
         android:layout_width="wrap_content"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
         android:layout_row="2"
         android:layout_row="2"
@@ -37,7 +44,6 @@
         android:textSize="20sp" />
         android:textSize="20sp" />
 
 
     <TextView
     <TextView
-        android:text="@string/date"
         android:textSize="20dp"
         android:textSize="20dp"
         android:layout_width="wrap_content"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"

+ 24 - 0
app/src/main/res/layout/recycler_view_frag.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:id="@+id/sample_main_layout">
+
+    <FrameLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+        <!--android:layout_marginLeft="@dimen/margin_medium"
+        android:layout_marginRight="@dimen/margin_medium"
+        android:gravity="center_vertical"-->
+
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/idk"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
+        <!--android:text="@string/element_text"-->
+    </FrameLayout><!--RelativeLayout from https://www.c-sharpcorner.com/article/recyclerview-in-andriod-with-java/
+                        and FrameLayout was befor from https://developer.android.com/guide/topics/ui/layout/recyclerview#java-->
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 0 - 63
app/src/main/res/layout/text_row_item.xml

@@ -1,63 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="@dimen/list_item_height"
-    android:layout_marginLeft="@dimen/margin_medium"
-    android:layout_marginRight="@dimen/margin_medium"
-    android:gravity="center_vertical">
-
-    <TextView
-        android:id="@+id/textView"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/Sender"/>
-        <!--android:text="@string/element_text"-->
-</FrameLayout>
-<!--GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
-android:layout_width="match_parent"
-android:layout_height="wrap_content"
-android:paddingLeft="16dp"
-android:paddingTop="16dp"
-android:paddingRight="16dp"
-android:layout_gravity="top"-->
-
-<!-- Add in TextView to display flower name   -->
-<!--TextView
-    android:id="@+id/flower_text"
-    android:layout_column="0"
-    android:layout_row="0"
-    android:layout_height="wrap_content"
-    android:layout_width="wrap_content"
-    android:textSize="25dp"
-    android:text="@string/Sender"/>
-
-<TextView
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:layout_row="1"
-    android:layout_column="0"
-    android:text="@string/Betreff"
-    android:textSize="20sp"
-    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
-    android:textColor="@color/colorBetreff" />
-
-<TextView
-    android:text="@string/Beginn"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:layout_row="2"
-    android:layout_column="0"
-    android:gravity="left"
-    android:textSize="20sp" />
-
-<TextView
-    android:text="@string/date"
-    android:textSize="20dp"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:layout_row="0"
-    android:layout_column="3"
-    android:gravity="left" />
-
-
-</GridLayout-->