Back to project page DynamicListView.
The source code is released under:
Apache License
If you think the Android project DynamicListView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.dynamiclistview; //from w w w .jav a 2 s .c o m import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; public class DynamicListView extends ListView implements OnTouchListener, android.widget.AdapterView.OnItemLongClickListener { private OnClickButtonListener mListener; private boolean mExtraItemShown; private ViewGroup mItemLayout; // The origin item in listview. private View mMainItem; // The item which will be replace the main item. private ViewGroup mExtraItem; // The listener of extraItem's button. private OnClickListener innerOnClickListener = new OnClickListener() { @Override public void onClick(View v) { restoreMainItem(); ViewGroup parent = (ViewGroup) v.getParent(); int index = parent.indexOfChild(v); mListener.onClickButton(index); } }; public DynamicListView(Context context) { super(context); init(); } public DynamicListView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public void setOnClickButtonListener(OnClickButtonListener l) { mListener = l; } @Override public boolean onTouch(View v, MotionEvent event) { if (mExtraItemShown && event.getAction() != MotionEvent.ACTION_UP) { restoreMainItem(); } return false; } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { if (!mExtraItemShown) { mItemLayout = (ViewGroup) getChildAt(position - getFirstVisiblePosition()); mExtraItem = (ViewGroup) LayoutInflater.from(getContext()).inflate( R.layout.extra_item, null); Animation inAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in); mExtraItem.startAnimation(inAnimation); setButtonsListener(mExtraItem); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); mItemLayout.addView(mExtraItem, params); Animation outAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_out); mMainItem = mItemLayout.getChildAt(0); mMainItem.startAnimation(outAnimation); mMainItem.setVisibility(View.INVISIBLE); mExtraItemShown = true; } return true; } /** * @author sampythoner * * The caller can implements this interface, which will deliver the * button's index to the caller. * */ public interface OnClickButtonListener { void onClickButton(int index); } private void init() { setOnTouchListener(this); setOnItemLongClickListener(this); } private void setButtonsListener(ViewGroup extraItem) { ImageButton searchButton = (ImageButton) mExtraItem .findViewById(R.id.imageButton1); searchButton.setOnClickListener(innerOnClickListener); ImageButton editButton = (ImageButton) mExtraItem .findViewById(R.id.imageButton2); editButton.setOnClickListener(innerOnClickListener); ImageButton shareButton = (ImageButton) mExtraItem .findViewById(R.id.imageButton3); shareButton.setOnClickListener(innerOnClickListener); ImageButton sortButton = (ImageButton) mExtraItem .findViewById(R.id.imageButton4); sortButton.setOnClickListener(innerOnClickListener); ImageButton helpButton = (ImageButton) mExtraItem .findViewById(R.id.imageButton5); helpButton.setOnClickListener(innerOnClickListener); } /** * Clear extra item ,recover main item. */ private void restoreMainItem() { Animation outAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_out); mExtraItem.startAnimation(outAnimation); mItemLayout.removeView(mExtraItem); mExtraItem = null; mExtraItemShown = false; Animation inAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in); mMainItem = mItemLayout.getChildAt(0); mMainItem.startAnimation(inAnimation); mMainItem.setVisibility(View.VISIBLE); } }