为了快速实现,请看图,看代码,关键点在实现ScrollView 的onScrollChanged() 方法
import android.os.Bundle
;
import android.support.v7.app.AppCompatActivity
;
import android.view.View
;
import android.widget.LinearLayout
;
import android.widget.RelativeLayout
;
import android.widget.ScrollView
;
import java.util.ArrayList
;
import java.util.List
;
/**
* @author lxs
* @date :2017/4/13
* @Description:
*/
public class MainActivity
extends AppCompatActivity {
private List<String>
list =
new ArrayList<String>()
;
private ListviewDataAdapter
listviewDataAdapter;
private PayScrollView
payScrollView;
private ListViewForScrollView
listView;
private RelativeLayout
header_rl;
private LinearLayout
header_il, header_sheliang_ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
;
setContentView(R.layout.
activity_main)
;
listView = (ListViewForScrollView) findViewById(R.id.
list_v)
;
listView.setFocusable(
false)
;
payScrollView = (PayScrollView) findViewById(R.id.
scroll_v)
;
header_rl = (RelativeLayout) findViewById(R.id.
header_rl)
;
header_il = (LinearLayout) findViewById(R.id.
header_il)
;
header_sheliang_ll = (LinearLayout) findViewById(R.id.
header_sheliang_ll)
;
payScrollView.setOnScrollChangedListeners(
new PayScrollView.OnScrollChangedListener() {
@Override
public void onScrollChanged(ScrollView who
, int l
, int t
, int oldl
, int oldt) {
if (t >
header_sheliang_ll.getHeight() /
2 *
3) {
header_il.setVisibility(View.
VISIBLE)
;
header_rl.setVisibility(View.
GONE)
;
}
else if (t <
header_sheliang_ll.getHeight() /
2) {
header_il.setVisibility(View.
GONE)
;
header_rl.setVisibility(View.
VISIBLE)
;
}
}
})
;
getData(
true)
;
}
private void getData(
boolean falge) {
if (falge) {
for (
int i =
0; i <
50; i++) {
list.add(
"张三" + i)
;
}
listviewDataAdapter =
new ListviewDataAdapter(
list, this)
;
listView.setAdapter(
listviewDataAdapter)
;
}
}
}
import android.content.Context
;
import android.util.AttributeSet
;
import android.widget.ScrollView
;
/**
* @author lxs
* @date 创建时间 : 2017/4/10
* @Description:
*/
public class PayScrollView
extends ScrollView{
public interface OnScrollChangedListener {
void onScrollChanged(ScrollView who
, int l
, int t
, int oldl
, int oldt)
;
}
private OnScrollChangedListener
mOnScrollChangedListener;
public PayScrollView(Context context) {
super(context)
;
}
public PayScrollView(Context context
, AttributeSet attrs) {
super(context
, attrs)
;
}
public PayScrollView(Context context
, AttributeSet attrs
, int defStyle) {
super(context
, attrs
, defStyle)
;
}
@Override
protected void onScrollChanged(
int l
, int t
, int oldl
, int oldt) {
super.onScrollChanged(l
, t
, oldl
, oldt)
;
if (
mOnScrollChangedListener !=
null) {
mOnScrollChangedListener.onScrollChanged(
this, l
, t
, oldl
, oldt)
;
}
}
public void setOnScrollChangedListeners(OnScrollChangedListener listener) {
mOnScrollChangedListener = listener
;
}
}
下面是实现listview 防止冲突
import android.content.Context
;
import android.util.AttributeSet
;
import android.widget.ListView
;
/**
* @author lxs
* @date 创建时间 : 2017/4/10
* @Description:
*/
public class ListViewForScrollView
extends ListView {
public ListViewForScrollView(Context context
, AttributeSet attrs
,
int defStyle) {
super(context
, attrs
, defStyle)
;
// TODO Auto-generated constructor stub
}
public ListViewForScrollView(Context context
, AttributeSet attrs) {
super(context
, attrs)
;
// TODO Auto-generated constructor stub
}
public ListViewForScrollView(Context context) {
super(context)
;
// TODO Auto-generated constructor stub
}
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
@Override
protected void onMeasure(
int widthMeasureSpec
, int heightMeasureSpec) {
int expandSpec = MeasureSpec.
makeMeasureSpec(Integer.
MAX_VALUE >>
2,
MeasureSpec.
AT_MOST)
;
super.onMeasure(widthMeasureSpec
, expandSpec)
;
}
}效果如下图
转载请注明原文地址: https://ju.6miu.com/read-665231.html