将margin设置为listview项,不影响整个listview的边距

时间:2016-03-27 18:09:43

标签: android listview adapter margin swipe

我想设置特定列表视图项的左边距,但是当我尝试更改它时,列表视图的整个边距会发生变化。我想通过改变特定视图的边距来实现向左滑动和删除视图(让我们说第一个,参见img1)。您可以在下面找到一些代码...

/*custom adapter*/

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

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) myContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = infalInflater.inflate(R.layout.summary_list_item1, null);
        convertView.setTag(position);

        TextView tt1 = (TextView) convertView.findViewById(R.id.listText);

        tt1.setText(myItems.get(position));
        }

        return convertView;
}

 /*onTouchListener*/       
 lv.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Log.i("onTouch", "Down");
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    historicX = event.getX();
                    historicY = event.getY();
                    /*keep view first time that is touched*/
                    if (removeView == null) {
                        removeView = v;
                    }
                    break;

                case MotionEvent.ACTION_MOVE:
                    performSlideViewEffect(v, historicX, historicY, event.getX(), event.getY());

                    historicX = event.getX();
                    historicY = event.getY();
                    break;
            }
  }

 public void performSlideViewEffect(View v,float historicX,float historicy,float x,float y) {

    TextView tt1 = (TextView) v.findViewById(R.id.listText);

    LinearLayout.MarginLayoutParams lp = (LinearLayout.MarginLayoutParams) v.getLayoutParams();

    lp.setMarginStart(lp.leftMargin - Math.round(historicX - x));
    v.setLayoutParams(lp);
    v.invalidate();
}

/*Layout with the ListView*/

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#f4f4f4"
android:scrollbars="vertical">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="50sp"
    android:layout_gravity="center_horizontal"
    android:id="@+id/table"
    android:padding="10sp"
    android:textSize="20sp"
    android:text=""
    android:textStyle="bold|italic"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="3sp"
    android:background="#EFEFEF"
    android:layout_margin="2sp" />

<ListView
    android:id="@+id/summary_list"
    android:layout_height="wrap_content"
    android:layout_width="280sp"
    android:background="#E7E7E7"
    android:layout_marginTop="5sp"
    android:layout_gravity="center_horizontal"
    android:scrollbars="vertical">
</ListView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/submit_order"
    android:text="@string/SubmitOrder"
    android:layout_gravity="center_horizontal"/>

/*summary_list_item1.xml*/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/listText"
    android:tag=""
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="#ffffff"
    android:layout_marginTop="2sp"
    android:layout_marginBottom="1sp"
    android:layout_marginLeft="3sp"
    android:layout_marginRight="3sp"
    android:padding="15sp"
    android:background="#000000"
    android:layout_gravity="center_vertical|center_horizontal" />
</LinearLayout>

在我选择移动第一个布局之前,初始布局如下 img1

但我得到的是所有要移动的观点...不仅仅是触及&#34;一... img2

2 个答案:

答案 0 :(得分:1)

基本上你想要实现的是轻扫以解除效果。

您最好的选择是将ListView更改为RecyclerView。 阅读答案Swipe to Dismiss for RecyclerView如何实现这样的效果:

答案 1 :(得分:0)

有一点猜测:我认为致电(LinearLayout.MarginLayoutParams) v.getLayoutParams();会返回 所有 项目的常见布局参数。您可能需要为“在行动中”项目创建单个变量