DevsmartLib-Android icon indicating copy to clipboard operation
DevsmartLib-Android copied to clipboard

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

Open krishnalalstha opened this issue 11 years ago • 2 comments

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

krishnalalstha avatar May 13 '14 11:05 krishnalalstha

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/dinocore1/DevsmartLib-Android/issues/43#issuecomment-42985540 .

krishnalalstha avatar May 14 '14 02:05 krishnalalstha

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.

azzits avatar Sep 04 '14 11:09 azzits