Android UI实践 ScrollView嵌套横向滑动UI (GridView及LinearLayout)

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 mData;
        private MyAdapter() {
            super();
            init();
        }

        private void init() {
            mData = new ArrayList();
            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;
        }
    }
}



    
        
            
                
                
                
            
        
    

你可能感兴趣的:(Android UI实践 ScrollView嵌套横向滑动UI (GridView及LinearLayout))