溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android仿QQ首頁(yè)ListView左滑置頂、刪除功能

發(fā)布時(shí)間:2020-10-09 16:55:41 來(lái)源:腳本之家 閱讀:167 作者:預(yù)見(jiàn)才能遇見(jiàn) 欄目:移動(dòng)開(kāi)發(fā)

 Android 仿QQ首頁(yè)ListView左滑置頂、刪除等實(shí)現(xiàn)源碼,具體內(nèi)容如下

效果圖

Android仿QQ首頁(yè)ListView左滑置頂、刪除功能

實(shí)現(xiàn)源碼:package com.duguang.baseanimation.ui.listivew.deletelistview;  
 

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.util.TypedValue; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.Scroller; 
import android.widget.TextView; 
 
import com.kayak.deletelistview.R; 
 
public class SlideView extends LinearLayout { 
 
 private static final String TAG = "SlideView"; 
 
 private Context mContext; 
 private LinearLayout mViewContent; 
 private RelativeLayout mHolder; 
 private Scroller mScroller; 
 private OnSlideListener mOnSlideListener; 
 
 private int mHolderWidth = 120; 
 
 private int mLastX = 0; 
 private int mLastY = 0; 
 private static final int TAN = 2; 
 
 public interface OnSlideListener { 
  public static final int SLIDE_STATUS_OFF = 0; 
  public static final int SLIDE_STATUS_START_SCROLL = 1; 
  public static final int SLIDE_STATUS_ON = 2; 
 
  /** 
   * @param view current SlideView 
   * @param status SLIDE_STATUS_ON or SLIDE_STATUS_OFF 
   */ 
  public void onSlide(View view, int status); 
 } 
 
 public SlideView(Context context) { 
  super(context); 
  initView(); 
 } 
 
 public SlideView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
  initView(); 
 } 
 
 private void initView() { 
  mContext = getContext(); 
  mScroller = new Scroller(mContext); 
 
  setOrientation(LinearLayout.HORIZONTAL); 
  View.inflate(mContext, R.layout.activity_listview_delete_slide_view_merge, this); 
  mViewContent = (LinearLayout) findViewById(R.id.view_content); 
  mHolderWidth = Math.round(TypedValue.applyDimension( 
    TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources() 
      .getDisplayMetrics())); 
 } 
 
 public void setButtonText(CharSequence text) { 
  ((TextView)findViewById(R.id.delete)).setText(text); 
 } 
 
 public void setContentView(View view) { 
  mViewContent.addView(view); 
 } 
 
 public void setOnSlideListener(OnSlideListener onSlideListener) { 
  mOnSlideListener = onSlideListener; 
 } 
 
 public void shrink() { 
  if (getScrollX() != 0) { 
   this.smoothScrollTo(0, 0); 
  } 
 } 
 
 public void onRequireTouchEvent(MotionEvent event) { 
  int x = (int) event.getX(); 
  int y = (int) event.getY(); 
  int scrollX = getScrollX(); 
  Log.d(TAG, "x=" + x + " y=" + y); 
 
  switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN: { 
   if (!mScroller.isFinished()) { 
    mScroller.abortAnimation(); 
   } 
   if (mOnSlideListener != null) { 
    mOnSlideListener.onSlide(this, 
      OnSlideListener.SLIDE_STATUS_START_SCROLL); 
   } 
   break; 
  } 
  case MotionEvent.ACTION_MOVE: { 
   int deltaX = x - mLastX; 
   int deltaY = y - mLastY; 
   if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) { 
    break; 
   } 
 
   int newScrollX = scrollX - deltaX; 
   if (deltaX != 0) { 
    if (newScrollX < 0) { 
     newScrollX = 0; 
    } else if (newScrollX > mHolderWidth) { 
     newScrollX = mHolderWidth; 
    } 
    this.scrollTo(newScrollX, 0); 
   } 
   break; 
  } 
  case MotionEvent.ACTION_UP: { 
   int newScrollX = 0; 
   if (scrollX - mHolderWidth * 0.75 > 0) { 
    newScrollX = mHolderWidth; 
   } 
   this.smoothScrollTo(newScrollX, 0); 
   if (mOnSlideListener != null) { 
    mOnSlideListener.onSlide(this, 
      newScrollX == 0 ? OnSlideListener.SLIDE_STATUS_OFF 
        : OnSlideListener.SLIDE_STATUS_ON); 
   } 
   break; 
  } 
  default: 
   break; 
  } 
 
  mLastX = x; 
  mLastY = y; 
 } 
 
 private void smoothScrollTo(int destX, int destY) { 
  // 緩慢滾動(dòng)到指定位置 
  int scrollX = getScrollX(); 
  int delta = destX - scrollX; 
  mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3); 
  invalidate(); 
 } 
 
 @Override 
 public void computeScroll() { 
  if (mScroller.computeScrollOffset()) { 
   scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); 
   postInvalidate(); 
  } 
 } 
 
} 

