gpt4 book ai didi

android - GridView 项目不向下滚动到向上(重复项目)以及当我从 android 中的服务器加载数据时?

转载 作者:搜寻专家 更新时间:2023-11-01 07:43:21 25 4
gpt4 key购买 nike

我有一个 GridView ,使用 api 调用在 GridView 中更新数据,网格项目使用 picasso 库从 url 加载图像,所有这些都是完美的,但是当我向下滚动到向上时,网格项目没有滚动并重复项目(向上向下滚动是完美的)。

适配器类.java

private class ProductAdapter extends BaseAdapter {
ViewHolder holder = null;
@Override
public int getCount() {
return mProducts != null ? mProducts.size() : 0;
}

@Override
public Object getItem(int position) {
return null;
}

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

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

holder = new ViewHolder();
if (convertView == null) {
// convertView = LayoutInflater.from(Products_activity.this).inflate(R.layout.product_layout, null, false);

LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.product_layout, null);

holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
holder.product_descr = (TextView) convertView.findViewById(R.id.product_desc);
holder.product_price = (TextView) convertView.findViewById(R.id.product_price);
holder.product_pic = (ImageView) convertView.findViewById(R.id.iv_division_img);
holder.product_share = (ImageView) convertView.findViewById(R.id.productshare_button);
holder.like_image=(ImageView) convertView.findViewById(R.id.like_image);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}





if (mProducts != null) {
String url = "http://staggershop.pivalue.net/assets/product_images/"+mProducts.get(position).product_image;
String urls = url.replaceAll(" ", "%20");
Picasso.with(getApplicationContext()).load(urls).resize(300,300).noFade().error(R.drawable.home_brands).into(holder.product_pic);
holder.product_name.setText(mProducts.get(position).product_name);
//product_descr.setText(mProducts.get(position).pr);
// product_price.setText(mProducts.get(position).product_price);
/* Picasso
.with(getApplicationContext())
.setLoggingEnabled(true);

Picasso.with(getApplicationContext())
.load(urls)
.error(R.drawable.home_brands).memoryPolicy(MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_STORE)
.into(product_pic);*/
holder.product_descr.setText(mProducts.get(position).product_description);

if(!recently_beans.isEmpty()){
for(int i=0;i<recently_beans.size();i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id))
{
holder.like_image.setImageResource(R.drawable.like);

}
}

notifyDataSetChanged();


}






}

holder.product_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
});
holder.like_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (!recently_beans.isEmpty()) {

for (int i = 0; i < recently_beans.size(); i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id)) {

holder.like_image.setImageResource(R.drawable.dislike);
Inserfavorite(mProducts.get(position).prdid);


}
}


}
}
});

}

});

// notifyDataSetChanged();
convertView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Inserrecent(mProducts.get(position).prdid);
Intent intent = new Intent(Products_activity.this, Product_description.class);

startActivity(intent);


}
});
return convertView;
}

}
public static class ViewHolder {
public TextView product_name;
public TextView product_descr;
public TextView product_price;
public ImageView product_pic ;
public ImageView product_share;
public ImageView like_image;

这是我的 product.xml

 <RelativeLayout
android:id="@+id/rl_divisioin_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/product_img">


<ImageView
android:id="@+id/iv_division_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="@drawable/home_brands" />
</LinearLayout>

<LinearLayout
android:id="@+id/prod_name_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/product_img"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="@string/font_family_regular"
android:gravity="center"
android:padding="5dp"
android:maxLines="1"
android:text="product name"
android:textColor="@color/black"
android:textSize="10sp" />
<TextView
android:id="@+id/product_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="@string/font_family_regular"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:text="product Description"
android:textColor="@color/black"
android:textSize="10sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/product_desc"
android:layout_gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:elevation="4dp"
android:text="100$"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/productshare_button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:elevation="4dp"
android:layout_marginRight="5dp"
android:src="@mipmap/symbol_dotss"
android:text="product name"
android:textColor="@color/black"
android:textSize="10sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:id="@+id/like_image"
android:layout_alignParentEnd="true"
android:src="@drawable/dislike" />
</RelativeLayout>

activty_product.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:id="@+id/rltool"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar1"
android:background="@color/textColorPrimary"

app:titleTextColor="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"

android:background="@drawable/background_shape_buttons">



<!-- <EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:hint="Search..................."
android:layout_gravity="center"
android:backgroundTint="#00000000" />-->
<SearchView
android:id="@+id/search"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"

android:text="@string/search"
android:layout_gravity="center"
android:iconifiedByDefault="false">

<requestFocus />
</SearchView>

</LinearLayout>
</android.support.v7.widget.Toolbar>

<View
android:id="@+id/shadow_view"
android:layout_width="match_parent"
android:layout_alignBottom="@+id/toolbar1"
android:visibility="visible"
android:layout_height="3dp"
android:background="@drawable/toolbar_shadow" />
</RelativeLayout>
<GridView
android:id="@+id/product_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical"

android:layout_margin="15dp"
android:horizontalSpacing="10dp"
android:numColumns="2"

android:verticalSpacing="10dp">


</GridView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

最佳答案

为避免意外行为,提供

的实现
@Override
public Object getItem(int position) {
return mProducts.get(position);
}

@Override
public long getItemId(int position) {
//return position; or better to provide some unique id
// as there is one in your code, product id
return mProducts.get(position).prod_id;
}

注意:删除 getView 中的 notifyDataSetChanged(); 因为它会触发适配器刷新所有正在创建的 View ,这可能会导致此行为。

推荐阅读

What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

关于android - GridView 项目不向下滚动到向上(重复项目)以及当我从 android 中的服务器加载数据时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com