主activity页面;
package com.example.scrollviewlistview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import com.example.adapter.MybaseAdatpter;
import com.example.myview.Mylistview;
public class MainActivity extends Activity implements OnScrollListener {
private List<String> list;
private ScrollView myscollview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myscollview = (ScrollView) findViewById(R.id.sco);
Mylistview listView=(Mylistview) findViewById(R.id.listview);
initdata();
listView.setAdapter(new MybaseAdatpter(list, getApplicationContext()));
listView.setBackgroundColor(Color.RED);
//设置listview的滚动监听;这个主要监听的是listview滑动到最后一条时,scrollview就能滚动了
listView.setOnScrollListener(this);
//设置listveiw的触屏事件;
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//点击listview里面滚动停止时,scrollview拦截listview的触屏事件,就是scrollview该滚动了
myscollview.requestDisallowInterceptTouchEvent(false);
} else {
//当listview在滚动时,不拦截listview的滚动事件;就是listview可以滚动,
myscollview.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
}
private void initdata() {
list = new ArrayList<String>();
for (int i = 0; i <30; i++) {
list.add("data"+i);
}
}
//滚动监听,需实现的两个方法;
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if((firstVisibleItem+visibleItemCount)==totalItemCount){
//如果listview部分加载到最后一条了 ,拦截listview的触屏事件,意思就是scrollview可以滚动了;
myscollview.requestDisallowInterceptTouchEvent(false);
}
}
}
自定义的listview类;
package com.example.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class Mylistview extends ListView{
public Mylistview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Mylistview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Mylistview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
//解决listview高度问题;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int me=MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, me);
}
}
适配器类;
package com.example.adapter;
import java.util.List;
import com.example.scrollviewlistview.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MybaseAdatpter extends BaseAdapter{
List<String> list;
Context context;
public MybaseAdatpter(List<String> list, Context context) {
super();
this.list = list;
this.context = context;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView=View.inflate(context, R.layout.activity_main_item, null);
TextView textView=(TextView) convertView.findViewById(R.id.te);
textView.setText(list.get(position));
return convertView;
}
}
main.xml;
<ScrollView 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"
tools:context=".MainActivity"
android:id="@+id/sco"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.example.myview.Mylistview
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.myview.Mylistview>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdf"
/>
</LinearLayout>
</ScrollView>
main-item.xml;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/te"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
|