Exchange

Offline RecyclerView in Android Studio

 in activity_main.xml

<?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"
android:background="#939292">


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

in single_view.xml

<?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="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

in MyAdapter.java


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {

int []arr;
String []arry;
Context mContext;


public MyAdapter(int[] arr, String[]arry, Context context) {
this.arr = arr;
this.arry = arry;
mContext = context;

}


@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent, false);
return new myViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.imageView.setImageResource(arr[position]);
holder.textView.setText("Image Number" + position);

}

@Override
public int getItemCount() {
return arr.length;
}

public class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

ImageView imageView;
TextView textView;

public myViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView =itemView.findViewById(R.id.textView);

imageView.setOnClickListener(this::onClick);
}

@Override
public void onClick(View view) {
Toast.makeText(mContext, arry[getAdapterPosition()], Toast.LENGTH_SHORT).show();
}
}
}

in MainActivity.java


public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;

MyAdapter myAdapter;

String []arry = {"அ ஆ இ","One two three", "ABC", "Animals", "Birds", "Flowers", "Family",};
int []arr = {R.drawable.aa_aa, R.drawable.one_two, R.drawable.abc, R.drawable.animals, R.drawable.birds,
R.drawable.flower, R.drawable.family};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.rv);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);

myAdapter = new MyAdapter(arr, arry,this);
recyclerView.setAdapter(myAdapter);

recyclerView.setHasFixedSize(true);

}

}


recyclerview in android studio
android studio




Post a Comment

0 Comments