-2
「GridView」に問題があります.6個の項目を使用すると、スクロール中に問題は発生しませんが、増やすと問題は発生しません。その動作は予測できません。私はここに私のコードを添付しています GridView Issues video linkスクロール中に大きなデータが表示されるGridViewの問題
:
activity_achievements.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentTop="true" android:background="@color/green_color"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/back_iv" android:text="Acheivements" android:textColor="@color/white_color" android:textSize="24sp" android:textStyle="bold" /> <ImageView android:id="@+id/back_iv" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@drawable/back" /> <TextView android:id="@+id/total_achievements_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:text="0/15" android:textColor="@color/white_color" android:textSize="24sp" android:textStyle="bold" /> </RelativeLayout> <GridView android:id="@+id/acheivements_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/header" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="5dp" android:numColumns="2" android:stretchMode="columnWidth" android:verticalSpacing="5dp" /> </RelativeLayout>
achievements_row.xml
を は私の問題をよりよく理解するために以下のビデオのリンクをご確認ください<?xml version="1.0" encoding="utf-8"?> <com.xyz.quitsmoking.view.AchievementsView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <android.support.v7.widget.CardView android:id="@+id/card_view2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/card_view1" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" card_view:cardCornerRadius="2dp" android:background="@color/white_color" card_view:contentPadding="2dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/image_iv" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:layout_margin="10dp" android:src="@drawable/achievement" /> <TextView android:id="@+id/title_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:textSize="20sp" android:padding="10dp" android:text="Himalyi" android:textColor="@color/green_color" /> <TextView android:id="@+id/desc_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:textSize="16sp" android:padding="10dp" android:text="5 cigarettes non-smoked" android:textColor="@color/teal_color" /> </LinearLayout> </android.support.v7.widget.CardView> </pairroxz.com.quitsmoking.view.AchievementsView>
AchievementsAdapter.java
public class AchievementsAdapter extends BaseAdapter { private Context context; private ArrayList<Achievements> list; private LayoutInflater inflater; public AchievementsAdapter(Context context,ArrayList<Achievements> list){ this.context = context; this.list = list; initialize(); } private void initialize() { inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { AchievementsView view = null; if(view==null) { view = (AchievementsView) inflater.inflate(R.layout.achievements_row,null); }else{ view = (AchievementsView) convertView; } view.setContents(list.get(position)); notifyDataSetChanged(); return view; } }
AchievementsView.java
public class AchievementsView extends RelativeLayout { private TextView title_tv,desc_tv; private ImageView image_iv; public AchievementsView(Context context) { super(context); } public AchievementsView(Context context, AttributeSet attrs) { super(context, attrs); } public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onFinishInflate() { super.onFinishInflate(); initialize(); } private void initialize() { image_iv = (ImageView)findViewById(R.id.image_iv); title_tv = (TextView)findViewById(R.id.title_tv); desc_tv = (TextView) findViewById(R.id.desc_tv); } public void setContents(Achievements achievements){ image_iv.setImageDrawable(getContext().getResources().getDrawable(achievements.getImg())); title_tv.setText(achievements.getTitle()); desc_tv.setText(achievements.getDesc()); } }
AchievementsActivity.java
public class AchievementsActivity extends AppCompatActivity implements View.OnClickListener { private GridView achievements_gv; private AchievementsAdapter adapter; private ArrayList<Achievements> list; DatabaseHelper helper; private ImageView back_iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_achievements); initialize(); } private void initialize() { helper = DatabaseHelper.getInstance(this); back_iv = (ImageView) findViewById(R.id.back_iv); achievements_gv = (GridView) findViewById(R.id.acheivements_gv); list = new ArrayList<Achievements>(); getData(); setListener(); } private void setListener() { back_iv.setOnClickListener(this); } private void getData() { list = helper.getAchievementsList(); //list = adapter = new AchievementsAdapter(this, list); achievements_gv.setAdapter(adapter); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.back_iv: onBackPressed(); break; } } }
それ以上の情報を提供する必要があります。 –
これはセル/グリッドの項目を再利用する際の問題です。なぜなら最初の6点が得られた理由はそれらが最初に読み込まれたときに画面上に表示される項目なので、その後、Androidはアイテムを再利用してコンテンツを更新します。 – necixy