如何创建扩展和缩小的列表视图?

时间:2014-01-24 10:55:42

标签: android performance android-listview

在我的应用程序中,我有listview with plus&加号按钮上的减号按钮单击列表视图应该展开,在减号按钮上单击它应该缩小。我用以下代码完成了这个:

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);
}

但我需要的是用户可以打开单个链接,假设用户尝试点击前一个应该收缩的其他一个,然后再打开新链接。这该怎么做??我希望你有我的问题

请事先提出建议

1 个答案:

答案 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);
                }
            });             
        }
    });

这在Listview适配器类

@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;

Listview适配器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;
    }
}}

这将有助于您使Listview成为可扩展的Listview