中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android仿QQ首頁ListView左滑置頂、刪除功能

發布時間:2020-10-09 16:55:41 來源:腳本之家 閱讀:168 作者:預見才能遇見 欄目:移動開發

 Android 仿QQ首頁ListView左滑置頂、刪除等實現源碼,具體內容如下

效果圖

Android仿QQ首頁ListView左滑置頂、刪除功能

實現源碼: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) { 
  // 緩慢滾動到指定位置 
  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 = "青島爆炸滿月:大量魚蝦死亡"; 
     item.time = "晚上18:18"; 
    } else { 
     item.iconRes = R.drawable.delete_wechat_icon; 
     item.title = "微信團隊"; 
     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+"個條目", 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左滑置頂刪除功能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阿瓦提县| 耒阳市| 西峡县| 扶绥县| 滨州市| 黔江区| 文登市| 浮山县| 梓潼县| 望江县| 志丹县| 武平县| 青神县| 襄汾县| 古田县| 浠水县| 策勒县| 泰和县| 巫溪县| 前郭尔| 宁城县| 建昌县| 泰安市| 会同县| 云梦县| 博爱县| 如皋市| 洞头县| 武川县| 梁河县| 重庆市| 澄江县| 武冈市| 集安市| 曲周县| 周至县| 自治县| 五原县| 南木林县| 弥渡县| 柘城县|