【问题标题】:HorizontalListView scaling content (items layouts)Horizo​​ntalListView 缩放内容(项目布局)
【发布时间】:2015-12-21 21:57:36
【问题描述】:

我正在尝试创建一个HorizontalListView(by DevSmart),其中第一项(列)将具有Ndp 宽度,所有下一项将具有N/2dp 宽度。在我的例子中N = 300。所以我为标准的 Android ListView 完美地完成了它,它工作得很好。但是遵循与 Horizo​​ntalListView 完全相同的逻辑,我面临着误解。

所有项目的宽度和高度都与 Horizo​​ntalListView 相同。它 我在photo.xml 中指定的宽度/高度无关紧要。 结果,我的项目在整个屏幕上都变得又宽又高(因为我的 com.example.yura.listviewscale.HorizontalListViewmatch_parent 的宽度和高度)

有人可以帮我吗?

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>

适配器类的存根:

@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;
    }

【问题讨论】:

    标签: java android listview horizontallistview


    【解决方案1】:

    使用带有自定义 LayoutManager 的 RecyclerView 解决了这个问题。在 onLayoutChildren 方法中执行所有魔法。尤其是测量所有视图和布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-29
      • 2015-05-25
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多