选择时向ListView行添加视图

时间:2014-01-01 02:38:37

标签: android listview

我正在开发一个Android应用程序,我想添加视图到用户选择用户选择接下来要做的行。在twitter的plume应用程序中找到了这样的实现示例...我不我知道如何去做。我已经实现了以下代码,但注意到了。

        teaam_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Button btn= new Button(getActivity());

            btn.setText("added with click");
            ArrayList<View> vs= new ArrayList<View>();
            vs.add(btn);


           view.addChildrenForAccessibility(vs);


           view.invalidate();

        }
    });

1 个答案:

答案 0 :(得分:1)

我以另一种方式为我的需要做这项工作。在此处,当用户单击列表项时,将在该列表项的下方添加另一个视图。如果再次单击,则视图最小化。为此你可以这样做:

1)初始化您的列表视图:

mListView = (ListView) findViewById(R.id.lv_settings_list);
    mSettingListArrayAdapter = new SettingsListAdapter(this, R.layout.list_items,
            mSettingsArrayList);
    mSettingListArrayAdapter.notifyDataSetChanged();
    mListView.setAdapter(mSettingListArrayAdapter);
    mListView.setOnItemClickListener(this);

2)添加onItemClickListener:

    @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    View toolbar = view.findViewById(R.id.toolbar);
    ExpandAnimation expandAnimation = new ExpandAnimation(toolbar, 500);
    toolbar.startAnimation(expandAnimation);
}

3)动画类:

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

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

}

4)查看将在onItemClick上添加的内容:

<LinearLayout
    android:id="@+id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="-50dip"
    android:divider="#6F8290"
    android:gravity="right"
    android:visibility="gone" >

    <ImageView
        android:id="@+id/iv_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:paddingRight="5dip"
        android:focusableInTouchMode="false"
        android:src="@drawable/edit" />

    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:paddingLeft="5dip"
        android:focusableInTouchMode="false"
        android:paddingRight="10dip"
        android:src="@drawable/delete"
        android:visibility="gone" />
</LinearLayout>

这就是全部。我认为它会对你有帮助。