package com.trickuweb.expandcollapse;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.net.Uri;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<FormulaModel> formulaList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerViewFormula);
initData();
initRecyclerView();
}
private void initRecyclerView() {
FormulaAdapter formulaAdapter = new FormulaAdapter(this, formulaList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(formulaAdapter);
}
private void initData() {
formulaList = new ArrayList<>();
Uri path1 = Uri.parse("android.resource://com.trickuweb.expandcollapse/" + R.drawable.basictrigformulas);
Uri path2 = Uri.parse("android.resource://com.trickuweb.expandcollapse/" + R.drawable.evenoddformula);
Uri path3 = Uri.parse("android.resource://com.trickuweb.expandcollapse/" + R.drawable.cosinerule);
Uri path4 = Uri.parse("android.resource://com.trickuweb.expandcollapse/" + R.drawable.firstquadrant);
formulaList.add(new FormulaModel("Basic Trigonometry Formulas",
path1.toString()));
formulaList.add(new FormulaModel("Even Odd Formula",
path2.toString()));
formulaList.add(new FormulaModel("Cosine Rule",
path3.toString()));
formulaList.add(new FormulaModel("First Quadrant",
path4.toString()));
}
}
<?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/recyclerViewFormula"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/parent_layout"
android:padding="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewMonth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="8dp"
android:text="Name"
android:textSize="16dp"
android:paddingRight="6dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintBottom_toTopOf="@+id/expandableLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/expandableLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textViewMonth">
<ImageView
android:id="@+id/formulaImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/cosinerule"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
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'
implementation 'com.squareup.picasso:picasso:2.71828'
}
package com.trickuweb.expandcollapse;
public class FormulaModel {
private String name;
private String pic;
private boolean expanded;
public FormulaModel(String name, String pic) {
this.name = name;
this.pic = pic;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
@Override
public String toString() {
return "FormulaModel{" +
"name='" + name + '\'' +
", pic='" + pic + '\'' +
", expanded=" + expanded +
'}';
}
}
package com.trickuweb.expandcollapse;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
public class FormulaAdapter extends RecyclerView.Adapter<FormulaAdapter.ViewHolder>{
List<FormulaModel> formulaList;
Context mContext;
public FormulaAdapter(Context mContext, List<FormulaModel> formulaList) {
this.formulaList = formulaList;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_formula, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
FormulaModel formula = formulaList.get(position);
holder.textViewMonth.setText(formula.getName());
Picasso.get().load(formula.getPic()).placeholder(R.drawable.cosinerule).error(R.drawable.cosinerule).into(holder.imageViewName);
// holder.linearLayout.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(mContext,holder.textViewMonth.getText(),Toast.LENGTH_SHORT).show();
// }
// });
// boolean isExpanded = formulaList.get(position).isExpanded();
// holder.expandableLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
}
@Override
public int getItemCount() {
return formulaList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ConstraintLayout expandableLayout;
TextView textViewMonth;
ImageView imageViewName;
LinearLayout linearLayout;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
textViewMonth = itemView.findViewById(R.id.textViewMonth);
imageViewName = itemView.findViewById(R.id.formulaImage);
expandableLayout = itemView.findViewById(R.id.expandableLayout);
linearLayout =itemView.findViewById(R.id.parent_layout);
// textViewMonth.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// FormulaModel formula = formulaList.get(getAdapterPosition());
// formula.setExpanded(!formula.isExpanded());
// notifyItemChanged(getAdapterPosition());
// }
// });
}
}
}