package com.duguang.baseanimation.ui.listivew.deletelistview; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ListView; 
 
import com.duguang.baseanimation.ui.listivew.deletelistview.DeleteListViewMainActivity.MessageItem; 
 
public class ListViewCompat extends ListView { 
 
 private static final String TAG = "ListViewCompat"; 
 
 private SlideView mFocusedItemView; 
 
 public ListViewCompat(Context context) { 
  super(context); 
 } 
 
 public ListViewCompat(Context context, AttributeSet attrs) { 
  super(context, attrs); 
 } 
 
 public ListViewCompat(Context context, AttributeSet attrs, int defStyle) { 
  super(context, attrs, defStyle); 
 } 
 
 public void shrinkListItem(int position) { 
  View item = getChildAt(position); 
 
  if (item != null) { 
   try { 
    ((SlideView) item).shrink(); 
   } catch (ClassCastException e) { 
    e.printStackTrace(); 
   } 
  } 
 } 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN: { 
   int x = (int) event.getX(); 
   int y = (int) event.getY(); 
   int position = pointToPosition(x, y); 
   Log.e(TAG, "postion=" + position); 
   if (position != INVALID_POSITION) { 
    MessageItem data = (MessageItem) getItemAtPosition(position); 
    mFocusedItemView = data.slideView; 
    Log.e(TAG, "FocusedItemView=" + mFocusedItemView); 
   } 
  } 
  default: 
   break; 
  } 
 
  if (mFocusedItemView != null) { 
   mFocusedItemView.onRequireTouchEvent(event); 
  } 
 
  return super.onTouchEvent(event); 
 } 
 
} 

package com.duguang.baseanimation.ui.listivew.deletelistview; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import com.duguang.baseanimation.ui.base.BaseActivity; 
import com.duguang.baseanimation.ui.listivew.deletelistview.SlideView.OnSlideListener; 
import com.kayak.deletelistview.R; 
 
