简体   繁体   中英

Android how to align table to cell position

I have a first TableLayout with rows and cells generated dynamically by code.
Now, i have a second TableLayout smaller composed by 3 row.
This second table is showed by clicking any cell in the first Table.
Now my question is:
How i can align this second Table at the position of the cell clicked, overlapped to the first table?

Thanks for the advices.


EDIT: unfortunally i can't show the layout to public. but i drawed how it should be. i'm sorry for the unconvenience.

first table. click on cell

show the second table at the senter of cell clicked

You may choose to use a custom Dialog Fragment to do so.

First, get the global x & y position of the cell of the first table view that you clicked. From this post:

Rect myViewRect = new Rect();
myView.getGlobalVisibleRect(myViewRect);
float cx = myViewRect.exactCenterX();
float cy = myViewRect.exactCenterY();

Then, create a class that extends DialogFragment (let's call it SecondTableFragment ). To change the Dialog from your default Android style dialogs, you need to do a few things

Make the title disappear

@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

Set the width of the Dialog to fit your second table view

    @Override
    public void onResume() {
    getDialog().getWindow().setLayout([PARENT_LAYOUT_TYPE].LayoutParams.WRAP_CONTENT, [PARENT_LAYOUT_TYPE].LayoutParams.WRAP_CONTENT);
    super.onResume();
}

Set the position of your dialog window above that of your clicked cell (this should most probably be done in onCreateDialog() as well):

 WindowManager.LayoutParams wmlp =  dialog.getWindow().getAttributes();

 wmlp.gravity = Gravity.TOP | Gravity.LEFT;
 wmlp.cx = cx;   //x position
 wmlp.cy = cy;   //y position

For the logic behind filling out the Dialog view itself, you need to create a new layout file for the 2nd table layout (like you do for a new Activity) and inflate it and fill it up with relevant information like any other normal fragment. Make sure to pass in the x & y co-ordinates you want the dialog to be in when calling it's newInstance() method. See Google's Documentation

Once everything's set up, you can then show a "2nd table Dialog" above the 1st TableLayout with the relevant information by using Fragment Manager (or ChildFragmentManager if you are already in a Fragment)

SecondTableFragment fragment = SecondTableFragment.newInstance(cx, cy, RELEVANT INFORMATION ...);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("secondTable");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.add(fragment, "secondTable");
    ft.addToBackStack(null);
    ft.commitAllowingStateLoss();
    fragment.setCancelable(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM