使用padding值控制控件的隐藏与显示

  在学自定义控件下拉刷新这一案例,控制ListView头条目和尾条目的显示隐藏时,就是设置其padding值的正负控制其的显示与隐藏。这并不是什么很大的知识点。只是一个小技巧,这里给大家分享一下。

这一使用,是否有限制:没有限制,针对View都是适用的。

在我的案例中,我分别使用了TextView、ImageView和LinearLayout作为显示隐藏的对象,成功控制其显示与隐藏。案例中使用paddingTop控制向上的隐藏与显示,当然你也可以使用另外三个,控制不同方向的显示与隐藏。

简单说一下View消失的原因:在初始显示的View,其四个边就是设置padding的参考线,设置值,会在参考线的基础上移动。例如paddingTop为正值时,参考上边,向下移动。为负值时,参考上边,向上移动。当移动过程中,也到其他的VIew就会被盖住。并没有消失。只是看不见了。

这是我的代码:

MainActivity:

  1 package com.aimqq.showhide;
  2 
  3 import android.os.AsyncTask;
  4 import android.os.Bundle;
  5 import android.os.Handler;
  6 import android.os.SystemClock;
  7 import android.app.Activity;
  8 import android.view.MotionEvent;
  9 import android.view.View;
 10 import android.view.View.OnTouchListener;
 11 import android.widget.ImageView;
 12 import android.widget.LinearLayout;
 13 import android.widget.RelativeLayout;
 14 import android.widget.TextView;
 15 
 16 public class MainActivity extends Activity {
 17 
 18     private static final Integer HIDE = 1001;
 19     private static final Integer SHOW = 1002;
 20     private TextView content;
 21     private int height;
 22     boolean isStop;
 23     private MyAsyncTask task;
 24     private ImageView ic;
 25     private int height2;
 26     private LinearLayout lay;
 27     private int height3;
 28 
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_main);
 33         content = (TextView) findViewById(R.id.tv_content);
 34         ic = (ImageView) findViewById(R.id.iv_ic);
 35         lay = (LinearLayout) findViewById(R.id.ll_lay);
 36 //        content.measure(0, 0);
 37 //        height = content.getMeasuredHeight();
 38 //        ic.measure(0, 0);
 39 //        height2 = ic.getMeasuredHeight();
 40         lay.measure(0, 0);
 41         height3 = lay.getMeasuredHeight();
 42     }
 43 
 44     public void show(View v) {
 45         stopPreTask();
 46         task = new MyAsyncTask();
 47         isStop = false;
 48         task.execute(-height3, 0, SHOW);
 49     }
 50 
 51     public void hide(View v) {
 52         stopPreTask();
 53         isStop = false;
 54         task = new MyAsyncTask();
 55         task.execute(0, height3, HIDE);
 56     }
 57 
 58     private void stopPreTask() {
 59         if (task != null) {
 60             isStop = true;
 61         }
 62         task = null;
 63     }
 64 
 65     public class MyAsyncTask extends AsyncTask<Integer, Integer, Void> {
 66 
 67         @Override
 68         protected void onPreExecute() {
 69             
 70         }
 71 
 72         @Override
 73         protected Void doInBackground(Integer... params) {
 74             if (params[2] == HIDE) {
 75                 for (int i = params[0]; i <= params[1]; i++) {
 76                     if (isStop) {
 77                         break;
 78                     }
 79                     publishProgress(-i);
 80                     SystemClock.sleep(10);
 81                 }
 82             } else if (params[2] == SHOW) {
 83                 for (int i = params[0]; i <= params[1]; i++) {
 84                     if (isStop) {
 85                         break;
 86                     }
 87                     publishProgress(i);
 88                     SystemClock.sleep(10);
 89                 }
 90             }
 91             return null;
 92         }
 93 
 94         @Override
 95         protected void onPostExecute(Void result) {
 96             isStop = false;
 97         }
 98 
 99         @Override
100         protected void onProgressUpdate(Integer... values) {
101             if (isStop) {
102                 return;
103             }
104             lay.setPadding(0, values[0], 0, 0);
105             lay.invalidate();
106         }
107 
108     }
109 }

 

布局代码:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <LinearLayout
 7         android:id="@+id/ll_lay"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:layout_marginTop="45dp"
11         android:orientation="vertical" >
12 
13         <TextView
14             android:id="@+id/tv_content"
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_gravity="center_horizontal"
18             android:text="看看我"
19             android:textColor="#ff0000"
20             android:textSize="25sp" />
21 
22         <ImageView
23             android:id="@+id/iv_ic"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_gravity="center_horizontal"
27             android:src="@drawable/ic_launcher" />
28     </LinearLayout>
29 
30     <LinearLayout
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:layout_centerInParent="true"
34         android:orientation="horizontal" >
35 
36         <Button
37             android:layout_width="0dp"
38             android:layout_height="wrap_content"
39             android:layout_weight="1"
40             android:onClick="show"
41             android:text="显示" />
42 
43         <Button
44             android:layout_width="0dp"
45             android:layout_height="wrap_content"
46             android:layout_weight="1"
47             android:onClick="hide"
48             android:text="隐藏" />
49     </LinearLayout>
50 
51 </RelativeLayout>

效果图:

    

 

转载于:https://www.cnblogs.com/aimqqroad-13/p/5906851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值