I have created a app where on the first screen there will be a image, if the user click on that image a suggestion screen will open to change that image. I want that the suggestion image screen should be a Recyclerview screen, which will open on the same screen. This is just a very basic work to pratice recycler view. I have not implement the change till now I have stuck here. Actually I can't figure out that how should I connect those class on the main_activity to preform the desire task. I am sharing all the code I have done for this , please help me for the next step.
main_activity.xml
<RelativeLayout 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">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_img1"
android:layout_margin="11dp"
android:background="@color/black"
android:layout_centerInParent="true"
android:id="@ id/imgicon1">
</ImageView>
</RelativeLayout>
1.structure xml file which will use for the recycler view to set other image(lay_for_select_img.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="450dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="7dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:id="@ id/layLinearforimg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
>
<ImageView
android:id="@ id/imgicon2"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_img1"
android:background="@color/black">
</ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
structure class to set the image(structure.java)
public class structureclass {
int img;
public structureclass(int img){
this.img=img;
}
}
main_activity.java
public class MainActivity extends AppCompatActivity {
RecyclerView layrecycle = findViewById(id.layRecycle);
ImageView imgicon1;
ArrayList<structureclass> strucimg=new ArrayList<>();
RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
layrecycle = findViewById(id.layRecycle);
imgicon1=findViewById(id.imgicon1);
layrecycle.setLayoutManager(new GridLayoutManager(this,3));
strucimg.add(new structureclass(drawable.ic_img1));
strucimg.add(new structureclass(drawable.ic_img2));
strucimg.add(new structureclass(drawable.ic_img3));
strucimg.add(new structureclass(drawable.ic_img4));
strucimg.add(new structureclass(drawable.ic_img5));
strucimg.add(new structureclass(drawable.ic_img6));
strucimg.add(new structureclass(drawable.ic_img7));
strucimg.add(new structureclass(drawable.ic_img8));
strucimg.add(new structureclass(drawable.ic_img9));
strucimg.add(new structureclass(drawable.ic_img10));
strucimg.add(new structureclass(drawable.ic_img11));
strucimg.add(new structureclass(drawable.ic_img12));
strucimg.add(new structureclass(drawable.ic_img13));
strucimg.add(new structureclass(drawable.ic_img14));
strucimg.add(new structureclass(drawable.ic_img15));
adapter=new RecyclerViewAdapter(this,strucimg);
imgicon1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layrecycle.setVisibility(View.VISIBLE);
}
});
activityrecyclerclass.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/layRecycle">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.viewHolder> {
Context context;
ArrayList<structureclass> strucimg=new ArrayList<>();
public RecyclerViewAdapter(Context context, ArrayList<structureclass> strucimg){
this.context=context;
this.strucimg=strucimg;
}
public class viewHolder extends RecyclerView.ViewHolder{
ImageView imgicon2;
public viewHolder(@NonNull View itemView) {
super(itemView);
imgicon2=itemView.findViewById(R.id.imgicon2);
}
}
@NonNull
@Override
public RecyclerViewAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater.from(context).inflate(R.layout.lay_for_select_img,parent,false);
//inflate return view
viewHolder viewholder=new viewHolder(v);
return viewholder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.viewHolder holder, int position) {
holder.imgicon2.setImageResource(strucimg.get(position).img);
}
@Override
public int getItemCount() {
return strucimg.size();
}
}
CodePudding user response:
Next step is to create the adapter class which will handle the data binding between the structureclass and the lay_for_select_img layout. Here's how you can create the adapter class:
Adapter class (RecyclerAdapter.java)
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<structureclass> strucimg;
public RecyclerAdapter(ArrayList<structureclass> strucimg) {
this.strucimg = strucimg;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lay_for_select_img, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.imgicon2.setImageResource(strucimg.get(position).img);
}
@Override
public int getItemCount() {
return strucimg.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imgicon2;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imgicon2 = itemView.findViewById(R.id.imgicon2);
}
}
}
Finally, set the adapter to your RecyclerView in the MainActivity class:
layrecycle.setAdapter(new RecyclerAdapter(strucimg));
You also need to handle the click event on the ImageView in the MainActivity class. To open the RecyclerView when the ImageView is clicked:
imgicon1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layrecycle.setVisibility(View.VISIBLE);
}
});
And to hide the RecyclerView when a suggestion image is selected:
//TODO: implement the code to hide the RecyclerView and update the selected image on the main screen
CodePudding user response:
Add this line layrecycle.setAdapter(adapter);
below initialization of adapter as follow:
adapter=new RecyclerViewAdapter(this,strucimg);
layrecycle.setAdapter(adapter);
Moreover, you change height to wrap_content
of your item_layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="450dp"
...
/>
to following
android:layout_height="wrap_content"