2014-01-24 46 views
0

在我的應用程序我有加上&加上按鈕加上按鈕單擊列表視圖應展開和減號按鈕單擊它應縮小。我已經用下面的代碼這樣做:如何創建擴展和縮小的listview?

if (isChecked) { 
    objectsOrder.get(getPosition).setSelected(
    buttonView.isChecked()); 
    holder.details.setVisibility(View.VISIBLE); 
    holder.iorder.setVisibility(View.VISIBLE); 
} else { 
    objectsOrder.get(getPosition).setSelected(false); 
    holder.details.setVisibility(View.GONE); 
    holder.iorder.setVisibility(View.GONE); 
} 

,但我需要的是用戶可以能開單鏈路假設用戶嘗試點擊另一個之前開放新的前一個應該收縮。這個怎麼做??我希望你得到了我的問題

請給提前一個建議感謝

+2

你爲什麼不直接使用可擴展列表視圖。它會做你想要的。 – InnocentKiller

+0

在我的應用程序中應該只發生加號或減號按鈕點擊不列表項目點擊 –

回答

4

使用這mainactivity

ListView gv = (ListView) findViewById(R.id.listView1); 
gv.setAdapter(new TextAdapter()); 
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 

        View toolbar = view.findViewById(R.id.toolbar); 

        // Creating the expand animation for the item 
        ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 

        // Start the animation on the toolbar 
        toolbar.startAnimation(expandAni); 
       } 
      });    
     } 
    }); 

這在你的列表視圖適配器類

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    <-- your codes --> 

    View toolbar = convertView.findViewById(R.id.toolbar); 
     ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50; 
     toolbar.setVisibility(View.GONE); 
     return convertView; 

列表視圖適配器XML文件

<LinearLayout android:id="@+id/toolbar" 
     android:layout_marginBottom="-50dip" 
     android:visibility="gone" 
     android:layout_height="50dip" 
     android:layout_width="fill_parent"> 
    <Button android:id="@+id/doSomething1" 
      android:layout_height="50dip" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_width="wrap_content" 
      android:text="Harder"/> 
    <Button android:id="@+id/doSomething2" 
      android:layout_height="50dip" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_width="wrap_content" 
      android:text="Better"/> 
    <Button android:id="@+id/doSomething3" 
      android:layout_height="50dip" 
      android:layout_width="wrap_content" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Faster"/> 
    <Button android:id="@+id/doSomething4" 
      android:layout_height="50dip" 
      android:layout_width="wrap_content" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Stronger"/> 

</LinearLayout> 

這是ExpandAnimation類

public class ExpandAnimation extends Animation { 
private View mAnimatedView; 
private LayoutParams mViewLayoutParams; 
private int mMarginStart, mMarginEnd; 
private boolean mIsVisibleAfter = false; 
private boolean mWasEndedAlready = false; 


public ExpandAnimation(View view, int duration) { 

    setDuration(duration); 
    mAnimatedView = view; 
    mViewLayoutParams = (LayoutParams) view.getLayoutParams(); 

    // decide to show or hide the view 
    mIsVisibleAfter = (view.getVisibility() == View.VISIBLE); 

    mMarginStart = mViewLayoutParams.bottomMargin; 
    mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0); 

    view.setVisibility(View.VISIBLE); 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    super.applyTransformation(interpolatedTime, t); 

    if (interpolatedTime < 1.0f) { 

     // Calculating the new bottom margin, and setting it 
     mViewLayoutParams.bottomMargin = mMarginStart 
       + (int) ((mMarginEnd - mMarginStart) * interpolatedTime); 

     // Invalidating the layout, making us seeing the changes we made 
     mAnimatedView.requestLayout(); 

    // Making sure we didn't run the ending before (it happens!) 
    } else if (!mWasEndedAlready) { 
     mViewLayoutParams.bottomMargin = mMarginEnd; 
     mAnimatedView.requestLayout(); 

     if (mIsVisibleAfter) { 
      mAnimatedView.setVisibility(View.GONE); 
     } 
     mWasEndedAlready = true; 
    } 
}} 

這將幫助ü使列表視圖以列表視圖可擴展