1

Right now I have a ListView, with a custom ArrayAdapter to populate the items. Right now the items each only show a little bit of information, I want to be able to tap on an item, and have it 'expand', showing the rest of the information. I would like there to be an animation when it expands. I also want to get there to only be one item 'expanded' at once (ie if one is open, and I tap on another, the first one goes back to normal). I would also like to have expanded items stay expanded, even if they are scrolled out of the view.

This is the code I have now

single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="FIRST INFO"
        android:id="@+id/first"/>


    <Space
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:id="@+id/space"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="SECOND INFO"
        android:id="@+id/hidden"/>

</RelativeLayout>

my_cards.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#191919"

              android:id="@+id/card_layout"
              android:weightSum="1">


    <Space
        android:layout_width="match_parent"
        android:layout_height="29dp"
        android:layout_gravity="center_horizontal"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"
        android:id="@+id/list_view"

        android:divider="#191919"
        android:layout_marginRight="23dp"
        android:layout_marginLeft="23dp"
        android:dividerHeight="12dp"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

MyCards.java

public class MyCards extends android.support.v4.app.Fragment{

    private ListView mListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //Inflate the view
        View view = inflater.inflate(R.layout.my_cards, container, false);

        //Arrays for test information
        String[] names = {"Tim", "Kevin", "Bob", "John","Evan", "Roy", "Tristan", "Ronald"};
        String[] company = {"Apple", "Microsoft", "China", "Xiaomi","Facebook", "Unemployed", "CCP", "Government"};

        //Get reference to the listview
        mListView = (ListView) view.findViewById( R.id.list_view );

        //Initialize the adapter, and set it to the listview
        customAdapter adapter = new customAdapter(getContext(),names,company);
        mListView.setAdapter(adapter);

        return view;
    }
}
class customAdapter extends ArrayAdapter<String>{
    Context mContext;
    String[] mName;
    String[] mCompany;

    customAdapter(Context c, String[] names, String[] companies){
        super(c, R.layout.single_row, R.id.name, names);
        this.mContext = c;
        this.mName = names;
        this.mCompany = companies;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View mRow = inflater.inflate(R.layout.single_row, parent, false);

        TextView mNameText = (TextView) mRow.findViewById(R.id.name);
        TextView mCompanyText = (TextView) mRow.findViewById(R.id.company);

        mNameText.setText(mName[position]);
        mCompanyText.setText(mCompany[position]);

        mRow.setBackgroundResource(R.drawable.custom_listview_shape);



        return mRow;
    }
}

Now, I have tried to have a go at getting this to work. I tried adding this to my onCreateView method

  mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView hidden = (TextView) view.findViewById(R.id.hidden);


                if ( hidden.getVisibility() == View.GONE)
                {
                    //expandedChildList.set(arg2, true);
                    hidden.setVisibility(View.VISIBLE);
                }
                else
                {
                    //expandedChildList.set(arg2, false);
                    hidden.setVisibility(View.GONE);
                }
            }
        });

Now, while this technically does what I want it to do (cause item to expand on click), it doesn't do it the way I would like to. There is no animation, multiple items can be expanded at once, and it goes back to normal as soon as it is scrolled out of view.

Any help with this would be greatly appreciated.

ethanzh
  • 133
  • 2
  • 5
  • 19
  • 1
    Either you have to create two layouts for the expanded and collapsed views or you have to use an `ExpandableListView`. – K Neeraj Lal Mar 26 '16 at 15:46
  • So for instance right now I have a **single_row.xml** , by two layouts do you mean making a **expanded_view.xml** ? How would I integrate the two? – ethanzh Mar 26 '16 at 15:48
  • Yeah, you also need a custom Adapter where you can selectively inflate the layout. – K Neeraj Lal Mar 26 '16 at 15:50
  • Okay. Would the **expanded_view.xml** need to inlude the things already in the **single_row.xml**? Also, could you please explain how to 'selectively inflate the layout'? Could I do this with the custom Adapter I already have? – ethanzh Mar 26 '16 at 15:51
  • i think what you're looking for is something like http://stackoverflow.com/questions/12729830/expand-listview-item-with-animation – Sergio Lima Mar 26 '16 at 15:51
  • Sergio, in that example, he already has a working expand/unexpand, he is simply asking how to do the animation. I still need to satisfy the other requirements (not going back to normal when scrolled away from, etc). Thank you though – ethanzh Mar 26 '16 at 15:54
  • Yeah you can use the custom adapter you have(i didn't see that earlier). You will need to save the states of the rows in separate boolean array. Using the state of this boolean array expand the corresponding layout. – K Neeraj Lal Mar 26 '16 at 15:57
  • Could you please explain that in a bit simpler terms. Where would the boolean array be stored? Would each value correspond to whether each cell is 'expanded' or not? – ethanzh Mar 26 '16 at 15:59
  • Create the boolean array inside your `onCreateView`. Yeah each variable correspond to whether each cell is 'expanded' or not. – K Neeraj Lal Mar 26 '16 at 16:02
  • Okay I have created it. Now how/where do I use the state of the boolean array to determine which layout to show? – ethanzh Mar 26 '16 at 16:04
  • Have you got the solution for this? Because I am facing same problem at a time many rows are expanded and it goes out to normal position if it is out of view. – Tara Oct 06 '16 at 18:05

0 Answers0