<span style="font-family: Arial, Helvetica, sans-serif;">因为公司项目需求,学习了股票列表的实现,最后主要参考了https://github.com/zimoguo/LeftRightScroll实现了需要的功能,自己因此写下Demo~</span>
整个Demo是为了实现listview和scrollview的嵌套使用,这里使用到了Scrollview嵌套HorizonalScrollview再嵌套ListView,从而实现listview的横向和竖向的滑动。
关键在于两点:
<span style="white-space:pre"> </span>1: scrollview里面嵌套listview,需要对listview的孩子进行高度测量,这里通过<pre name="code" class="java"><span style="white-space:pre"> </span> UtilTools.setListViewHeightBasedOnChildren(lv_demo1);实现。
<span style="white-space:pre"> </span>2:多重嵌套,主要通过布局实现,自定义布局中要设置控件滚动监听,得到滚动的距离,然后让传进来的view也设置相同的滚动距离。
</pre><pre>
</pre><pre name="code" class="java">public class MainActivity extends Activity {
private ListView lv_demo1;
private String[] data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_demo1 = (ListView) findViewById(R.id.lv_demo1);
data = getData();
ListScrollAdapter adapter = new ListScrollAdapter();
lv_demo1.setAdapter(adapter);
UtilTools.setListViewHeightBasedOnChildren(lv_demo1);//测量listview里面的item的高度,以设置给scrollview,完成向scrollview里面填充listview
}
private String[] getData() {
String da = "11111111111111111222222222222222222233333333333333333333";
String[] data = new String[15];
for (int i = 0; i < 15; i++) {
data[i] = da;
}
return data;
}
class ListScrollAdapter extends BaseAdapter{
int i = data.length;
@Override
public int getCount() {
return i;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {//没有优化
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.lsiscrollview, null);
TextView tv_content = (TextView) view.findViewById(R.id.textView1);
tv_content.setText(data[position]);
return view;
}
}
}
public class MyScrollView extends HorizontalScrollView {
public MyScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
private View mView;
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//设置控件滚动监听,得到滚动的距离,然后让传进来的view也设置相同的滚动具体
if(mView!=null) {
mView.scrollTo(l, t);
}
}
/**
* 设置跟它联动的view
* @param view
*/
public void setScrollView(View view) {
mView = view;
}
}
工具类:
public class UtilTools {
/*
* as
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0,0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
最后是布局文件:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 右侧内容的父容器 实现水平滚动 -->
<com.example.listscrollviewdemo.MyScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="@null" >
<LinearLayout
android:id="@+id/right_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ListView
android:id="@+id/lv_demo1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</com.example.listscrollviewdemo.MyScrollView>
</LinearLayout>
</ScrollView>
<com.example.listscrollviewdemo.MyScrollView
android:id="@+id/scrollview_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</com.example.listscrollviewdemo.MyScrollView>