3

我正在尝试实现,在自定义列表视图中向右滑动显示按钮。我现在面临的问题是,我实现了我在网上找到的 onSwipeTouchListener。我可以看到滑动按钮。但它并不一致,我的意思是如果我在一行上向右滑动,按钮将显示在另一行。这是我尝试过的。

我创建了一个用于保存视图的适配器

//TransactionAddDropViewHolder


        public static final class TransactionAddDropViewHolder {

    public View moveUpButton = null;
    public View moveDownButton = null;
    public View withdrawButton = null;
    public View reviewButton = null;

    public View approveButton = null;
    public View rejectButton = null;

    public LinearLayout addContainer = null;
    public LinearLayout dropContainer = null;



    public void swipeButtons() {
        addDropListView.setOnTouchListener(new OnSwipeTouchListener() {

            public void onSwipeRight() {
                withdrawButton.setVisibility(View.VISIBLE);
            }

            public void onSwipeLeft() {
                withdrawButton.setVisibility(View.INVISIBLE);
            }
        });
    }

    public void showUserButtons() {
        this.moveUpButton.setVisibility(View.VISIBLE);
        this.moveDownButton.setVisibility(View.VISIBLE);
        // this.withdrawButton.setVisibility(View.VISIBLE);
    }

    public void hideUserButtons() {
        this.moveUpButton.setVisibility(View.GONE);
        this.moveDownButton.setVisibility(View.GONE);
        // this.withdrawButton.setVisibility(View.GONE);
    } 

}

这是我显示 onSwipeButton 的自定义视图

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

        final TransactionAddDrop addDropData = this.addDropList.get(position);

        TransactionAddDropViewHolder holder = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_pending_transaction_list_item, null);
            holder = new TransactionAddDropViewHolder();

holder.withdrawButton = convertView.findViewById(R.id.pendingTransactionItem_withdrawButton);
            holder.addContainer = (LinearLayout) convertView.findViewById(R.id.pendingTransactionItem_addContainer);
            **holder.swipeButtons();** 
            convertView.setTag(holder);
        } else {
            holder = (TransactionAddDropViewHolder) convertView.getTag();
            holder.swipeButtons(); 
        }

这是我的 OnSwipeTouchListener 类

    private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

添加 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.cbssports.nflapp.ffb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipeRight"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/claimlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/claim"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp" >

        <com.cbssports.nflapp.ffb.CustomText
            android:id="@+id/claimNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="1"
            android:textColor="#B4B4B5"
            android:textSize="36dp"
            app:typeface="oswald_regular" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/pendingPlayers"
        android:layout_width="200dp"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/tradeDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:orientation="horizontal"
            android:layout_toRightOf="@+id/claimlayout"
            android:paddingTop="5dp" >

            <com.cbssports.nflapp.ffb.CustomText
                android:id="@+id/pendingTransactionItem_teamName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#B4B4B5"
                android:textSize="9dp"
                app:typeface="oswald_regular" />

            <com.cbssports.nflapp.ffb.CustomText
                android:id="@+id/pendingTransactionItem_bidAmount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#797979"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="9dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/adddropView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tradeDate"
            android:layout_toRightOf="@+id/claimlayout"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/addViewContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <com.cbssports.nflapp.ffb.CustomText
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:paddingRight="10dp"
                    android:text="ADD"
                    android:textColor="#797979"
                    android:textSize="15dp"
                    app:typeface="oswald_regular" />

                <LinearLayout
                    android:id="@+id/pendingTransactionItem_addContainer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/adddropViewContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/claimlayout"
                android:orientation="horizontal" >

                <com.cbssports.nflapp.ffb.CustomText
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:paddingRight="10dp"
                    android:text="DROP"
                    android:textColor="#797979"
                    android:textSize="15dp"
                    app:typeface="oswald_regular" />

                <LinearLayout
                    android:id="@+id/pendingTransactionItem_dropContainer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/pendingTransactionItem_userButtonContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_vertical"
        android:layout_toRightOf="@+id/pendingPlayers"
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/pendingTransactionItem_moveUpButton"
                android:layout_width="20dp"
                android:layout_height="15dp"
                android:layout_margin="3dp"
                android:background="@drawable/gfx_up_arrow"
                android:padding="1dp"
                android:textColor="#ffffff"
                android:textSize="7sp"
                android:visibility="visible" />

            <Button
                android:id="@+id/pendingTransactionItem_moveDownButton"
                android:layout_width="20dp"
                android:layout_height="15dp"
                android:layout_margin="3dp"
                android:background="@drawable/gfx_down_arrow"
                android:padding="1dp"
                android:textColor="#ffffff"
                android:textSize="7sp"
                android:visibility="visible" />
        </LinearLayout>

        <ImageView
            android:id="@+id/pendingTransactionItem_withdrawButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="1dp"
            android:src="@drawable/ic_cancelclaim"
            android:visibility="gone" />

        <!-- Transaction Review -->

        <ImageView
            android:id="@+id/pendingTransactionItem_reviewButton"
            android:layout_width="38dp"
            android:layout_height="38dp"
            android:padding="1dp"
            android:src="@drawable/btn_review"
            android:visibility="gone" />
    </LinearLayout>
