亲宝软件园·资讯

展开

Android仿QQ左滑置顶删除功能 Android仿QQ首页ListView左滑置顶、删除功能

预见才能遇见 人气:0
想了解Android仿QQ首页ListView左滑置顶、删除功能的相关内容吗,预见才能遇见在本文为您仔细讲解Android仿QQ左滑置顶删除功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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左滑置顶删除功能

加载全部内容

相关教程
猜你喜欢
用户评论