expandable_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:id="@+id/motherLayout" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/itemClicked" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/bookImg" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerVertical="true" android:src="@drawable/ic_book" /> <LinearLayout android:id="@+id/titleLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="5dp" android:layout_marginEnd="5dp" android:layout_toStartOf="@id/arrowImg" android:layout_toEndOf="@+id/bookImg" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="@string/title" android:textColor="@android:color/black" android:textSize="18sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Bangladesh" android:textColor="@android:color/black" android:textSize="15sp" /> </LinearLayout> <ImageView android:id="@+id/arrowImg" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:src="@drawable/ic_down_arrow" /> </RelativeLayout> <LinearLayout android:visibility="gone" android:id="@+id/discLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/dis" android:layout_marginTop="5dp" android:textColor="@android:color/black" android:fontFamily="serif" android:textAlignment="center" android:padding="5dp" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
activity_main.xml
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
ListView listView; // OnCreate Method Start Here ============= listView = findViewById(R.id.listView); MyAdapter myAdapter = new MyAdapter(); listView.setAdapter(myAdapter); // OnCreate Method Close Here ============= private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 10; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View myView = layoutInflater.inflate(R.layout.expandable_item, parent,false); LinearLayout motherLayout = myView.findViewById(R.id.motherLayout); RelativeLayout itemClicked = myView.findViewById(R.id.itemClicked); ImageView arrowImg = myView.findViewById(R.id.arrowImg); LinearLayout discLayout = myView.findViewById(R.id.discLayout); itemClicked.setOnClickListener(v -> { // code here === if (discLayout.getVisibility() == View.GONE){ TransitionManager.beginDelayedTransition(motherLayout, new AutoTransition()); discLayout.setVisibility(View.VISIBLE); motherLayout.setBackgroundColor(Color.parseColor("#724CAF50")); arrowImg.setImageResource(R.drawable.ic_up_arrow); } else { TransitionManager.beginDelayedTransition(motherLayout, new AutoTransition()); discLayout.setVisibility(View.GONE); motherLayout.setBackgroundColor(Color.parseColor("#FFFFFF")); arrowImg.setImageResource(R.drawable.ic_down_arrow); } }); return myView; } }
Tags:
Android Studio