Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to center the child if there is only one child in horizontalListview. #43

Open
krishnalalstha opened this issue May 13, 2014 · 2 comments

Comments

@krishnalalstha
Copy link

I want to center the child in horizontalListview but horizontal listview not taking wrap_content from xml. any suggestions?

@krishnalalstha
Copy link
Author

The code is Same what it has in horizontalList. Problem here is Horizontal
list has no effect on wrap_content as it takes match parent

On Tue, May 13, 2014 at 11:10 PM, Troy Locke [email protected]:

Could you send some sample code. Or post your layout here.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-42985540
.

@azzits
Copy link

azzits commented Sep 4, 2014

Managed to make it work, now the content width is set to wrap_content and you can give gravity to your parent layout.

Add this function to the component class file.
public void resetPosition() {

if (mContext != null && mAdapter != null) {

    DisplayMetrics metrics = mContext.getResources()
            .getDisplayMetrics();
    int width = metrics.widthPixels;

    if (mAdapter.getCount() * mChildWidth > 0
            && mAdapter.getCount() * mChildWidth < width) {

        Log.e("child width", "child: " + mAdapter.getCount()
                + ": width:" + mChildWidth + "");

        setLayoutParams(new LinearLayout.LayoutParams(
                mAdapter.getCount() * mChildWidth, 300));
    }
}

}
After you set the adapter/notifydatasetchanged

call the function like

yourHorizontalListView.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            yourHorizontalListView.resetPosition();

        }
    }); 

Worked in my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants