2016-01-05 3 views
1

Я хочу показать список кругов в GridView, прокручиваемом вертикально. Я хочу один элемент из двух в 3 столбцах.GridView android сухие элементы при прокрутке

Вы можете увидеть скриншот результата.

GridView after loading

Но у меня есть еще две проблемы: - Когда я прокрутите вниз в мое приложение, приложение перерисовать круги и Суффле их. (второй экран). After scroll - У меня нет идеальной круглой кнопки, я не понимаю, почему.

Для этого у меня есть что: моя активность с GridLayout

gridView = (GridView) findViewById(R.id.interestsgrid); 
    gridView.setColumnWidth(UTIL.getScreenWidth(getBaseContext())/3); 
    List<Interest> interests = new ArrayList<Interest>(); 
    interests.add(new Interest(18,"coucou7")); 
    interests.add(new Interest(18,"coucou7")); 
    interests.add(new Interest(18,"coucou6")); 
    interests.add(new Interest(18,"coucou6")); 
    interests.add(new Interest(18,"coucou5")); 
    interests.add(new Interest(18,"coucou5")); 
    interests.add(new Interest(18,"coucou4")); 
    interests.add(new Interest(18,"coucou4")); 
    interests.add(new Interest(18,"coucou3")); 
    interests.add(new Interest(18,"coucou3")); 
    interests.add(new Interest(18,"coucou8")); 
    interests.add(new Interest(18,"coucou8")); 
    interests.add(new Interest(18,"coucou9")); 
    interests.add(new Interest(18,"coucou9")); 
    interests.add(new Interest(18,"coucou10")); 
    interests.add(new Interest(18,"coucou10")); 
    interests.add(new Interest(18,"coucou11")); 
    interests.add(new Interest(18,"coucou12")); 
    interests.add(new Interest(18,"coucou13")); 
    interests.add(new Interest(18,"coucou13")); 
    interests.add(new Interest(18,"coucou14")); 
    interests.add(new Interest(18,"coucou14")); 
    gridView.setAdapter(new InterestsGridAdapter(this, interests)); 

Activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:fitsSystemWindows="true" 
    tools:context=".viewController.user.ChoiceInterests"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/AppBarLayoutInterest" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

      <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/interestsgrid" 
       android:layout_width="match_parent" 
       android:paddingTop="10sp" 
       android:layout_below="@+id/AppBarLayoutInterest" 
       android:layout_height="match_parent" 
       android:columnWidth="90dp" 
       android:numColumns="auto_fit" 
       android:stretchMode="columnWidth" 
       android:gravity="center" 
       /> 
    </RelativeLayout> 

и мой адаптер:

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

     // LayoutInflator to call external grid_item.xml file 

      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View gridView; 


      if (convertView == null) { 

       gridView = new View(context); 

       // get layout from grid_item.xml (Defined Below) 

       gridView = inflater.inflate(R.layout.interest_item, null); 

       // set value into textview 

       Button buttonView = (Button) gridView 
         .findViewById(R.id.buttonCircleInterest); 

       int width = UTIL.getScreenWidth(parent.getContext()); 


       ViewGroup.LayoutParams params = buttonView.getLayoutParams(); 
       params.width = (width/3); 
       params.height = (width/3); 
       buttonView.setLayoutParams(params); 

       if(position%2 != 0){ 
        buttonView.setVisibility(View.INVISIBLE); 
       } 

       buttonView.setText(this.interests.get(position).getName()); 

      } else { 
       gridView = (View) convertView; 
      } 

      return gridView; 
    } 
    } 

Мой GridItem:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <Button 
     android:layout_width="50sp" 
     android:layout_height="50sp" 
     android:id="@+id/buttonCircleInterest" 
     android:background="@drawable/rounshapebtn" 
     android:textColor="@color/white" 
     android:layout_marginBottom="2sp" 
     android:text="1"/> 
</LinearLayout> 

мой Drawable иметь круглую кнопку:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> 
    <solid android:color="#FFA633"/> <!-- this one is ths color of the Rounded Button --> 
    <corners 
     android:bottomRightRadius="50dp" 
     android:bottomLeftRadius="50dp" 
     android:topLeftRadius="50dp" 
     android:topRightRadius="50dp"/> 
</shape> 

Спасибо за ваши ответы!

ответ

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

    // LayoutInflator to call external grid_item.xml file 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View gridView; 


     if (convertView == null) { 

      gridView = new View(context); 

      // get layout from grid_item.xml (Defined Below) 

      gridView = inflater.inflate(R.layout.interest_item, null); 


     } else { 
      gridView = (View) convertView; 
     } 

     // set value into textview 

      Button buttonView = (Button) gridView 
        .findViewById(R.id.buttonCircleInterest); 

      int width = UTIL.getScreenWidth(parent.getContext()); 


      ViewGroup.LayoutParams params = buttonView.getLayoutParams(); 
      params.width = (width/3); 
      params.height = (width/3); 
      buttonView.setLayoutParams(params); 

      if(position%2 != 0){ 
       buttonView.setVisibility(View.INVISIBLE); 
      } 

      buttonView.setText(this.interests.get(position).getName()); 


     return gridView; 
    } 
    } 
+0

Спасибо за помощь, но когда я прокручу, у меня все еще есть та же проблема. Я не понимаю, почему? –

+0

Хорошо, я исправил проблему с перерисованием своих кругов. Для этого, если convertview! = Null, я помещаю его в нуль и каждый раз раздуваю представление. –

+0

Не жаль, что я не получаю решение от вас, потому что ваш код не работает –

Смежные вопросы