GridView第一项的OnClickListener无法正常工作

时间:2015-04-24 13:47:54

标签: android gridview onclicklistener android-viewholder

这个问题已经回答here

但我的要求是重用视图,因为我必须处理大数据,因此每次需要显示视图时都无法启动视图对象。

当我重复使用convertView而不是onClickListener时,我正在使用带有视图持有者模式的网格视图,因为第一个项目没有响应,并且通过触摸gridview中的任何地方它完成了它的操作。

以下是我使用GridView元素进行布局的代码。

    <GridView
        android:id="@+id/peopleList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"            
        android:numColumns="2"
        android:clickable="false"
        android:gravity="center"
        android:focusable="false"
        android:scrollbars="none"
        android:visibility="visible" 
        android:stretchMode="columnWidth"
        android:focusableInTouchMode="false"
        android:background="@color/transparent">
    </GridView>

以下是我对gridView项目的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_gridview_data">

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/peopleImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/light_color"
    android:scaleType="fitXY"
    android:src="@drawable/image_placeholder" />

<TextView
    android:id="@+id/peopleNameText"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_gravity="center|top"
    android:background="@drawable/picture_gradient_small"
    android:gravity="left|center"
    android:paddingLeft="@dimen/horizontal_margin_10dp"
    android:text="abc123"
    android:textColor="@color/white"
    android:textSize="15sp" >
</TextView>

<ImageButton
    android:id="@+id/followUnfollow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:layout_marginBottom="@dimen/horizontal_margin_10dp"
    android:layout_marginRight="@dimen/vertical_margin_5dp"
    android:background="@color/transparent"
    android:src="@drawable/follow" />

</FrameLayout>

以下是我的Adapter的getView()

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

    final Person mDiscoverPeople = mDiscoverPlacesList.get(position);

    if (convertView == null) {

        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.discover_people_grid_data_update, parent, false);

        mDiscoverPeopleListItemViewHolder = new DiscoverPeopleListItemViewHolder();
        mDiscoverPeopleListItemViewHolder.peopleImage = (NetworkImageView) convertView.findViewById(R.id.peopleImage);
        mDiscoverPeopleListItemViewHolder.peopleName = (TextView) convertView.findViewById(R.id.peopleNameText);
        mDiscoverPeopleListItemViewHolder.mFrameLayout = (FrameLayout) convertView.findViewById(R.id.fl);
        mDiscoverPeopleListItemViewHolder.follow = (ImageView) convertView.findViewById(R.id.followUnfollow);

        convertView.setTag(mDiscoverPeopleListItemViewHolder);
    } else {
        mDiscoverPeopleListItemViewHolder = (DiscoverPeopleListItemViewHolder) convertView.getTag();
    }

    mDiscoverPeopleListItemViewHolder.peopleImage.setImageUrl(mDiscoverPeople.getImgUrl(), imageLoader, false);
    mDiscoverPeopleListItemViewHolder.mFrameLayout.setLayoutParams(new GridView.LayoutParams(AppConstants.columnWidth - AppConstants.columnPadding, AppConstants.columnWidth - AppConstants.columnPadding));
    mDiscoverPeopleListItemViewHolder.peopleName.setText(mDiscoverPeople.getName());
    if (mDiscoverPeople.isFollowed())
        mDiscoverPeopleListItemViewHolder.follow.setImageResource(R.drawable.following);
    else
        mDiscoverPeopleListItemViewHolder.follow.setImageResource(R.drawable.follow);

    mDiscoverPeopleListItemViewHolder.follow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FollowUnfollowUser mFollowUnfollowUser = new FollowUnfollowUser(mContext, mDiscoverPeopleListItemViewHolder.follow, mDiscoverPeople);
            mFollowUnfollowUser.followUnFollowUser(AppConstants.DISCOVER_PEOPLE);
            Toast.makeText(mContext, "Follow/Unfollow", Toast.LENGTH_SHORT).show();
        }
    });

    mDiscoverPeopleListItemViewHolder.peopleImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent mUserProfile = new Intent(mContext, FriendsRekkiesList.class);
            Bundle userData = new Bundle();
            userData.putSerializable(AppConstants.USER_DETAILS, mDiscoverPeople);
            mUserProfile.putExtras(userData);
            mContext.startActivity(mUserProfile);
        }
    });

    return convertView;
}

如果我不重用我的视图,除了应用程序性能之外它工作正常,我需要一种解决方法。

任何建议都将受到赞赏.... Thankx

0 个答案:

没有答案