知识点记录

    xiaoxiao2021-12-12  4

    知识总结

    1.在很多时候都会遇到NestedScrollview和Scrollview嵌套listview的情况,而导致listview中的item不能完全显示,其实只需在xml文件中加入android:fillViewport="true"这个属性就可以使listview完全显示(发现问题,添加以后虽然可以全屏显示但是scrollview却无法滚动,还没有找到解决办法)。

    2.很多时候也会用到listview嵌套listview,gridview等,但是问题在于嵌套之后只能显示一个item。这时候就需要对被嵌套的listview或者是gridview进行动态测量。如下为listview嵌套gridview(3列)时,listview嵌套listview时大体一致,需稍加改动。

    GridAdapter adapter = new GridAdapter(context, historycity_lsit); gridView.setAdapter(adapter); int totalHeight = 0; for(int i=0;i<adapter.getCount()%3;i++) { View viewItem = adapter.getView(i, null, gridView);//这个很重要,那个展开的item的measureHeight比其他的大 viewItem.measure(0, 0); totalHeight += viewItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = totalHeight + (gridView.getColumnWidth() * (gridView.getCount() - 1)); gridView.setLayoutParams(params); adapter.notifyDataSetChanged(); 3.fragement切换方式: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initView(); fragments = new Fragment[] { new LiveSquareFragment(), ConversationListFragment.newInstance(null, true), new MyProfileFragment()}; // 添加显示第一个fragment getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragments[0]) .commit(); } /** * 初始化组件 */ private void initView() { mTabs = new Button[3]; mTabs[0] = (Button) findViewById(R.id.btn_square); mTabs[1] = (Button) findViewById(R.id.btn_conversation_list); mTabs[2] = (Button) findViewById(R.id.btn_setting); // 把第一个tab设为选中状态 mTabs[0].setSelected(true); } /** * button点击事件 * * @param view */ public void onTabClicked(View view) { switch (view.getId()) { case R.id.btn_square: index = 0; break; case R.id.btn_conversation_list: index = 1; break; case R.id.btn_setting: index = 2; break; case R.id.btn_publish: startActivity(new Intent(this, StartLiveActivity.class)); break; } if (currentTabIndex != index) { FragmentTransaction trx = getSupportFragmentManager().beginTransaction(); trx.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) { trx.add(R.id.fragment_container, fragments[index]); } trx.show(fragments[index]).commit(); } mTabs[currentTabIndex].setSelected(false); // 把当前tab设为选中状态 mTabs[index].setSelected(true); currentTabIndex = index; } 3.Dialog的创建,并规定显示位置 AlertDialog.Builder builder = new AlertDialog.Builder(this); View v = LayoutInflater.from(this).inflate(R.layout.photoselect_dialog,null); camera = (TextView) v.findViewById(R.id.camera); bucketpic = (TextView) v.findViewById(R.id.select_bucket); cancel = (TextView) v.findViewById(R.id.cancel); camera.setOnClickListener(this); bucketpic.setOnClickListener(this); cancel.setOnClickListener(this); dialog = builder.create(); dialog.show(); dialog.setContentView(v);// 一定要放在dialog.show方法后面 Window dialogwindow = dialog.getWindow(); WindowManager.LayoutParams params = dialogwindow.getAttributes(); WindowManager m = this.getWindowManager(); Display display = m.getDefaultDisplay(); int widith = (int) (display.getWidth()); int hight = (int) (LayoutParams.WRAP_CONTENT); params.width = widith; params.height = hight; dialogwindow.setAttributes(params); dialogwindow.setGravity(Gravity.BOTTOM); dialog.getWindow().setAttributes(params); 4.同一个Activity中的Fragment在点击按钮时进行切换: FragmentManager fm = getActivity().getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); MainActivity.currentTabIndex=2; ft.hide(MainActivity.fragements[0]).show(MainActivity.fragements[2]).commit(); MainActivity.btns[2].setSelected(true); MainActivity.btns[0].setSelected(false);

    5.PopWindow的创建

    PopupWindow popupWindow = new PopupWindow(context); popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); View popview=LayoutInflater.from(context).inflate(R.layout.item_projecttype,null); popupWindow.setContentView(popview); //点击popupWindow以外的区域自动关闭popupWindow popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); popupWindow.showAsDropDown(projectInfoText, 0, 0);//设置popwindow的弹出方式为向下弹出

    转载请注明原文地址: https://ju.6miu.com/read-900226.html

    最新回复(0)