【问题标题】:Changes to the layout of item at position 0 of the gridview更改gridview位置0处的项目布局
【发布时间】:2014-04-14 14:02:15
【问题描述】:

我有一个gridview,我正在用relativelayout 膨胀它。将此视为布局示例:

0  1  2
3  4  5
......
......

我在布局中有一个progress bar,我只想将其应用于位置0 的布局。并在设置适配器后使用async task 对其进行更新。当然,使用 gridview 我不能只访问位置 0 的视图并在再次渲染时显示/隐藏progressbar。我应该怎么做才能修复gridview的第一个孩子的布局,并且在用户滚动项目时不让它改变?

这是我getView()的方法:

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

        ViewHolder holder = null;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.gridview_tvguide, null);
            holder = new ViewHolder();
            holder.image = (ImageButton) convertView.findViewById(R.id.image);
            holder.showTitle = (TextView) convertView
                    .findViewById(R.id.ShowTitle);
            holder.showDuration = (TextView) convertView
                    .findViewById(R.id.ShowTime);
                    //by default visibility is set to GONE
            holder.progBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

            //logic to control the progressbar from asynctask
        if (this.updateNowPlaying){
            holder.progBar.setProgress(this.progress);
            return convertView;
        }

        ImageLoader.getInstance().displayImage(
                this.shows.get(position).getShowThumb(), holder.image);
        holder.showTitle.setText(this.shows.get(position).getShowTitle());
        holder.showDuration.setText(this.shows.get(position).getShowTime());

        return convertView;
    }

编辑

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlImage"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:paddingBottom="7dp"
    android:paddingRight="7dp" >

    <ImageButton
        android:id="@+id/image"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:scaleType="centerCrop"
        android:src="@drawable/noimage" />

    <ImageButton
        android:id="@+id/imageHover"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/tile_selector_style" />

    <TextView
        android:id="@+id/ShowTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:hint="Show Title"
        android:paddingBottom="40dp"
        android:paddingRight="20dp"
        android:singleLine="true" />

    <TextView
        android:id="@+id/ShowTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:hint="Show Time"
        android:paddingBottom="20dp"
        android:paddingRight="20dp"
        android:singleLine="true" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        android:layout_margin="0dp" />

</RelativeLayout>

这是我的gridview

<GridView
            android:id="@+id/gridView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:clipToPadding="true"
            android:columnWidth="200dp"
            android:fitsSystemWindows="true"
            android:layout_weight="1"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" >
        </GridView>

【问题讨论】:

  • 如何在不影响布局的情况下做到这一点?我也在添加我的layout inflator 代码。
  • @MikeM。我已经尝试了您可能的解决方案。它工作正常,但现在的问题是这个项目不再是gridview 的一部分。这意味着没有scrolling
  • 我希望 gridview 的位置 0 成为其中包含 progressbar 的布局。 gridview 中的其余项目应该在 NOT 中有一个 progressbar,无论我滚动多少......

标签: android android-layout android-gridview


【解决方案1】:

仅显示item 0 的进度条在if/else 初始化您的持有人之后添加此代码:

if (position == 0) {
    holder.progBar.setVisibility(View.VISIBLE);
} else {
    holder.progBar.setVisibility(View.GONE);
}

【讨论】:

  • 是的,这就是这段代码将产生的。左上方的项目将有一个ProgressBar,当用户滚动回item 0 时,它将正确滚动并再次显示。你有什么顾虑?
  • @MikeM。是的。我希望 progressbar 仅在左上方的项目中可见。整个 gridview 将仅包含 1 个带有 progressbar 的项目
  • 抱歉,我无法想象这种行为。因此,当用户滚动GridView 时,可以说左上角的项目部分滚动出屏幕。它显示了进展吗?还是它下面的那个?
  • @MikeM。不用担心。希望我们最终能够帮助他。
  • @MikeM .. 我最终碰巧尝试了您要求的内容.. 原来指令顺序在这里是一个问题。感谢您正确设置!
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多