cachapa / ExpandableLayout

An expandable layout container for Android
Apache License 2.0
2.34k stars 273 forks source link

Issue using expandable layout in recycler view #53

Closed cheesemcbaguette closed 6 years ago

cheesemcbaguette commented 6 years ago

The first time I expand an item, I don't get onClicked event, only after I expand/collapse a second time and I don't know why.

Here's my code :

`public class RestaurantAdapter extends RecyclerView.Adapter{ private static final int UNSELECTED = -1; private List restaurantList; private static int selectedItem = UNSELECTED; private RecyclerView recyclerView; private Context context;

//constructor, call on creation
public RestaurantAdapter(ArrayList<Restaurant> restaurantList, RecyclerView recyclerView, Context context) {
    this.restaurantList = restaurantList;
    this.recyclerView = recyclerView;
    this.context = context;
}

// Create new views (invoked by the layout manager)
@Override
public RestaurantAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
    // create a new view
    View v =  LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_layout, parent, false);
    // set the view's size, margins, paddings and layout parameters
    Log.w("RestaurantAdapter", "View created");

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(RestaurantAdapter.ViewHolder holder, int position) {
    Restaurant restaurant = restaurantList.get(position);
    holder.name.setText(restaurant.getName());
    holder.distance.setText(restaurant.getDistance() + " m");
    Log.w("RestaurantAdapter", "Data binded");

    holder.bind();
}

@Override
public int getItemCount() {
    return restaurantList.size();
}

public List<Restaurant> getRestaurantList() {
    return restaurantList;
}

public void setRestaurantList(List<Restaurant> restaurantList) {
    this.restaurantList = restaurantList;
}

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ExpandableLayout.OnExpansionUpdateListener{
    // each data item is just a string in this case

    private TextView name, distance;
    private Button details;
    private ExpandableLayout expandableLayout;
    private LinearLayout layoutTextView;
    private ViewHolder holder;

    private ViewHolder(View v) {
        super(v);
        name = (TextView) v.findViewById(R.id.listTitle);
        distance = (TextView) v.findViewById(R.id.listDistance);
        expandableLayout = v.findViewById(R.id.expandable_layout);
        layoutTextView = v.findViewById(R.id.layoutTextView);
        details = (Button) v.findViewById(R.id.details);
        expandableLayout.setInterpolator(new OvershootInterpolator());
        expandableLayout.setOnExpansionUpdateListener(this);
        layoutTextView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Log.w("RestaurantAdapter", "View clicked");
        final ViewHolder holder = (ViewHolder) recyclerView.findViewHolderForAdapterPosition(selectedItem);

        if(holder != null) {
            holder.details.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(context, RestaurantActivity.class);
                    Toast.makeText(context, "Activity Launched", Toast.LENGTH_SHORT).show();
                    holder.layoutTextView.setSelected(false);
                    holder.expandableLayout.collapse();
                }
            });

            holder.layoutTextView.setSelected(false);
            holder.expandableLayout.collapse();
        }

        int position = getAdapterPosition();
        if (position == selectedItem) {
            selectedItem = UNSELECTED;

        } else {
            layoutTextView.setSelected(true);
            expandableLayout.expand();
            selectedItem = position;
        }
    }

    @Override
    public void onExpansionUpdate(float expansionFraction, int state) {
        Log.d("ExpandableLayout", "State: " + state);
        if (state == ExpandableLayout.State.EXPANDING) {
            recyclerView.smoothScrollToPosition(getAdapterPosition());
        }
    }

    private void bind() {
        int position = getAdapterPosition();
        boolean isSelected = position == selectedItem;

        layoutTextView.setSelected(isSelected);
        expandableLayout.setExpanded(isSelected, false);
    }

}

} `

cachapa commented 6 years ago

Did you close this issue on purpose?

princealirehman1 commented 4 years ago

I am having the same issue, What is the cause of this ?

Is the issue closed ? Where is the solution ref ?

cachapa commented 4 years ago

The issue was closed by the reporter. I assumed he found the problem and it wasn't related to this lib.