</LinearLayout>

再增加一个问题。获取视图的总计数。但想在不同的布局中显示。这是我的 onCreatedView

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

    fragmentPendingTrades = inflater.inflate(R.layout.fragment_transactions_pending, container, false);
    pendingTradesView = inflater;

    return fragmentPendingTrades;
}

    public void onViewCreated(final View view, final Bundle savedInstanceState) {

    this.addDropListView = (ListView) view.findViewById(R.id.transactions_pending_transactionsListView);
    this.addDropAdapter = new TransactionAddDropAdapter(pendingTradesView);
    this.addDropListView.setAdapter(this.addDropAdapter);
    this.emptyTransationsContainer = view.findViewById(R.id.transactions_pending_transactions_emptyContainer);



    TextView getTotalCount = (TextView) view.findViewById(R.id.transactions_pending_TransactionsAddDropCount);

    getTotalCount.setText(""+addDropListView.getCount());



}
4

1 回答 1

1

因此,正如我所见,这可以通过两种方式完成:

  1. 听整个列表上的滑动,找出滑动发生在哪一行,并显示/隐藏正确行的按钮。
  2. 聆听每一行的滑动,并显示/隐藏该行的按钮。

现在您正在列表中设置侦听器,但尝试应用显示/隐藏适配器中附加的按钮。在某种程度上,你正在做每个解决方案的一半。有了你所拥有的,我建议你选择#2。

在您的TransactionAddDropViewHolder中,您需要View在行布局中获取对根的引用,因此我们可以对其进行设置OnSwipeTouchListener

public static final class TransactionAddDropViewHolder {

    public View rootView = null;

    // Your other code

    public void swipeButtons() {
        rootView.setOnTouchListener(new OnSwipeTouchListener() {

            public void onSwipeRight() {
                withdrawButton.setVisibility(View.VISIBLE);
            }

            public void onSwipeLeft() {
                withdrawButton.setVisibility(View.INVISIBLE);
            }
        });
    }

    // Your other code

}

接下来,在您的getView方法中,更新它,以便您获得对布局TransactionAddDropViewHolder中根的引用。View

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

    final TransactionAddDrop addDropData = this.addDropList.get(position);

    TransactionAddDropViewHolder holder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.fragment_pending_transaction_list_item, null);
        holder = new TransactionAddDropViewHolder();

        // Get a reference to the root of the row layout
        holder.rootView = convertView.findViewById(R.id.swipeRight); 

        holder.withdrawButton = convertView.findViewById(R.id.pendingTransactionItem_withdrawButton);
        holder.addContainer = (LinearLayout) convertView.findViewById(R.id.pendingTransactionItem_addContainer);

        holder.swipeButtons();
        convertView.setTag(holder);
    } else {
        holder = (TransactionAddDropViewHolder) convertView.getTag();
        holder.swipeButtons(); 
    }
}

您还会遇到一个问题,滑动时按钮会正确显示,但如果用户继续滚动,它将出现在其他行上。这是因为ListView回收其行的Views。我建议你做两件事:

  1. 当按钮出现后用户再次开始滚动时,您将再次隐藏它。这就是 iOS 在列表中使用“滑动删除”所做的事情。
  2. boolean例如,如果该行处于按钮可见的模式,则使用 a存储在您的数据对象中isDeleteShowing。然后在你的 onSwipe 回调中,你设置isDeleteShowing为真/假。最后,在您的 中getView,您根据isDeleteShowing该数据行显示/隐藏按钮。

希望这一切都有帮助。

于 2013-04-25T16:55:19.630 回答