效果图如下:
代码如下:
MainActivity:
public class MainActivity extends Activity { private Button btn_fixed; private Button btn_scale; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); btn_fixed = (Button) findViewById(R.id.btn_one); btn_scale = (Button) findViewById(R.id.btn_two); btn_fixed.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent fixIntent = new Intent(MainActivity.this,FixedActivity.class); startActivity(fixIntent); } }); btn_scale.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent fixIntent = new Intent(MainActivity.this,ScaleActivity.class); startActivity(fixIntent); } }); } }
FixedActivity代码:public class FixedActivity extends Activity implements OnScrollListener{ private ListView lv_test; private LinearLayout ll_tt; private TextView tv_shadow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_lv); lv_test = (ListView) findViewById(R.id.lv_test); tv_shadow = (TextView) findViewById(R.id.tv_shadow); View view = LayoutInflater.from(this).inflate(R.layout.lv_test, null); View view2 = LayoutInflater.from(this).inflate(R.layout.item, null); ll_tt = (LinearLayout) view2.findViewById(R.id.ll_tt); lv_test.addHeaderView(view); lv_test.addHeaderView(view2); lv_test.setOnScrollListener(this); lv_test.setAdapter(new MyAdapter()); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 40; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(FixedActivity.this); tv.setText("listview"+position); return tv; } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @SuppressLint("NewApi") @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int top2 = ll_tt.getTop(); if (top2 <= 0 || firstVisibleItem >= 1 ) { tv_shadow.setVisibility(View.VISIBLE); }else { tv_shadow.setVisibility(View.INVISIBLE); } } }ScaleActivity代码如下:public class ScaleActivity extends Activity implements OnScrollListener{ private ListView lv_test; private LinearLayout ll_tt; private TextView tv; private int top; private float scale = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_lv); lv_test = (ListView) findViewById(R.id.lv_test); View view = LayoutInflater.from(this).inflate(R.layout.lv_test, null); View view2 = LayoutInflater.from(this).inflate(R.layout.item, null); ll_tt = (LinearLayout) view2.findViewById(R.id.ll_tt); tv = (TextView) view2.findViewById(R.id.tv_tt); lv_test.addHeaderView(view); lv_test.addHeaderView(view2); lv_test.setOnScrollListener(this); lv_test.setAdapter(new MyAdapter()); } @Override protected void onResume() { super.onResume(); ViewTreeObserver vto = ll_tt.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { ll_tt.getViewTreeObserver().removeOnGlobalLayoutListener(this); } top = ll_tt.getTop(); scale = (float) scale / (float) top; System.out.println("scale : " + scale); } }); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 40; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(ScaleActivity.this); tv.setText("listview"+position); return tv; } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int top2 = ll_tt.getTop(); if (top2 >= 0) { tv.setScaleX(top2 * scale); tv.setScaleY(top2 * scale); } } }item代码如下:activity_main代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="title固定" /> <Button android:id="@+id/btn_two" android:layout_below="@id/btn_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="title缩放" /> </RelativeLayout>activity_lv代码如下:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#ffffffff" tools:context=".MainActivity" > <ListView android:id="@+id/lv_test" android:background="#ffffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="@null" android:dividerHeight="0dp" android:scrollbars="none" /> <TextView android:id="@+id/tv_shadow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30sp" android:text="这是测试条目" android:background="#ffffffff" android:visibility="invisible" android:gravity="center"/> </RelativeLayout>lv_text代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffffff" android:id="@+id/ll" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="200dp" android:background="@mipmap/widget_autoscroollview_demo_banner1" /> </LinearLayout><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll_tt" android:orientation="vertical" > <TextView android:id="@+id/tv_tt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30sp" android:text="这是测试条目" android:visibility="visible" android:gravity="center"/> </LinearLayout>