Search code examples
androidlistviewviewflipper

Android + Use ViewFlipper to flip individual items within a ListView?


This is probably a stupid question.

I know I can wrap a ListView in ViewFlipper, but can we wrap individual ListView Items in a ViewFlipper? For instance, the latest Twitter app uses a ListView to display Tweets. It also allows you to set settings on individual items by sliding the tweet out of the way exposing the setting option icons below. Is this a custom implementation or do we have the ability to create something similar using ListView and ViewFlipper? Thanks, any advice is appreciated!


I've spent some time on this and got the ListView to display additional content via the ViewFlipper. However, the view only seems to "flip" on the last item in the ListView. How do I get each item to flip when clicked? Here's some code:

row.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/toptext" android:layout_width="fill_parent"
                android:layout_height="0dip" android:layout_weight="1"
                android:gravity="center_vertical" />
    <TextView android:id="@+id/bottomtext" android:layout_width="fill_parent"
                android:layout_height="0dip" android:layout_weight="1"
                android:gravity="center_vertical" />
</ViewFlipper>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView android:id="@id/android:list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

ListActivityView.java - extends ListActivity

    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    ...
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // doesn't matter which line is clicked
        // only the last item in the ListView displays the bottomText
        viewFlipper.showNext(); 
    }

ListingAdapter.java - extends ArrayAdapter

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

        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);

            viewFlipper = (ViewFlipper) v.findViewById(R.id.flipper);
        }

Solution

  • If I am getting your question correct - you can use ViewFlipper inside a layout that defines a row in your list and init it the way you like when rendering corresponding view