Add Recyclerview #1

Merged
structix merged 2 commits from recyclerview into master 2020-04-27 22:12:11 +00:00
7 changed files with 115 additions and 3 deletions
Showing only changes of commit cdf9b5b155 - Show all commits

View File

@@ -27,4 +27,5 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.squareup.okhttp3:okhttp:4.2.2' implementation 'com.squareup.okhttp3:okhttp:4.2.2'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
} }

View File

@@ -1,6 +1,10 @@
package com.janasroboter; package com.janasroboter;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle; import android.os.Bundle;
import android.os.StrictMode; import android.os.StrictMode;
import android.view.View; import android.view.View;
@@ -10,10 +14,16 @@ import android.widget.Toast;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private final String URL = "http://192.168.4.1"; private final String URL = "http://192.168.4.1";
private RecyclerView recyclerView;
private RecyclerView.Adapter modeAdapter;
private LinearLayoutManager layoutManager;
private List<String> data;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -29,6 +39,25 @@ public class MainActivity extends AppCompatActivity {
.detectNetwork() .detectNetwork()
.penaltyLog() .penaltyLog()
.build()); .build());
recyclerView = findViewById(R.id.recyclerv);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getBaseContext());
recyclerView.setLayoutManager(layoutManager);
data = new ArrayList<>();
setRecyclerData(data);
modeAdapter = new ModeListAdapter(data);
recyclerView.setAdapter(modeAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getBaseContext(), DividerItemDecoration.VERTICAL));
}
private void setRecyclerData(List<String> data) {
data.add("Mode 1");
data.add("Mode 2");
} }

View File

@@ -0,0 +1,53 @@
package com.janasroboter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ModeListAdapter extends RecyclerView.Adapter<ModeListAdapter.LineHolder> {
private List<String> data;
static class LineHolder extends RecyclerView.ViewHolder {
public TextView mode;
public LineHolder(View itemView) {
super(itemView);
mode = itemView.findViewById(R.id.tv_mode);
}
}
public ModeListAdapter(List<String> data) {
this.data = data;
}
public ModeListAdapter.LineHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
return new LineHolder(view);
}
@Override
public void onBindViewHolder(@NonNull LineHolder holder, final int position) {
// Set the username
String text = data.get(position);
System.out.println("Username: " + text);
holder.mode.setText(text);
}
@Override
public int getItemCount() {
return data.size();
}
}

View File

@@ -85,4 +85,15 @@
android:text="Singen" android:text="Singen"
app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/state" /> app:layout_constraintTop_toBottomOf="@+id/state" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerv"
android:layout_width="409dp"
android:layout_height="540dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSingen"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -0,0 +1,18 @@
<?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:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -7,7 +7,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.5.1' classpath 'com.android.tools.build:gradle:3.6.3'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files

View File

@@ -1,6 +1,6 @@
#Wed Apr 15 23:02:08 CEST 2020 #Sun Apr 26 20:33:59 CEST 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip