user3033262
user3033262

Reputation: 41

Adding Views to ListView Row when it selected

Im developing an android app and i want to add views to the row on which the user selected for the user to select what next to do.An example of such implementation is found in the plume app for twitter...i dont know how to go about it.I have implemented the following code but noting is working.

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

        }
    });

Upvotes: 0

Views: 951

Answers (1)

Shahinoor Shahin
Shahinoor Shahin

Reputation: 685

I do this work another way for my need. Here when user click on list item then an additional view is just added below of that list item. If you clicked again then view is minimized. For that you can do this :

1) Initialize your listview :

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) Add 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) Class for animation :

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) View which will be added 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>

That's all. I think it will help you.

Upvotes: 1

Related Questions