chthai64 / SwipeRevealLayout

Easy, flexible and powerful Swipe Layout for Android
MIT License
1.57k stars 397 forks source link

When recyclerview is scrolled, all layouts reveal their swipe layout #74

Open TheChetanShewale opened 6 years ago

TheChetanShewale commented 6 years ago

//Adapter Code public class NotificationListAdapter extends RecyclerView.Adapter { private ArrayList notifications; // This object helps you save/restore the open/close state of each view private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();

public NotificationListAdapter(ArrayList<Notification> notifications) {
    this.notifications = notifications;
    //viewBinderHelper.setOpenOnlyOne(true);
}

@Override
public NotificationListAdapter.NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new NotificationListAdapter.NotificationViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_notification, parent, false));
}

@Override
public void onBindViewHolder(NotificationViewHolder holder, int position) {
    viewBinderHelper.bind(holder.swipeLayout, notifications.get(position).getNotificationId());
    holder.onBindView(notifications.get(position), viewBinderHelper);
}

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

/**
 * Only if you need to restore open/close state when the orientation is changed.
 * Call this method in {@link android.app.Activity#onSaveInstanceState(Bundle)}
 */
public void saveStates(Bundle outState) {
    viewBinderHelper.saveStates(outState);
}

/**
 * Only if you need to restore open/close state when the orientation is changed.
 * Call this method in {@link android.app.Activity#onRestoreInstanceState(Bundle)}
 */
public void restoreStates(Bundle inState) {
    viewBinderHelper.restoreStates(inState);
}

public class NotificationViewHolder extends ViewHolder {
    private TextView tvTitle, tvTime, tvDelete;
    private Notification notification;
    private SwipeRevealLayout swipeLayout;

    public NotificationViewHolder(View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tvTitle);
        tvTime = itemView.findViewById(R.id.tvTime);
        tvDelete = itemView.findViewById(R.id.tvDelete);
        swipeLayout = itemView.findViewById(R.id.swipeLayout);
        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public void onBindView(Object object, ViewBinderHelper viewBinderHelper) {
        notification = (Notification) object;
        tvTitle.setText(notification.getMessage());
        tvTime.setText(DateFormatter.getTimeDifference(notification.getCreatedDate(), MyApplication.getInstance()));
    }
}

}

//Activity Code public class NotificationsActivity extends BaseActivity { private ProgressBar progressBar; private ArrayList notifications = new ArrayList<>(); private NotificationListAdapter notificationListAdapter; private TextView tvNoNotificationsFound; private RecyclerView rvNews;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);
    setToolbarWithBackButtonAndTitle(getString(R.string.notification));
    initViews();
    getNotifications();
}

private void initViews() {
    progressBar = findViewById(R.id.progressBar);
    tvNoNotificationsFound = findViewById(R.id.tvNoNotificationsFound);
    rvNews = findViewById(R.id.rvNotifications);
    notificationListAdapter = new NotificationListAdapter(notifications);
    rvNews.setLayoutManager(new LinearLayoutManager(this));
    rvNews.setAdapter(notificationListAdapter);
}

private void getNotifications() {
    if (Common.isOnline(this)) {
        progressBar.setVisibility(View.VISIBLE);
        DataManager.getNotifications(new ResultListener<NotificationResponse>() {
            @Override
            public void success(NotificationResponse response) {
                if (response != null
                        && response.getNotifications() != null
                        && !response.getNotifications().isEmpty()) {
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notifications.addAll(response.getNotifications());
                    notificationListAdapter.notifyDataSetChanged();
                    tvNoNotificationsFound.setVisibility(View.GONE);
                    rvNews.setVisibility(View.VISIBLE);
                } else {
                    rvNews.setVisibility(View.GONE);
                    tvNoNotificationsFound.setVisibility(View.VISIBLE);
                }
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void failure(VolleyError error) {
                progressBar.setVisibility(View.GONE);
                Common.showError(NotificationsActivity.this, error);
            }
        });
    } else {
        Common.showShortToast(this, getString(R.string.not_connected_to_internet));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // Only if you need to restore open/close state when
    // the orientation is changed
    if (notificationListAdapter != null) {
        notificationListAdapter.saveStates(outState);
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Only if you need to restore open/close state when
    // the orientation is changed
    if (notificationListAdapter != null) {
        notificationListAdapter.restoreStates(savedInstanceState);
    }
}

}

XarBpeak commented 6 years ago

I did it by closing all other views programmatically when current recyclerView item swiped.