-
-
Save pstricks-fans/520ef257009bbf629edd5034a7b010f9 to your computer and use it in GitHub Desktop.
Do we need a FrameLayout as the root layout of data item?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/rv" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/tv" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="TextView" | |
android:textColor="#FF00FF00" | |
android:textSize="30sp" | |
android:textStyle="bold" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testrv; | |
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; | |
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.DataHolder> { | |
private String data[]; | |
public DataAdapter(String[] data) { | |
this.data = data; | |
} | |
@NonNull | |
@Override | |
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_item_layout, parent, false); | |
return new DataHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull DataHolder holder, int position) { | |
holder.tv.setText(data[position]); | |
} | |
@Override | |
public int getItemCount() { | |
return data.length; | |
} | |
static class DataHolder extends RecyclerView.ViewHolder { | |
private final TextView tv; | |
public TextView getTv() { | |
return tv; | |
} | |
public DataHolder(@NonNull View itemView) { | |
super(itemView); | |
tv = itemView.findViewById(R.id.tv); | |
// set click listener here | |
itemView.setOnClickListener(v -> { | |
tv.setText("item at " + getAdapterPosition() + " is clicked!"); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testrv; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.RecyclerView; | |
import android.os.Bundle; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
String data[] = {"A", "B"}; | |
DataAdapter da = new DataAdapter(data); | |
RecyclerView rv = findViewById(R.id.rv); | |
rv.setLayoutManager(new LinearLayoutManager(this)); | |
rv.setAdapter(da); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment