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

自闭症网瘾萝莉.ら 提交于 2019-11-28 14:46:44

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

Below code helps you achieve a requirement for having a view flipper in list view with invidual row flipping
## xml file ##

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/regularLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <ViewFlipper
        android:id="@+id/row_item_flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/list_item"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Regular Layout"
                android:textSize="28sp" />

            <Button
                android:text="Flip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/front_button" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/backTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Flip row done"
                android:textSize="18sp" />

            <Button
                android:text="Flip back"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/back_button" />
        </LinearLayout>
    </ViewFlipper>
</LinearLayout>


----------
## Java code ##


    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, int position) {

        final CustomItem data = dataList.get(position);
            holder.regularLayout.setVisibility(View.VISIBLE);
            holder.swipeLayout.setVisibility(View.GONE);
            holder.listItem.setText(data.dataItemValue );
            holder.backTextView.setText(data.dataItemValue + " position : "+ position);
            holder.viewFlipper.setFlipInterval(0);
            holder.viewFlipper.setInAnimation(null);
            holder.viewFlipper.setOutAnimation(null);
            if(holder.viewFlipper.getDisplayedChild()==0 && data.isFlipON){
                holder.viewFlipper.setDisplayedChild(1);
            } else if(holder.viewFlipper.getDisplayedChild()==1 && !data.isFlipON){
                holder.viewFlipper.setDisplayedChild(0);
            }else if(data.isFlipON){
                holder.viewFlipper.setDisplayedChild(1);
            }else{
                holder.viewFlipper.setDisplayedChild(0);
            }



        holder.frontButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AnimationFactory.flipTransition(holder.viewFlipper, AnimationFactory.FlipDirection.LEFT_RIGHT);
                data.isFlipON = true;
            }
        });
        holder.backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.isFlipON = false;

                AnimationFactory.flipTransition(holder.viewFlipper, AnimationFactory.FlipDirection.LEFT_RIGHT);
            }
        });

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!