[안드로이드] 당겨서 새로고침 간단히 구현하기!
프로그래밍/안드로이드2017. 5. 14. 14:54
리스트뷰나 리사이클뷰등을 사용시 당겨서 새로고침 해야 될 경우가 있는데요
손쉽게 구현할수 있는 방법이 있습니다
일단 build.gradle에 support v4 라이브러리를
1 2 3 4 | dependencies { ... compile 'com.android.support:support-v4:23.4.0' } | cs |
추가 해줘야 됩니다
하지만 compileSdkVersion 버전이 22인 기존 앱에 위와 같이 추가시
This support library should not use a different version (23) than the compileSdkVersion (22) less... (Ctrl+F1)
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion.)
에러가 발생하더군요 최소 지원이 마시멜로인거 같은데 롤리팝까지 지원해야 되서
위의 라이브러리를 적용할수 없었는데 라이브러리 추가 없이 사용해도 되네요^^;
1 2 3 4 5 6 7 8 | <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="match_parent"> ...새로고침을 적용할 리스트 뷰 또는 리싸이클뷰 </android.support.v4.widget.SwipeRefreshLayout> | cs |
라이브러리 추가 후 레이아웃에 위와 같이 새로고침을 적용할 곳을 android.support.v4.widget.SwipeRefreshLayout로 감싸줍니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | SwipeRefreshLayout mSwipeRefreshLayout; // 클래스에 변수 선언 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //새로고침시 실행 할 작업 실행... new AppTask().execute(); mSwipeRefreshLayout.setRefreshing(false); } }); } | cs |
사용할 액티비티에 SwipeRefreshLayout를 선언후 onCreate에 위와 적영후 onRefresh() 안에서 실행할 작업을 수행하면 됩니다
저의 경우 리사이클 뷰를 다시 불러오고 있습니다
작업 실행 후 mSwipeRefreshLayout.setRefreshing(false);를 반드시 호출해야 되는데요
그렇지 않을 경우 새로고침 아이콘이 사라지지 않습니다
제대로 적용시 아래처럼 당겼을때 새로고침 아이콘을 보실수 있습니다
'프로그래밍 > 안드로이드' 카테고리의 다른 글
[안드로이드] EditText 엔터키로 키보드 내리기(숨기기) (1) | 2017.06.11 |
---|---|
[안드로이드(자바)] 문자열에 특정 문자 갯수 구하기 (0) | 2017.06.08 |
[안드로이드] 애니메이션 효과 주기 (0) | 2017.02.15 |
[안드로이드] Error:Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment] 에러 관련 (0) | 2017.01.29 |
안드로이드 쉽게 앱을 지우자! 쉬운 앱 삭제(uninstaller) 툴 추천!! (0) | 2017.01.29 |