2

我正在使用 ListView 开发一个应用程序,以显示我在运行时从服务器获取的一些数据。

我在列表视图中动态设置数据。下面是我的 ROW XML:

<LinearLayout
    android:id="@+id/top_lilay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/top_header_relay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/timeline_header_bg_480" >

        <TextView
            android:id="@+id/bride_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="13dp"
            android:text="STELLA"
            android:textColor="@android:color/black"
            android:textSize="50sp" />

        <ImageView
            android:id="@+id/and_IV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/bride_tv"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="3dip"
            android:background="@drawable/and" />

        <TextView
            android:id="@+id/groom_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/and_IV"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="3dip"
            android:text="JOSE"
            android:textColor="@android:color/black"
            android:textSize="50sp" />

        <TextView
            android:id="@+id/time_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/groom_tv"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dip"
            android:text="85 DIAS 23 HOURAS 22 MINUS Y 30 SEG"
            android:textColor="@android:color/white"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/text_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/time_tv"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="2dip"
            android:text="para nuestro gran dia"
            android:textColor="@android:color/white"
            android:textSize="15sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/bar_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/orange_bar_480" />

    <ImageView
        android:id="@+id/photo_IV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:background="@drawable/post_photo" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/heading_relay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/top_lilay"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip" >

    <ImageView
        android:id="@+id/profile_pic_IV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:background="@drawable/profile_pic" />

    <TextView
        android:id="@+id/username_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/profile_pic_IV"
        android:text="LOREM IPSUM TITLE"
        android:textColor="@android:color/black"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/posttime_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/username_tv"
        android:layout_toRightOf="@id/profile_pic_IV"
        android:text="Jose Q. a las 2:30 pm el 24 september 2013"
        android:textColor="@android:color/black"
        android:textSize="12sp" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/buttons_relay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/heading_relay"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:weightSum="3" >

    <LinearLayout
        android:id="@+id/like_lilay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/like_IV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:background="@drawable/like" />

        <TextView
            android:id="@+id/likes_count_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/likes_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/likes"
            android:textColor="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/comment_lilay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/comment_IV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:background="@drawable/comment" />

        <TextView
            android:id="@+id/comment_count_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/comment_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/commments"
            android:textColor="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/fav_lilay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/fav_IV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:background="@drawable/favorite" />

        <TextView
            android:id="@+id/fav_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/favorites"
            android:textColor="@android:color/black" />
    </LinearLayout>
</LinearLayout>

仅对于第一个元素,我必须显示“ top_header_relay ”以及我必须将可见性更改为 GONE的其余元素。在这样做时,其他行的高度保持与第一行相同,从而导致行底部有一个大的空白区域。

建议我如何在运行时调整列表其他行的高度。

适配器类:

public class PostsListAdapter extends BaseAdapter implements OnClickListener{

    ArrayList<String> itemList=null;

    private Activity _mContext=null;
    public LayoutInflater inflater=null;
    private Context _Context = null;
    private ArrayList<ViewHolder> listHolder=null;
    private int[] imageList = {R.drawable.calendar_icon,
            R.drawable.location_icon,R.drawable.fun_icon,
            R.drawable.notification_icon,R.drawable.invitation_icon,
            R.drawable.change_wedding,R.drawable.settings_icon,
            R.drawable.logout_icon};

    /**
     * PostsListAdapter
     * @param context
     * @param itemList
     */
    public PostsListAdapter(Activity context,ArrayList<String> itemList) {
        super();
        this._Context = context;
        this._mContext = context;
        this.itemList = itemList;

        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listHolder = new ArrayList<PostsListAdapter.ViewHolder>();
    }


    public int getCount() {
        return itemList.size();
    }

    public Object getItem(int position) {
        return itemList.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    public static class ViewHolder{
        private ImageView _postImageView=null;
        private ImageView _profilePicImageView=null;
        private TextView _usernameTitleTextView=null;
        private TextView _timeTextView=null;
        private TextView _likeCountTextView=null;
        private TextView _commentsCountTitleTextView=null;

        private LinearLayout _likeLinearLayout=null;
        private LinearLayout _commentLinearLayout=null;
        private LinearLayout _favLinearLayout=null;
        private RelativeLayout _topheaderRelay=null;
        private ImageView _barViewImageView=null;
    }
    /**
     * 
     */
    public View getView(final int position, View convertView, ViewGroup parent) {

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

            convertView = inflater.inflate(R.layout.post_list_element, null);

            holder._postImageView=(ImageView)convertView.findViewById(R.id.photo_IV);
            holder._postImageView.setOnClickListener(this);
            holder._profilePicImageView=(ImageView)convertView.findViewById(R.id.profile_pic_IV);
            holder._profilePicImageView.setOnClickListener(this);

            holder._usernameTitleTextView = (TextView) convertView.findViewById(R.id.username_tv);
            holder._usernameTitleTextView.setClickable(false);
            holder._timeTextView = (TextView) convertView.findViewById(R.id.posttime_tv);
            holder._timeTextView.setClickable(false);
            holder._likeCountTextView = (TextView) convertView.findViewById(R.id.likes_count_tv);
            holder._likeCountTextView.setClickable(false);
            holder._commentsCountTitleTextView = (TextView) convertView.findViewById(R.id.comment_count_tv);
            holder._commentsCountTitleTextView.setClickable(false);

            holder._likeLinearLayout = (LinearLayout) convertView.findViewById(R.id.like_lilay);
            holder._likeLinearLayout.setOnClickListener(this);
            holder._commentLinearLayout = (LinearLayout) convertView.findViewById(R.id.comment_lilay);
            holder._commentLinearLayout.setOnClickListener(this);
            holder._favLinearLayout = (LinearLayout) convertView.findViewById(R.id.fav_lilay);
            holder._favLinearLayout.setOnClickListener(this);

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


        if(position!=0){
            holder._topheaderRelay = (RelativeLayout) convertView.findViewById(R.id.top_header_relay);
            holder._topheaderRelay.setVisibility(View.GONE);
            holder._barViewImageView = (ImageView) convertView.findViewById(R.id.bar_view);
            holder._barViewImageView.setVisibility(View.GONE);
        }else{
            holder._topheaderRelay = (RelativeLayout) convertView.findViewById(R.id.top_header_relay);
            holder._topheaderRelay.setVisibility(View.VISIBLE);
            holder._barViewImageView = (ImageView) convertView.findViewById(R.id.bar_view);
            holder._barViewImageView.setVisibility(View.VISIBLE);
        }
        listHolder.add(holder);

        return convertView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.like_lilay:

            break;
        case R.id.comment_lilay:

            break;
        case R.id.fav_lilay:

            break;
        case R.id.profile_pic_IV:

            break;
        case R.id.photo_IV:

            break;
        default:
            break;
        }
    }
}
4

1 回答 1

0

当您填充列表时,getview 函数是扩展视图和设置其布局属性的最佳位置。您可以使用 setvisibility 函数根据项目位置隐藏其他行的可见性

于 2013-09-07T06:27:20.893 回答