RecyclerView 드래그 정렬 효과 구현
13804 단어 RecyclerView끌다정렬
RecyclerView 는 우리 에 게 제스처 기 를 제공 했다.
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFrlg = 0;
if (recyclerView.getLayoutManager() instanceof GridLayoutManager){
dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
}else if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
}
return makeMovementFlags(dragFrlg,0);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
// , ,
// Collections.swap(datas,viewHolder.getAdapterPosition(),target.getAdapterPosition());
// ap.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
// viewHolder Position
int fromPosition = viewHolder.getAdapterPosition();
// item viewHolder
int toPosition = target.getAdapterPosition();
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(datas, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(datas, i, i - 1);
}
}
ap.notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// ;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
/**
* Item
*
* @param viewHolder
* @param actionState
*/
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setBackgroundColor(Color.RED);
// // 70
Vibrator vib = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(70);
}
super.onSelectedChanged(viewHolder, actionState);
}
/**
*
* @param recyclerView
* @param viewHolder
*/
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(0);
ap.notifyDataSetChanged(); // ,
}
});
RecyclerView 에 설정 하면 됩 니 다:
helper.attachToRecyclerView(rv);
다음은 완전한 코드 와 Xml 레이아웃 파일 입 니 다.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final String TAG = this.getClass().getSimpleName();
private List<String> d = Arrays.asList(
"A","B","C","D","E","F","G"
,"H","I","J","K","L","M","N"
,"O","P","Q","R","S","T"
,"U","V","W","X","Y","Z");
private RecyclerView rv ;
private Ap<String> ap;
private List<String> datas;
private EditText edAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
rv = findViewById(R.id.rv);
edAdd =findViewById(R.id.et_add);
rv.setLayoutManager(new GridLayoutManager(this,3));
// rv.setLayoutManager(new LinearLayoutManager(this));
ap = new Ap(this, datas);
rv.setAdapter(ap);
helper.attachToRecyclerView(rv);
findViewById(R.id.tv).setOnClickListener(this);
findViewById(R.id.tv_add).setOnClickListener(this);
}
private void initData() {
datas = new ArrayList<>();
// d ,Arrays.asList ;
for (int i = 0; i < d.size(); i++) {
datas.add(d.get(i));
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv:
for (int i = 0; i < datas.size(); i++) {
Log.i(TAG, "onClick: ____"+datas.get(i));
}
break;
case R.id.tv_add:
ap.add(edAdd.getText().toString().trim());
edAdd.setText(null);
break;
}
}
class Ap<T> extends RecyclerView.Adapter<Ap.Vh>{
private Context context;
public List<T> stringList;
public Ap(Context context, List<T> stringList) {
this.context = context;
this.stringList = stringList;
}
@Override
public Ap.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
return new Vh(LayoutInflater.from(context).inflate(R.layout.item_rv,null));
}
@Override
public void onBindViewHolder(Ap.Vh holder, final int position) {
holder.tv.setText(stringList.get(position).toString());
holder.iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
remove(position);
}
});
}
@Override
public int getItemCount() {
return stringList.size();
}
public void add(T item){
int position = stringList.size();
stringList.add(item);
notifyItemInserted(position);
}
public void add(int position,T item){
stringList.add(position,item);
notifyItemInserted(position);
}
// public void remove(T item) {
// final int position = stringList.indexOf(item);
// if (-1 == position)
// return;
// stringList.remove(item);
// notifyItemRemoved(position);
// }
public void remove(int position) {
stringList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,stringList.size());
}
class Vh extends RecyclerView.ViewHolder {
public Vh(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
iv = itemView.findViewById(R.id.iv_delete);
}
public TextView tv;
public ImageView iv;
}
}
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFrlg = 0;
if (recyclerView.getLayoutManager() instanceof GridLayoutManager){
dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
}else if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
}
return makeMovementFlags(dragFrlg,0);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
// , ,
// Collections.swap(datas,viewHolder.getAdapterPosition(),target.getAdapterPosition());
// ap.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
// viewHolder Position
int fromPosition = viewHolder.getAdapterPosition();
// item viewHolder
int toPosition = target.getAdapterPosition();
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(datas, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(datas, i, i - 1);
}
}
ap.notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// ;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
/**
* Item
*
* @param viewHolder
* @param actionState
*/
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setBackgroundColor(Color.RED);
// // 70
Vibrator vib = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(70);
}
super.onSelectedChanged(viewHolder, actionState);
}
/**
*
* @param recyclerView
* @param viewHolder
*/
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(0);
ap.notifyDataSetChanged(); // ,
}
});
}
Xml 레이아웃 파일:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ccb.pactera.dragrecyclerviewdemo.MainActivity">
<LinearLayout
android:id="@+id/ll"
android:background="#eeeeee"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="48dp"
android:id="@+id/tv"
android:text=" "
android:textColor="#fff"
android:textSize="16dp"
android:gravity="center"
android:background="@color/colorAccent"
/>
<EditText
android:id="@+id/et_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="48dp"
android:id="@+id/tv_add"
android:text=" "
android:textColor="#fff"
android:textSize="16dp"
android:gravity="center"
android:background="#fc1"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_below="@id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
RecyclerView 의 항목:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#f1f1f1"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_margin="0.5dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:src="@mipmap/icon"
android:id="@+id/iv"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_below="@id/iv"
android:gravity="center_horizontal"
android:layout_marginBottom="10dp"
/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:src="@mipmap/detele"
android:id="@+id/iv_delete"
/>
</RelativeLayout>
</RelativeLayout>
자원 링크이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Android】RecyclerView+RadioButton의 작성RadioGroup을 사용하지 않고 작성합니다. RecyclerView 배치 activity_main.xml RadioButton 배치 view_item.xml RecyclerView 용 어댑터 작성. 어느 버튼을 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.