public class DeleteListViewMainActivity extends BaseActivity implements OnItemClickListener, OnClickListener, 
  OnSlideListener { 
 
 private static final String TAG = "MainActivity"; 
 
 private ListViewCompat mListView; 
 
 private List<MessageItem> mMessageItems = new ArrayList<DeleteListViewMainActivity.MessageItem>(); 
 
 private SlideView mLastSlideViewWithStatusOn; 
  
 private SlideAdapter adapter; 
  
 @Override 
 public void setView() { 
   setContentView(R.layout.activity_listview_delete_main); 
   
 } 
 
 @Override 
 public void initView() { 
   mListView = (ListViewCompat) findViewById(R.id.list); 
 
   for (int i = 0; i < 20; i++) { 
    MessageItem item = new MessageItem(); 
    if (i % 3 == 0) { 
     item.iconRes = R.drawable.delete_default_qq_avatar; 
     item.title = "騰訊新聞"; 
     item.msg = "青島爆炸滿月:大量魚(yú)蝦死亡"; 
     item.time = "晚上18:18"; 
    } else { 
     item.iconRes = R.drawable.delete_wechat_icon; 
     item.title = "微信團(tuán)隊(duì)"; 
     item.msg = "歡迎你使用微信"; 
     item.time = "12月18日"; 
    } 
    mMessageItems.add(item); 
   } 
    
   adapter = new SlideAdapter(); 
   mListView.setAdapter(adapter); 
   mListView.setOnItemClickListener(this); 
   
 } 
 
 @Override 
 public void setListener() { 
  // TODO Auto-generated method stub 
   
 } 
 
 private class SlideAdapter extends BaseAdapter { 
 
  private LayoutInflater mInflater; 
  SlideAdapter() { 
   super(); 
   mInflater = getLayoutInflater(); 
  } 
 
  @Override 
  public int getCount() { 
   return mMessageItems.size(); 
  } 
 
  @Override 
  public Object getItem(int position) { 
   return mMessageItems.get(position); 
  } 
 
  @Override 
  public long getItemId(int position) { 
   return position; 
  } 
 
  @Override 
  public View getView(final int position, View convertView, ViewGroup parent) { 
   ViewHolder holder; 
   SlideView slideView = (SlideView) convertView; 
   if (slideView == null) { 
    View itemView = mInflater.inflate(R.layout.item_listview_delete, null); 
 
    slideView = new SlideView(DeleteListViewMainActivity.this); 
    slideView.setContentView(itemView); 
 
    holder = new ViewHolder(slideView); 
    slideView.setOnSlideListener(DeleteListViewMainActivity.this); 
    slideView.setTag(holder); 
   } else { 
    holder = (ViewHolder) slideView.getTag(); 
   } 
   MessageItem item = mMessageItems.get(position); 
   item.slideView = slideView; 
   item.slideView.shrink(); 
 
   holder.icon.setImageResource(item.iconRes); 
   holder.title.setText(item.title); 
   holder.msg.setText(item.msg); 
   holder.time.setText(item.time); 
   holder.deleteHolder.setOnClickListener(new OnClickListener() { 
     
    @Override 
    public void onClick(View v) { 
     mMessageItems.remove(position); 
     adapter.notifyDataSetChanged(); 
     Toast.makeText(DeleteListViewMainActivity.this, "刪除第" + position+"個(gè)條目", 0).show(); 
    } 
   }); 
    
   return slideView; 
  } 
 
 } 
 
 public class MessageItem { 
  public int iconRes; 
  public String title; 
  public String msg; 
  public String time; 
  public SlideView slideView; 
 } 
 
 private static class ViewHolder { 
  public ImageView icon; 
  public TextView title; 
  public TextView msg; 
  public TextView time; 
  public ViewGroup deleteHolder; 
 
  ViewHolder(View view) { 
   icon = (ImageView) view.findViewById(R.id.icon); 
   title = (TextView) view.findViewById(R.id.title); 
   msg = (TextView) view.findViewById(R.id.msg); 
   time = (TextView) view.findViewById(R.id.time); 
   deleteHolder = (ViewGroup)view.findViewById(R.id.holder); 
  } 
 } 
 
 @Override 
 public void onItemClick(AdapterView<?> parent, View view, int position, 
   long id) { 
  Toast.makeText(this, "onItemClick position=" + position, 0).show(); 
   
 } 
 
 @Override 
 public void onSlide(View view, int status) { 
  if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) { 
   mLastSlideViewWithStatusOn.shrink(); 
  } 
 
  if (status == SLIDE_STATUS_ON) { 
   mLastSlideViewWithStatusOn = (SlideView) view; 
  } 
 } 
 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
 case R.id.holder: 
   
  break; 
 
 default: 
  break; 
 } 
 } 
 
} 

源碼下載地址:Android仿QQ左滑置頂刪除功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI