added adapter and holder
This commit is contained in:
@@ -26,4 +26,5 @@ dependencies {
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.1.0'
|
||||
}
|
||||
|
||||
@@ -1,14 +1,29 @@
|
||||
package com.example.recyclerview;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
ArrayList<String> mData;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
mData = new ArrayList<>();
|
||||
mData.add("eins");
|
||||
mData.add("zwei");
|
||||
mData.add("drei");
|
||||
|
||||
MyAdapter myAdapter = new MyAdapter(mData);
|
||||
RecyclerView mRecyclerView = findViewById(R.id.recyclerView);
|
||||
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
|
||||
mRecyclerView.setLayoutManager(mLinearLayoutManager);
|
||||
mRecyclerView.setAdapter(myAdapter);
|
||||
}
|
||||
}
|
||||
|
||||
38
app/src/main/java/com/example/recyclerview/MyAdapter.java
Normal file
38
app/src/main/java/com/example/recyclerview/MyAdapter.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.example.recyclerview;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
|
||||
|
||||
ArrayList<String> mData;
|
||||
|
||||
public MyAdapter(ArrayList<String> aData) {
|
||||
mData = aData;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||||
View newView = inflater.inflate(R.layout.one_row_layout, parent, false);
|
||||
MyViewHolder myViewHolder = new MyViewHolder(newView);
|
||||
return myViewHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
|
||||
holder.mTextView.setText(mData.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mData.size();
|
||||
}
|
||||
}
|
||||
15
app/src/main/java/com/example/recyclerview/MyViewHolder.java
Normal file
15
app/src/main/java/com/example/recyclerview/MyViewHolder.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.example.recyclerview;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
public class MyViewHolder extends RecyclerView.ViewHolder {
|
||||
TextView mTextView;
|
||||
public MyViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
mTextView = (TextView) itemView;
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,10 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="409dp"
|
||||
android:layout_height="729dp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user