1. Intro Subject.pptx
1. Intro Subject.pptx
Getter(), Setter()...
}
USING RECYCLER VIEW
Add RecyclerView to layout
<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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerHero"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
USING RECYCLER VIEW
Create Custom row layout
<?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="wrap_content">
<ImageView
android:id="@+id/image_hero"
android:layout_width="0dp"
android:layout_height="@dimen/dp_180"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textColor="#FFF"
app:layout_constraintBottom_toBottomOf="@+id/image_hero"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.112"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/image_hero"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
USING RECYCLER VIEW
Create RecyclerView.Adapter
public class HeroAdapter extends
RecyclerView.Adapter<HeroAdapter.ViewHolder> {
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Hero hero = mHeros.get(position);
Glide.with(mContext)
.load(hero.getImage())
.into(holder.mImageHero);
holder.mTextName.setText(hero.getName());
}
@Override
public int getItemCount() {
return mHeros.size();
}
USING RECYCLER VIEW
Connect Adapter to RecyclerView
public class HeroActivity extends AppCompatActivity {
private ArrayList<Hero> mHeros ;
private RecyclerView mRecyclerHero;
private HeroAdapter mHeroAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hero);
mRecyclerHero = findViewById(R.id.recyclerHero);
mHeros = new ArrayList<>();
createHeroList();
mHeroAdapter = new HeroAdapter(this,mHeros);
mRecyclerHero.setAdapter(mHeroAdapter);
mRecyclerHero.setLayoutManager(new LinearLayoutManager(this));
}
Recreated
From the
backstack
Thank you!
Khoa CNTT ĐH Nông Lâm - Võ Tấn 33