在listView中滑动会使链接缩小

时间:2017-03-25 18:32:28

标签: android android-layout listview

我按照指南添加滑动到我的listView。 基本上每行都有一个手势监听器,当滑动后面布局的可见性设置为可见并且按钮出现时,前面的布局消失

不幸的是,每当我滑动时,行的尺寸都会缩小到我想要在滑动后显示的按钮的尺寸。

after swiping the row shrinks

for better comprehension, with visibility of both backgrounds

(抱歉还不能发布图片)

显示按钮时如何保持相同的高度? (注意行和高度可能会有所不同)

list_item_swipeable.xml(每行)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/layout_front"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/list_item_link"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/list_item_origin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#5d5d5d"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/list_item_created_at"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="@color/colorAccent" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/layout_back"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="right"
        android:visibility="gone"
        >
        <ImageButton
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/delete"
            android:background="@color/btn_delete_bg"
            android:layout_alignParentRight="true"
            />
        <ImageButton
            android:id="@+id/btnEdit"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/edit"
            android:background="@color/btn_edit_bg"
            android:layout_toLeftOf="@id/btnDelete"
            />
    </RelativeLayout>
</RelativeLayout>

MyGestureListener

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int MIN_DISTANCE = 50;
    private static final String TAG = "MyGestureListener";
    private RelativeLayout backLayout;
    private LinearLayout frontLayout;
    private Animation inFromRight,outToRight,outToLeft,inFromLeft;
    private ViewGroup.LayoutParams frontParams;
    private ViewGroup.LayoutParams backParams;

    public MyGestureListener(Context ctx, View convertView) {
        backLayout = (RelativeLayout) convertView.findViewById(R.id.layout_back);
        frontLayout = (LinearLayout) convertView.findViewById(R.id.layout_front);
        inFromRight = AnimationUtils.loadAnimation(ctx, R.anim.in_from_right);
        outToRight = AnimationUtils.loadAnimation(ctx, R.anim.out_to_right);
        outToLeft = AnimationUtils.loadAnimation(ctx, R.anim.out_to_left);
        inFromLeft = AnimationUtils.loadAnimation(ctx, R.anim.in_from_left);


    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        float diffX = e2.getX() - e1.getX();
        float diffY = e2.getY() - e1.getY();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > MIN_DISTANCE) {
                if(diffX<0){
                    Log.v(TAG, "Swipe Right to Left");
                    if(backLayout.getVisibility()==View.GONE){
                        frontParams = frontLayout.getLayoutParams();

                        backParams = backLayout.getLayoutParams();
                        Log.d("HEIGHT FRONT ", ": "+frontParams.height ); // why -1?
                        Log.d("HEIGHT BACK ", ": "+backParams.height ); // why -1?

                        frontLayout.startAnimation(outToLeft);
                        backLayout.setVisibility(View.VISIBLE);
                        backLayout.startAnimation(inFromRight);
                        frontLayout.setVisibility(View.GONE);
                    }
                }else{
                    Log.v(TAG, "Swipe Left to Right");
                    if(backLayout.getVisibility()!=View.GONE){
                        backLayout.startAnimation(outToRight);
                        backLayout.setVisibility(View.GONE);
                        frontLayout.setVisibility(View.VISIBLE);
                        frontLayout.startAnimation(inFromLeft);
                    }
                }
            }
        }
        return true;
    } 
}

我的自定义listAdapter中的getView方法

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

        final ViewHolder holder;

        if(convertView == null){
            convertView = inflator.inflate(R.layout.list_item_swipeable, null);
            holder = new ViewHolder();

//            holder.container = (LinearLayout) convertView.findViewById(R.id.container);
            holder.container = (RelativeLayout) convertView.findViewById(R.id.container);

            holder.created_at = (TextView) convertView.findViewById(R.id.list_item_created_at);
            holder.link = (TextView) convertView.findViewById(R.id.list_item_link);
            holder.origin = (TextView) convertView.findViewById(R.id.list_item_origin);

            holder.mDetector = new GestureDetectorCompat(mContext, new MyGestureListener (mContext, convertView));

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

        holder.created_at.setText( mData.get(position).getCreated_at());
        holder.link.setText( mData.get(position).getLink());
        holder.origin.setText( mData.get(position).getOrigin());



        holder.container.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                holder.mDetector.onTouchEvent(event);
                return true;
            }
        });

        return convertView;



    }

0 个答案:

没有答案