Android,QQ 사 이 드 슬라이딩(삭제,정상 설정 등)기능 구현

QQ 슬라이딩 과 같은 조작 가능 한 기능 을 실현 하고 인터넷 에서 LinearLayout 를 사용자 정의 하여 이 효 과 를 실현 하 는 것 을 볼 수 있 으 나 유연성 에 한계 가 있다.이 demo 는 오픈 소스 프로젝트 SwipeLayout 를 사용 하여 이 기능 을 실현 합 니 다.SwipeLayout 의 일반적인 설정 과 속성 에 대해 소개 합 니 다.다음은 본론 으로 들 어 갑 니 다.
효과 도

코드 세 션
홈 페이지 레이아웃 과 홈 페이지 의 자바 코드 는 평소 사용 하 는 것 과 다 르 지 않 으 므 로 코드 를 붙 일 필요 가 없습니다.여기 서 사용 하 는 ListView 프 리 젠 테 이 션 은 GridView,ExpandableListView 일 수도 있 습 니 다.
가장 중요 한 코드 부분,ListView 어댑터 레이아웃:

<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/swipe"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:drag_edge="right">

 <LinearLayout
  android:id="@+id/trash"
  android:layout_width="160dp"
  android:layout_height="match_parent"
  android:gravity="center"
  android:orientation="horizontal"
  android:tag="Bottom">

  <TextView
   android:id="@+id/swipe_open"
   android:layout_width="1dp"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:background="#f55509"
   android:gravity="center"
   android:text="  "
   android:textColor="@android:color/white"
   android:textSize="20sp" />

  <TextView
   android:id="@+id/swipe_delete"
   android:layout_width="1dp"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:background="@android:color/holo_red_dark"
   android:gravity="center"
   android:text="  "
   android:textColor="@android:color/white"
   android:textSize="20sp" />

 </LinearLayout>

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/white"
  android:gravity="center_vertical"
  android:orientation="horizontal"
  android:padding="5dp">

  <ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher" />

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@android:color/white"
   android:orientation="vertical"
   android:paddingLeft="5dp">

   <TextView
    android:id="@+id/tv_nickname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="17sp" />

   <TextView
    android:id="@+id/tv_msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:textSize="15sp" />
  </LinearLayout>
 </LinearLayout>
</com.daimajia.swipe.SwipeLayout>
설명:가장 바깥쪽 은 우리 의 SwipeLayout 이 고 안 에는 두 개의 LinearLayout 이 있 으 며 첫 번 째 층 은 우리 의 페이지 레이아웃 이 고 두 번 째 층 은 우리 의 사 이 드 에 그 려 진 레이아웃 입 니 다.관건 적 인 속성 은 여기에 나타난다.
app:drag_edge="right"
이 속성 은 사 이 드 레이아웃 의 위 치 를 설정 하 는 것 입 니 다.기본 값 은 오른쪽 입 니 다.왼쪽,아래쪽,상단 을 설정 할 수 있 습 니 다.
어댑터 Java 코드:

package com.example.mjj.swipelayoutdemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;

import java.util.List;

/**
 * Description:   
 * <p>
 * Created by Mjj on 2016/12/12.
 */

public class MySwipeAdapter extends BaseSwipeAdapter implements View.OnClickListener {

 private Context context;
 private List<ItemBean> list;
 private final String TAG = "MySwipeAdapter";

 public MySwipeAdapter(Context context, List<ItemBean> list) {
  this.context = context;
  this.list = list;
 }

 /**
  *   SwipeLayout ID
  *
  * @param position
  * @return
  */
 @Override
 public int getSwipeLayoutResourceId(int position) {
  return R.id.swipe;
 }

 /**
  *     
  *
  * @param position
  * @param parent
  * @return
  */
 @Override
 public View generateView(int position, ViewGroup parent) {
  View itemView = View.inflate(context, R.layout.item_swipe, null);
  SwipeLayout swipeLayout = (SwipeLayout) itemView.findViewById(getSwipeLayoutResourceId(position));

  //         ,   true
  swipeLayout.setSwipeEnabled(true);

  /**
   *      :    onStartOpen,onUpdate,onHandRelease,onUpdate,onOpen,
   *      :onStartClose,onUpdate,onHandRelease,onHandRelease,onUpdate,onClose
   */
  swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
   @Override
   public void onStartOpen(SwipeLayout layout) {
//    Log.e(TAG, "onStartOpen: ");
   }

   @Override
   public void onOpen(SwipeLayout layout) {
//    Log.e(TAG, "onOpen: ");
   }

   @Override
   public void onStartClose(SwipeLayout layout) {
//    Log.e(TAG, "onStartClose: ");
   }

   @Override
   public void onClose(SwipeLayout layout) {
//    Log.e(TAG, "onClose: ");
   }

   @Override
   public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//    Log.e(TAG, "onUpdate: ");
   }

