Custom ListView not collapsing after expansion

432 Views Asked by At

I have a custom listView that displays an Image and some text, as well as 3 buttons that only appear once an item is tapped. They Appear after a short drop down animation.

After clicking an item on the list, the buttons appear, but on clicking the same item, the buttons do not retract away from view, they remain active and can be clicked on, but when i click other items, the row expands and shows the buttons however they also dont collapse after clicking again.

On a side note, I'd also like to know how to auto collapse other items once another item on the list is expanded

Here is a row of the listView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitCenter"
        android:src="@drawable/placeholder" />

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="8dp"
        android:maxLines="3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:orientation="horizontal"
        android:id="@+id/listview_toolbar"
        android:layout_marginBottom="-50dip"
        android:visibility="gone"
        android:weightSum="90">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:text="@string/view"
            android:focusableInTouchMode="false"
            android:id="@+id/view"
            android:layout_weight="30" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:text="@string/edit"
            android:id="@+id/edit"
            android:focusableInTouchMode="false"
            android:layout_weight="30" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:focusableInTouchMode="false"
            android:text="@string/remove"
            android:id="@+id/remove"
            android:layout_weight="30" />

    </LinearLayout>


</LinearLayout>

Here Is my custom Adapter

public class MyAdapter extends ArrayAdapter<String> {
        private final Activity context;
        private final ArrayList<String> names;

        class ViewHolder {
            public TextView text;
            public ImageView image;
            public View toolbar;
        }

        public MyAdapter(Activity context, ArrayList<String> names) {
            super(context, R.layout.ui_row, names);
            this.context = context;
            this.names = names;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // reuse views
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.ui_row, null);
                // configure view holder
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.text = (TextView) convertView.findViewById(R.id.label);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.icon);
                viewHolder.toolbar = convertView.findViewById(R.id.listview_toolbar);
                convertView.setTag(viewHolder);
            }
            // fill data
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.text.setText(filelist.get(position));//sets textview of item row in listview
            PicassoTools.clearCache(Picasso.with(context));//resets picasso, erases caches
            Picasso.with(context).load(new File( Environment.getExternalStorageDirectory() + "//Thumbnails//" + position)).into(holder.image);
            ((LinearLayout.LayoutParams) holder.toolbar.getLayoutParams()).bottomMargin = -50;
            holder.toolbar.setVisibility(View.GONE);
            return convertView;
        }


    }

Here's the onCreate

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainactivity);

            ac = this;
            adapter = new MyAdapter(ac, filelist);



            listview = (ListView) findViewById(R.id.listview);


                    listview.setAdapter(adapter);
                    ListViewSetClickListener();

listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
            {

                Toast.makeText(getApplicationContext(), "Button Tap", Toast.LENGTH_SHORT).show();

                View toolbar = view.findViewById(R.id.listview_toolbar);
                Expand_Custom_Animation expandAni = new Expand_Custom_Animation(toolbar, 500);
                toolbar.startAnimation(expandAni);

                Button viewB = (Button) view.findViewById(R.id.view);
                viewB.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Toast.makeText(getApplicationContext(), "View", Toast.LENGTH_SHORT).show();
                    }
                });

                Button edit = (Button) view.findViewById(R.id.edit);
                edit.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Toast.makeText(getApplicationContext(), "Edit", Toast.LENGTH_SHORT).show();
                    }
                });

                Button remove = (Button) view.findViewById(R.id.remove);
                remove.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Toast.makeText(getApplicationContext(), "Remove", Toast.LENGTH_SHORT).show();
                    }
                });


            }
        });



        }

Here's the Animation Class, Expand_Custom_Animation.java

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

    public Expand_Custom_Animation(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) 
        {
            mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
            mAnimatedView.requestLayout();
        } 
        else if (!mWasEndedAlready)
        {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

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

There are 1 best solutions below

0
On BEST ANSWER

Turns out the

        android:focusableInTouchMode="false"

in my xml layouts wasn't working. So i did it programmatically via the ListView OnclickListener for every button with this code

            button.setFocusable(false);

Works OK now