반응형
공감 및 댓글은 포스팅 하는데 아주아주 큰 힘이 됩니다!! 포스팅 내용이 찾아주신 분들께 도움이 되길 바라며 더 깔끔하고 좋은 포스팅을 만들어 나가겠습니다^^
|
이번 포스팅에서는 swipeLayout 에 대해 공부합니다.
유튜브나 페이스북 등에서 가장 상단에 있을 때 아래로 스와이프를 하면
새로고침이 되죠?
바로 swipeLayout을 통해서 구현할 수 있는데요.
간단한 예제를 통해 알아보겠습니다.
먼저 앱 수준 build.gradle 의 dependencies에를 추가해주어야 합니다.
저는 support-v4, recyclerview 를 추가했씁니다.
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support:support-v4:28.0.0-rc02'
implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
버전은 알아서 맞추시면 됩니다 ㅎ
그리고 레이아웃을 만들면 되는데요.
저는 간단한 예제이기 때문에
ConstraintLayout
SwipeLayout
RecyclerView
형태로 만들었습니다.
DataBinding을 사용하였습니다.
MainActivity.java
import android.databinding.DataBindingUtil;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import com.example.rowan_dev.swipelayoutex.databinding.ActivityMainBinding;
import java.util.Vector;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private ActivityMainBinding binding;
private Vector<Integer> items;
private MainAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
swipe(binding.swipeLayout);
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
binding.recyclerView.setLayoutManager(manager);
refreshData();
}
private void refreshData() {
items = new Vector<>();
for(int i = 0; i < 6; i ++) {
int a = (int)(Math.random() * 45) + 1;
items.add(a);
}
adapter = new MainAdapter(items, this);
binding.recyclerView.setAdapter(adapter);
binding.swipeLayout.setRefreshing(false);
}
@Override
public void onRefresh() {
refreshData();
}
private void swipe(SwipeRefreshLayout layout) {
layout.setRefreshing(false);
layout.setOnRefreshListener(this);
layout.setColorSchemeColors(R.array.google_colors);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
</layout>
item_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:gravity="center"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
</layout>
MainAdapter.java
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.example.rowan_dev.swipelayoutex.databinding.ItemMainBinding;
import java.util.Vector;
public class MainAdapter extends RecyclerView.Adapter {
private Vector<Integer> items;
private Context context;
public MainAdapter(Vector<Integer> items, Context context) {
this.items = items;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ItemMainBinding binding = ItemMainBinding.inflate(LayoutInflater.from(context), viewGroup, false);
return new TextHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
TextHolder holder = (TextHolder)viewHolder;
ItemMainBinding binding = holder.binding;
binding.textView.setText("" + items.get(position));
}
@Override
public int getItemCount() {
return items.size();
}
class TextHolder extends RecyclerView.ViewHolder {
ItemMainBinding binding;
public TextHolder(ItemMainBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
예제 실행해보시면 크게 어렵지 않으실 거에요.
깃허브에 소스 공유해두었습니다.
https://github.com/Parksunggyun/swipeLayoutEx
이상입니다.
감사합니다.
반응형