ide/androidstudio

Android Swipe

C/H 2017. 1. 19. 08:30

hudomju/android-swipe-to-dismiss-undo

존재하지 않는 메소드, Override 되지 않는 메소드... 앱 실행까지는 되었지만... 죽어버리는 앱

Swipe to Dismiss for RecyclerView

결국은 Swipe to Dismiss for RecyclerView를 이용해서 Swipe기능을 완성했다. 물론 완벽하게 맘에 들지는 않지만...

public class MainActivity extends AppCompatActivity {
    ....

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();
    }

    private void init() {
        SwipeableRecyclerViewTouchListener swipeTouchListener =
            new SwipeableRecyclerViewTouchListener(recyclerView,
                    new SwipeableRecyclerViewTouchListener.SwipeListener() {
                        @Override
                        public boolean canSwipeLeft(int position) {
                            return true;
                        }

                        @Override
                        public boolean canSwipeRight(int position) {
                            return true;
                        }

                        @Override
                        public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                L.d(TAG, "remove Position:" + position );
                                delGame(games.get(position).getDrwNo(), games.get(position).getTR());
                                games.remove(position);
                                gameAdapter.notifyItemRemoved(position);
                            }
                            gameAdapter.notifyDataSetChanged();
                            Toast.makeText(mContext, "삭제 되었습니다.", Toast.LENGTH_SHORT).show();

                            if( games.size() < 1 ){ // 다시 한번 읽기
                                viewGame();
                            }
                        }

                        @Override
                        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                L.d(TAG, "remove Position:" + position );
                                if( keep == 1 ){
                                    unKeepGame(games.get(position).getDrwNo(), games.get(position).getTR());
                                }else{
                                    keepGame(games.get(position).getDrwNo(), games.get(position).getTR());
                                }
                                games.remove(position);
                                gameAdapter.notifyItemRemoved(position);
                            }
                            gameAdapter.notifyDataSetChanged();

                            if( keep == 1 ){
                                Toast.makeText(mContext, "보관취소 되었습니다.", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(mContext, "보관 되었습니다.", Toast.LENGTH_SHORT).show();
                            }

                            if( games.size() < 1 ){ // 다시 한번 읽기 // 다시 한번 읽기
                                viewGame();
                            }
                        }
                    });

        recyclerView.addOnItemTouchListener(swipeTouchListener);
    }

    ...
}
반응형

'ide > androidstudio' 카테고리의 다른 글

초기화 값 저장/호출 SharedPreferences  (0) 2017.01.23
네트워크 상태로 자동 실행  (0) 2017.01.20
Android Drawable Shape Syntax  (0) 2017.01.18
android SQLite DB 열기  (0) 2017.01.17
Circle TextView  (0) 2017.01.16