How to set size and layout in onSizeChanged?

前端 未结 4 1265
孤城傲影
孤城傲影 2020-12-25 11:39

I can\'t get my elements, for example ListView, to resize properly.

I got a ListActivity for searching, with a dashboard, an editbox and a

相关标签:
4条回答
  • 2020-12-25 12:27

    This could be a long shot...

    Have you tried playing with the android:layout_height="" setting for the ListView?

    Maybe using match_parent or wrap_content could force it to work...

    As I said... a long shot!

    0 讨论(0)
  • 2020-12-25 12:36

    You can force the ListView to redraw by changing its height by 1px and calling

    requestLayout()

    0 讨论(0)
  • 2020-12-25 12:37

    Adding the following at the end of onSizeChanged() will solve it:

    Handler handler = new Handler();
    handler.post(new Runnable() {
    
        @Override
        public void run() {
            requestLayout();
        }
    });
    

    See the answer to this question: Views inside a custom ViewGroup not rendering after a size change

    0 讨论(0)
  • 2020-12-25 12:42

    It's possible to do what you want by registering a setOnFocusChangeListener and a setOnClickListener to the EditText.

    There are a lot of different scenarios to consider when it comes to navigation and things might need to be changed to work with a certain layout.

    Anyway, start by overriding onSizeChanged to show the hidden element when the back button is touched.

    public class MyLinearLayout extends LinearLayout {
    
        private MyListActivity mMyListActivity;
    
        public MyLinearLayout(Context context) {
            super(context);
        }
    
        public MyLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);      
        }
    
        public void setMyListActivity(MyListActivity mla) {
            mMyListActivity = mla;
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            // show the element when we get more room       
            if (h > oldh) {
                if (mMyListActivity != null) {
                    mMyListActivity.showBar();
                }
            }
            super.onSizeChanged(w, h, oldw, oldh);
        }
    }
    

    In the ListActivity we grab the MyLinearLayout and pass this to it. Then a setOnFocusChangeListener is registered to handle things when the EditText's focus changes. The setOnClickListener is used to hide the element when the EditText already has focus.

    public class MyListActivity extends ListActivity {
    
        private ArrayList<MyData> mDataList = new ArrayList<MyData>();
    
        private MyLinearLayout mMyLinearLayout; 
        private LinearLayout mHideMeLinearLayout;
        private EditText mEditText;
    
        public void showBar() {
            if (mHideMeLinearLayout != null) {
                mHideMeLinearLayout.setVisibility(View.VISIBLE);
            }
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // Get MyLinearLayout and pass this to it.
            mMyLinearLayout = (MyLinearLayout) findViewById(R.id.myLinearLayout);
            mMyLinearLayout.setMyListActivity(this);
    
            // the LinearLayout to be hidden
            mHideMeLinearLayout = (LinearLayout) findViewById(R.id.LinearLayoutToHide);
    
            mEditText = (EditText) findViewById(R.id.editText);     
            mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
                    if (hasFocus) {
                        imm.showSoftInput(mEditText, 0);
                        mHideMeLinearLayout.setVisibility(View.GONE);
                    } else {
                        imm.hideSoftInputFromWindow(mMyLinearLayout.getWindowToken(), 0);
                        mHideMeLinearLayout.setVisibility(View.VISIBLE);
                    }
                }
            });
            mEditText.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mHideMeLinearLayout.setVisibility(View.GONE);
                }
            });
    
            .....
        }
    
        .....
    }
    

    I'll provide a working example later, but gotta run now.

    0 讨论(0)
提交回复
热议问题