2016-11-10 6 views
-2

Ich habe ein Problem mit "GridView". Wenn ich 6 Elemente verwenden, dann erstellt es kein Problem beim Scrollen, aber wie ich Nr. Erhöhen. von Gegenständen ist sein Verhalten unvorhersehbar. Überprüfen Sie bitte das folgende Video-Link für eine bessere mein Problem zu verstehen: GridView Issues video linkGridView Problem mit großen Daten beim Scrollen

ich meine Codes hier bin Befestigung:

  1. 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> 
    
  2. 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> 
    
  3. 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; 
          } 
    
         } 
    
  4. 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()); 
          } 
         } 
    
  5. 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; 
           } 
          } 
         } 
    
+0

Sie müssen eine Menge mehr Informationen als das zur Verfügung stellen. Layout jeder Zeile, Adaptercode usw. –

+0

Dies sollte ein Problem der Wiederverwendung von Zellen/Gitterelementen sein. Der Grund, warum die ersten 6 richtig sind, liegt darin, dass diese Elemente beim ersten Laden auf dem Bildschirm sichtbar sind. Danach können Sie die Objekte erneut verwenden und den Inhalt aktualisieren. – necixy

Antwort

0
if(view == null) 
view.setTag(viewHolder); 
viewHolder = (ViewHolder) view.getTag(); 

verwenden diese nicht. oder senden Sie Ihren Code

Verwandte Themen