   @Override
   public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//    Log.e(TAG, "onHandRelease: ");
   }
  });

  //    true,     item(       )   ,         ,   false
  swipeLayout.setClickToClose(true);

  // SwipeLayout    ,   ListView OnitemClickListener  .
  swipeLayout.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
//    Log.e(TAG, "onClick: ");
   }
  });

  return itemView;
 }

 /**
  *     
  *
  * @param position
  * @param convertView
  */
 @Override
 public void fillValues(int position, View convertView) {
  TextView tvNickName = (TextView) convertView.findViewById(R.id.tv_nickname);
  TextView tvMsg = (TextView) convertView.findViewById(R.id.tv_msg);
  TextView tvSwipeOpen = (TextView) convertView.findViewById(R.id.swipe_open);
  TextView tvSwipeDelete = (TextView) convertView.findViewById(R.id.swipe_delete);
  tvNickName.setText(list.get(position).getNickName());
  tvMsg.setText(list.get(position).getMsg());

  tvSwipeDelete.setOnClickListener(this);
  tvSwipeOpen.setOnClickListener(this);
 }

 @Override
 public int getCount() {
  return list.size();
 }

 @Override
 public ItemBean getItem(int i) {
  return list.get(i);
 }

 @Override
 public long getItemId(int i) {
  return i;
 }

 @Override
 public void onClick(View view) {
  int id = view.getId();
  switch (id) {
   case R.id.swipe_open:
    //        Swipe item
    this.closeAllItems();
    Toast.makeText(context, "Swipe--Open", Toast.LENGTH_SHORT).show();
    break;
   case R.id.swipe_delete:
    this.closeAllItems();
    Toast.makeText(context, "Swipe--Delete", Toast.LENGTH_SHORT).show();
    break;
  }
 }
}
설명:평소 우리 가 쓴 어댑터 와 달리 BaseSwipeAdapter 에서 계승 하 는 방법 은 그림 에서 보 여 주 는 것 외 에 getItemId()가 있 습 니 다.다른 건 없어.여기 서 주로 평소에 보지 못 했 던 몇 가지 방법 을 설명 한다.

public int getSwipeLayoutResourceId(int position)
이 방법 은 레이아웃 ID 가 아 닌 SwipeLayout 의 ID 를 되 돌려 줍 니 다.

public View generateView(int position, ViewGroup parent)
이 방법 은 우리 의 item 레이아웃 과 연 결 된 역할 을 되 돌려 주 며,swipeLayout 의 관련 속성 을 설정 합 니 다.

public void fillValues(int position, View convertView)
이 방법 은 아 이 템 의 컨트롤 에 데 이 터 를 연결 하고 필요 에 따라 이벤트 등 을 설정 하 는 데 사 용 됩 니 다.
*3.상용 설정 안내*
1.만약 에 우리 의 이 어댑터 가 다시 사용 되 고 어떤 때 는 미끄럼 기능 이 필요 하지 않 으 면 이 방법 으로 미끄럼 을 제어 할 수 있 습 니 다. 

swipeLayout.setSwipeEnabled(true);
2.우리 의 사 이 드 레이아웃 이 나 올 때 이 아 이 템 을 누 르 면 기본적으로 회수 되 지 않 습 니 다.즉,아래 코드 는 기본적으로 false 입 니 다.

falseswipeLayout.setClickToClose(true);
3.프 리 젠 테 이 션 과 같이 삭제 하거나 열 면 그 려 진 사 이 드 레이아웃 을 자동 으로 회수 하고 아래 속성 closeAllItems()방법 으로 제어 합 니 다.묵인 은 회수 하지 않 을 것 이다.

this.closeAllItems();
4.앞에서 언급 했 듯 이 우리 의 사 이 드 슬라이드 가 나타 난 위 치 는 왼쪽 이나 오른쪽 이 필요 하 다 면 잊 지 마 세 요.

app:drag_edge="right"
*4.사용*
compile 'com.daimajia.swipelayout:library:1.2.0'
총화
demo 는 github 에 업로드 되 었 습 니 다.링크 는 공중 번호'code 소생'에 놓 여 있 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기