YTerle
YTerle

Reputation: 2696

HorizontalListView scaling content (items layouts)

I'm trying to make a HorizontalListView(by DevSmart), in which first item(column) will have Ndp width and all next items will have N/2dp width. In my example N = 300. So i perfectly done it for standard Android ListView and it works great. But following exactly same logic with HorizontalListView, i face with misconception.

All items have same width and height as HorizontalListView. It doesen't matter what width/height i specify in photo.xml. And as a result i get items wide and tall for whole screen (because my com.example.yura.listviewscale.HorizontalListView has match_parent for width and height)

Can somebody help me ?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yura.listviewscale.MainActivity" >

<com.example.yura.listviewscale.HorizontalListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myHzListView" />

</LinearLayout>

photo.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parnet"
        android:layout_height="match_parent"
        android:id="@+id/photoView" />
</RelativeLayout>

Stub of Adapter class:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.photo, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.photoImageView = (ImageView) convertView.findViewById(R.id.photoView);

            mInitialWidth = convertView.getLayoutParams().height;

            convertView.setTag(viewHolder);
        } else
            viewHolder = (ViewHolder)convertView.getTag();

        Photo photo = mPhotoList.get(position);

        if  (position != 0) {
            RelativeLayout.LayoutParams params = convertView.getLayoutParams();
            params.width = (int) (mInitialWidth * 0.5f);
        }

        viewHolder.photoImageView.setBackgroundDrawable(photo.getDrawable());

        return convertView;
    }

    static final class ViewHolder {
        ImageView photoImageView;
    }

Upvotes: 1

Views: 44

Answers (1)

YTerle
YTerle

Reputation: 2696

Solved it using RecyclerView with custom LayoutManager. Performed all magic in onLayoutChildren method. Especially measuring all views and layouts.

Upvotes: 1

Related Questions