Android中ScrollView和ListView中可以嵌套一些其他的UI视图,比如GridView和LinearLayout等。
如果需要嵌套纵向滑动的View时,逻辑会稍微复杂些,需要协调滑动手势,嵌套横向滑动的View时,没有冲突,所以要简单一些,以下实例是分别使用GridView和LinearLayout实现了在ScrollView的滑动视图。
使用GridView的优点是可以复用
public class MainActivity extends AppCompatActivity {
private GridView mGridView;
private double width;
private double width2;
private MyAdapter myAdapter;
private LinearLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
getScreenDen();
mGridView = (GridView) findViewById(R.id.grid_view);
myAdapter = new MyAdapter();
mGridView.setAdapter(myAdapter);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) width*myAdapter.getCount(),LinearLayout.LayoutParams.WRAP_CONTENT);
mGridView.setLayoutParams(params);
mGridView.setNumColumns(myAdapter.getCount());
mLayout = (LinearLayout) findViewById(R.id.layout);
for (int i = 0; i < 10 ; i++) {
buildItem(mLayout);
}
}
private void buildItem(ViewGroup rootView) {
LinearLayout layout = new LinearLayout(this);
LayoutInflater.from(this).inflate(R.layout.layout_item,layout);
LayoutParams params = new LayoutParams((int)width2 ,LayoutParams.MATCH_PARENT);
rootView.addView(layout,params);
}
private void getScreenDen() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.density * 100;
width2 = dm.density * 100;
}
class MyAdapter extends BaseAdapter {
private List<String> mData;
private MyAdapter() {
super();
init();
}
private void init() {
mData = new ArrayList<String>();
mData.add("白日依山尽");
mData.add("白日依山尽");
mData.add("白日依山尽");
mData.add("白日依山尽");
mData.add("白日依山尽");
mData.add("白日依山尽");
mData.add("白日依山尽");
mData.add("白日依山尽");
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int i) {
return mData.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.grid_view_item,null);
}
TextView textView = (TextView) view.findViewById(R.id.ridtext);
textView.setText((String)getItem(i));
return view;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.***.***.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridView
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:horizontalSpacing="10dp">
</GridView>
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
网友评论