Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Item.java
- -----------------
- public class Item {
- private String text, subText, subText01, subText02;
- private boolean isExpandable;
- public Item(String text, String subText, String subText01, String subText02, boolean isExpandable) {
- this.text = text;
- this.subText = "Amount:" + subText;
- this.isExpandable = isExpandable;
- this.subText01 = "Address: " + subText01;
- this.subText02 = "Type: " + subText02;
- }
- public String getSubText02() {
- return subText02;
- }
- public String getSubText01() {
- return subText01;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public String getSubText() {
- return subText;
- }
- public boolean isExpandable() {
- return isExpandable;
- }
- }
- ========================================================================================================
- Create InterFace:
- public interface ItemClickListener {
- void onClick(View view, int position, boolean isLongClick);
- }
- ========================================================================================================
- MainActivity.java
- ===========================
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- import android.widget.Button;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
- RecyclerView list;
- RecyclerView.LayoutManager layoutManager;
- List<Item> items = new ArrayList<>();
- Button button_make,button_make01;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button_make = findViewById(R.id.button_make);
- button_make01 = findViewById(R.id.button_make01);
- list = (RecyclerView) findViewById(R.id.recycler);
- list.setHasFixedSize(true);
- layoutManager = new LinearLayoutManager(this);
- list.setLayoutManager(layoutManager);
- setPointer();
- }
- private void setPointer() {
- button_make.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Item item = new Item("Requests for food", "5","Nazareth","Extra care" ,true);
- items.add(item);
- MyAdapter adapter = new MyAdapter(items);
- list.setAdapter(adapter);
- }
- });
- button_make01.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Item item = new Item ("Food for pick up", "10","Haifa","Extra care",true);
- items.add(item);
- MyAdapter adapter = new MyAdapter(items);
- list.setAdapter(adapter);
- }
- });
- }
- }
- =================================================================================================================
- activity_main.xml
- --------------------
- <?xml version="1.0" encoding="utf-8"?>
- <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="com.arabiaexample.amjad.myapplication.MainActivity">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/recycler"
- android:layout_width="350dp"
- android:layout_height="400dp"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="49dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintHorizontal_bias="1.0"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.904">
- </android.support.v7.widget.RecyclerView>
- <Button
- android:id="@+id/button_make"
- android:layout_width="wrap_content"
- android:layout_height="50dp"
- android:layout_alignEnd="@+id/recycler"
- android:layout_alignParentBottom="true"
- android:layout_marginBottom="27dp"
- android:text="New Push for family" />
- <Button
- android:id="@+id/button_make01"
- android:layout_width="wrap_content"
- android:layout_height="50dp"
- android:layout_alignStart="@+id/recycler"
- android:layout_below="@+id/recycler"
- android:text="new push from donate" />
- </RelativeLayout>
- ====================================================================================================
- activity_with_child.xml
- -----------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="1dp"
- android:background="#7986CB"
- android:padding="5dp">
- <RelativeLayout
- android:id="@+id/relativelayout01"
- android:layout_width="40dp"
- android:layout_height="43dp"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true">
- <View
- android:id="@+id/button1"
- android:layout_width="40dp"
- android:layout_height="40dp"
- android:layout_alignParentEnd="true"
- android:layout_centerVertical="true"
- android:background="@drawable/up_arrow" />
- </RelativeLayout>
- <TextView
- android:id="@+id/textView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_toLeftOf="@+id/relativelayout01"
- android:padding="8dp"
- android:text="Requests for food"
- android:textColor="#ffffff"
- android:textSize="17sp" />
- <com.github.aakira.expandablelayout.ExpandableLinearLayout
- android:id="@+id/expandableLayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/textView"
- android:orientation="vertical"
- app:ael_duration="400"
- app:ael_expanded="false"
- app:ael_interpolator="bounce">
- <TextView
- android:id="@+id/textViewChild"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_toLeftOf="@+id/relativelayout01"
- android:padding="8dp"
- android:text="this is child item 00"
- android:textColor="#ead6d6"
- android:textSize="14sp" />
- <TextView
- android:id="@+id/textViewChild01"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_toLeftOf="@+id/relativelayout01"
- android:padding="8dp"
- android:text="this is child item 01"
- android:textColor="#ead6d6"
- android:textSize="14sp" />
- <TextView
- android:id="@+id/textViewChild02"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_toLeftOf="@+id/relativelayout01"
- android:padding="8dp"
- android:text="this is child item 02"
- android:textColor="#ead6d6"
- android:textSize="14sp" />
- </com.github.aakira.expandablelayout.ExpandableLinearLayout>
- <Button
- android:id="@+id/button01"
- android:layout_width="100dp"
- android:layout_height="40dp"
- android:layout_below="@+id/textView"
- android:layout_marginEnd="25dp"
- android:layout_toStartOf="@+id/relativelayout01"
- android:text="button 01"
- android:textAllCaps="false"
- android:visibility="gone" />
- <Button
- android:id="@+id/button02"
- android:layout_width="100dp"
- android:layout_height="40dp"
- android:layout_alignStart="@+id/button01"
- android:layout_below="@+id/button01"
- android:text="button 02"
- android:textAllCaps="false"
- android:visibility="gone" />
- <Button
- android:id="@+id/button03"
- android:layout_width="100dp"
- android:layout_height="40dp"
- android:layout_alignStart="@+id/button02"
- android:layout_below="@+id/button02"
- android:text="button 03"
- android:textAllCaps="false"
- android:visibility="gone" />
- </RelativeLayout>
- ============================================================================================
- MyAdapter.java
- ------------------
- import android.animation.ObjectAnimator;
- import android.content.Context;
- import android.graphics.Color;
- import android.support.v7.widget.RecyclerView;
- import android.util.SparseBooleanArray;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
- import com.github.aakira.expandablelayout.ExpandableLinearLayout;
- import com.github.aakira.expandablelayout.Utils;
- import java.util.List;
- class MyViewHolderWithChild extends RecyclerView.ViewHolder implements View.OnClickListener{
- public TextView textView, textViewChild,textViewChild01,textViewChild02;
- public RelativeLayout button;
- public Button send_messenger_button;
- public ExpandableLinearLayout expandableLayout;
- public Button button01, button02, button03;
- ItemClickListener itemClickListener;
- public MyViewHolderWithChild(View itemView) {
- super(itemView);
- textView = (TextView) itemView.findViewById(R.id.textView);
- textViewChild = (TextView) itemView.findViewById(R.id.textViewChild);
- textViewChild01= itemView.findViewById(R.id.textViewChild01);
- textViewChild02= itemView.findViewById(R.id.textViewChild02);
- button01 = itemView.findViewById(R.id.button01);
- button02 = itemView.findViewById(R.id.button02);
- button03 = itemView.findViewById(R.id.button03);
- button = (RelativeLayout) itemView.findViewById(R.id.relativelayout01);
- expandableLayout = (ExpandableLinearLayout) itemView.findViewById(R.id.expandableLayout);
- itemView.setOnClickListener(this);
- }
- public void setItemClickListener(ItemClickListener itemClickListener) {
- this.itemClickListener = itemClickListener;
- }
- @Override
- public void onClick(View view) {
- itemClickListener.onClick(view,getAdapterPosition(),false);
- }
- }
- public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
- List<Item> items;
- Context context;
- SparseBooleanArray expandState = new SparseBooleanArray();
- public MyAdapter(List<Item> items) {
- this.items = items;
- for (int i = 0; i < items.size(); i += 1) {
- expandState.append(i, false);
- }
- }
- @Override
- public int getItemViewType(int position) {
- if (items.get(position).isExpandable()) {
- return 1;
- } else {
- return 0;
- }
- }
- @Override
- public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- this.context = parent.getContext();
- LayoutInflater inflater = LayoutInflater.from(context);
- View view = inflater.inflate(R.layout.layout_with_child, parent, false);
- return new MyViewHolderWithChild(view);
- }
- @Override
- public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
- switch (holder.getItemViewType()) {
- case 1: {
- final MyViewHolderWithChild viewHolder = (MyViewHolderWithChild) holder;
- final Item item = items.get(position);
- viewHolder.setIsRecyclable(false);
- viewHolder.textView.setText(item.getText());
- viewHolder.expandableLayout.setInRecyclerView(true);
- viewHolder.expandableLayout.setExpanded(expandState.get(position));
- viewHolder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
- @Override
- public void onAnimationStart() {
- }
- @Override
- public void onAnimationEnd() {
- }
- @Override
- public void onPreOpen() {
- changeRotate((RelativeLayout) viewHolder.button, 0f, 180f).start();
- expandState.put(position, true);
- viewHolder.button01.setVisibility(View.VISIBLE);
- viewHolder.button02.setVisibility(View.VISIBLE);
- viewHolder.button03.setVisibility(View.VISIBLE);
- viewHolder.textView.setTextColor(Color.RED);
- }
- @Override
- public void onPreClose() {
- changeRotate((RelativeLayout) viewHolder.button, 180f, 0f).start();
- expandState.put(position, false);
- viewHolder.button01.setVisibility(View.GONE);
- viewHolder.button02.setVisibility(View.GONE);
- viewHolder.button03.setVisibility(View.GONE);
- viewHolder.textView.setTextColor(Color.WHITE);
- }
- });
- viewHolder.button.setRotation(expandState.get(position) ? 180f : 0f);
- viewHolder.button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Expandable child item
- viewHolder.expandableLayout.toggle();
- }
- });
- viewHolder.textViewChild.setText(items.get(position).getSubText());
- viewHolder.textViewChild01.setText(items.get(position).getSubText01());
- viewHolder.textViewChild02.setText(items.get(position).getSubText02());
- viewHolder.textViewChild.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(context, "Child Item Click" + items.get(position).getSubText(), Toast.LENGTH_SHORT).show();
- }
- });
- viewHolder.setItemClickListener(new ItemClickListener() {
- @Override
- public void onClick(View view, int position, boolean isLongClick) {
- Toast.makeText(context, "With child click :" + items.get(position).getText(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- break;
- default:
- break;
- }
- }
- private ObjectAnimator changeRotate(RelativeLayout button, float from, float to) {
- ObjectAnimator animator = ObjectAnimator.ofFloat(button, "rotation", from, to);
- animator.setDuration(300);
- animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
- return animator;
- }
- @Override
- public int getItemCount() {
- return items.size();
- }
- }
- ========================================================================================================
- Create Drawable:
- up_arrow.xml
- ------------------
- <vector xmlns:android="http://schemas.android.com/apk/res/android"
- android:width="24dp"
- android:height="24dp"
- android:viewportWidth="24.0"
- android:viewportHeight="24.0">
- <path
- android:fillColor="#FF000000"
- android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
- </vector>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement