2015-04-10 2 views
-1

Я создал один GridView, который содержит 2 столбца и несколько строк. Ребенок содержит 2 вида. Если я нажму на одного ребенка, мне нужно скрыть 1-й вид и показать 2-й вид этого ребенка. это работает нормально. Но когда я просматриваю сетку, выбранный ребенок (например: ребенок № 2) переходит в нормальное положение, а другой дочерний (случайный) появляется как выбранный. в чем проблема. пожалуйста помогите.Изменение ячейки выделения GridView при прокрутке

Мой Компоновка:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/relativeLayoutPlist1" 
    android:layout_width="165dp" 
    android:layout_height="150dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:background="#ffffff" 
    android:orientation="vertical" 
    android:visibility="visible" 
    android:focusable="false" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_marginLeft="6dp" 
     android:layout_marginRight="6dp" 
     android:orientation="horizontal" > 

     <com.android.volley.toolbox.NetworkImageView 
      android:id="@+id/img_subCategoryProducts" 
      android:layout_width="80dp" 
      android:layout_height="108dp" 
      android:layout_marginTop="5dp" /> 

     <!-- 
      <ImageView 
      android:layout_width="80dp" 
      android:layout_height="108dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/imp" /> 
     --> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:orientation="vertical" > 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginRight="5dp" > 

       <TextView 
        android:id="@+id/tv_subCategoryDetails_OrPrice" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:text="₹ 19.80" 
        android:textColor="#ab1b42" 
        android:textSize="15dp" /> 

       <TextView 
        android:id="@+id/tv_subCategoryDetails_MRPValue" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_below="@+id/tv_subCategoryDetails_OrPrice" 
        android:layout_marginTop="2dp" 
        android:text="₹ 11.80" 
        android:textColor="#ab1b42" 
        android:textSize="10dp" /> 
      </RelativeLayout> 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/linearLayout1" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:lines="2" 
      android:text="Knorr Cup a Soup Master Masala" 
      android:textColor="#4d4d4f" 
      android:textSize="12dp" /> 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productDescription" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productCategoryId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productSubCategoryId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productStoreId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tv_subCategoryDetails_productSize" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:text="(16.5 gm)" 
      android:textColor="#afafaf" 
      android:textSize="7dp" 
      android:visibility="gone" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="12dp" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/tv_subCategoryDetails_OrPrice1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="OR Price : ₹ 9.80" 
       android:textColor="#ab1b42" 
       android:textSize="9dp" 
       android:visibility="gone" /> 

      <TextView 
       android:id="@+id/tv_subCategoryDetails_MRPTitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="4dp" 
       android:text="MRP:₹ " 
       android:textColor="#4d4d4f" 
       android:textSize="8dp" 
       android:visibility="gone" /> 

      <TextView 
       android:id="@+id/tv_subCategoryDetails_MRPValue1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/price_strike" 
       android:text="10.00" 
       android:textColor="#4d4d4f" 
       android:textSize="8dp" 
       android:visibility="gone" /> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/img_subCategoryDetails_addToCart" 
      android:layout_width="match_parent" 
      android:layout_height="25dp" 
      android:layout_marginBottom="4dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="2dp" 
      android:layout_marginTop="17dp" 
      android:background="@drawable/img_add_to_cart" 
      android:scaleType="fitXY" 
      android:visibility="gone" /> 
    </LinearLayout> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/relativeLayoutPlist2" 
    android:layout_width="165dp" 
    android:layout_height="150dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:background="#bf1e4a" 
    android:orientation="vertical" 
    android:visibility="gone" 
    android:focusable="false" > 

    <RelativeLayout 
     android:id="@+id/plistDetailsRelativeLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="3dp" > 

     <ImageView 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:padding="4dp" 
      android:src="@drawable/details_plist" /> 

     <ImageView 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:padding="4dp" 
      android:src="@drawable/close_plist" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/plistBtnRelativeLayout" 
     android:layout_below="@+id/plistDetailsRelativeLayout" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" > 

     <LinearLayout 
      android:id="@+id/ll" 
      android:layout_width="35dp" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="20dp" 
       android:layout_height="22dp" 
       android:src="@drawable/leftarrow_plist" /> 
     </LinearLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager_plist" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_toLeftOf="@+id/ll1" 
      android:layout_toRightOf="@+id/ll" /> 

     <LinearLayout 
      android:id="@+id/ll1" 
      android:layout_width="35dp" 
      android:layout_height="match_parent" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/imageView2" 
       android:layout_width="20dp" 
       android:layout_height="22dp" 
       android:src="@drawable/rightarrow_plist" /> 
     </LinearLayout> 

     <FrameLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" > 

      <ProgressBar 
       android:id="@+id/pastPurchases_bill_prograssbar" 
       style="?android:attr/progressBarStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:indeterminateDrawable="@drawable/progress" 
       android:visibility="gone" /> 
     </FrameLayout> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/plistBtnRelativeLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" > 

     <ImageView 
      android:id="@+id/plusIconPlist" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:padding="4dp" 
      android:src="@drawable/plus_plist" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginTop="10dp" 
      android:layout_toLeftOf="@+id/minusIconPlist" 
      android:layout_toRightOf="@+id/plusIconPlist" 
      android:gravity="center" 
      android:text="23" 
      android:textColor="#FFFFFF" /> 

     <ImageView 
      android:id="@+id/minusIconPlist" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:padding="4dp" 
      android:src="@drawable/minus_plist" /> 
    </RelativeLayout> 
</RelativeLayout> 

MyAdapter:

class GridViewAdapterProductList extends BaseAdapter { 


    private ImageLoader mImageLoader; 
    Context ctx; 
    GridView gv; 
    LayoutInflater vi; 
    List<EntrySubCategoryDetails> product; 

    public GridViewAdapterProductList(Context context, 
      int textViewResourceId, List<EntrySubCategoryDetails> objects, 
      ImageLoader imageLoader, GridView gv_subCategoryDetails) { 

     mImageLoader = imageLoader; 
     ctx = context; 
     gv = gv_subCategoryDetails; 
     this.product = objects; 
     vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    class ViewHolder { 
     NetworkImageView image; 
     TextView tv_subCategoryDetails_productName; 
     TextView tv_subCategoryDetails_productId; 
     TextView tv_subCategoryDetails_productDescription; 
     TextView tv_subCategoryDetails_productCategoryId; 
     TextView tv_subCategoryDetails_productSubCategoryId; 
     TextView tv_subCategoryDetails_productStoreId; 
     TextView tv_subCategoryDetails_productSize; 
     TextView tv_subCategoryDetails_OrPrice; 
     TextView tv_subCategoryDetails_MRPValue; 
     TextView tv_subCategoryDetails_MRPTitle; 
     RelativeLayout r1; 
     RelativeLayout r2; 
    } 

    @Override 
    public int getCount() { 
     return product.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     return product.get(arg0); 
    } 

    @Override 
    public long getItemId(int position) { 
     return product.indexOf(getItem(position)); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View view; 
     ViewHolder holder =null; 
     if (convertView == null) { 
      view = LayoutInflater.from(ctx).inflate(
        R.layout.sub_category_product_list, null); 
      holder = new ViewHolder(); 
      holder.image = (NetworkImageView) view.findViewById(R.id.img_subCategoryProducts); 
      holder.tv_subCategoryDetails_productId = (TextView) view.findViewById(R.id.tv_subCategoryDetails_productId); 
      holder.tv_subCategoryDetails_productName = (TextView) view.findViewById(R.id.tv_subCategoryDetails_productName); 
      holder.tv_subCategoryDetails_productDescription = (TextView)view.findViewById(R.id.tv_subCategoryDetails_productDescription); 
      holder.tv_subCategoryDetails_productCategoryId = (TextView)view.findViewById(R.id.tv_subCategoryDetails_productCategoryId); 
      holder.tv_subCategoryDetails_productSubCategoryId = (TextView)view.findViewById(R.id.tv_subCategoryDetails_productSubCategoryId); 
      holder.tv_subCategoryDetails_productStoreId = (TextView)view.findViewById(R.id.tv_subCategoryDetails_productStoreId); 

      holder.tv_subCategoryDetails_productSize = (TextView) view.findViewById(R.id.tv_subCategoryDetails_productSize); 
      holder. tv_subCategoryDetails_OrPrice = (TextView) view.findViewById(R.id.tv_subCategoryDetails_OrPrice); 
      holder.tv_subCategoryDetails_MRPValue = (TextView) view.findViewById(R.id.tv_subCategoryDetails_MRPValue); 
      holder.tv_subCategoryDetails_MRPTitle = (TextView) view.findViewById(R.id.tv_subCategoryDetails_MRPTitle); 
      holder. r1 = (RelativeLayout) view.findViewById(R.id.relativeLayoutPlist1); 
      holder.r2 = (RelativeLayout) view.findViewById(R.id.relativeLayoutPlist2); 
      view.setTag(holder); 
     }else { 
       view = (View)convertView; 
       holder = (ViewHolder) view.getTag(); 
       //ShoppingListNames rowItems = (ShoppingListNames) getItem(position); 
      } 
     // final ViewHolder holder = (ViewHolder) convertView.getTag(); 
     EntrySubCategoryDetails entry = (EntrySubCategoryDetails) getItem(position);   

     holder.image.setDefaultImageResId(R.drawable.img_loading); 
     if (entry.getThumbnailUrl() != null) { 
      holder.image.setImageUrl(entry.getThumbnailUrl(), mImageLoader); 
     } 
     holder.image.setErrorImageResId(R.drawable.img_loading); 
     holder.tv_subCategoryDetails_productName.setText(entry.getProductName()); 
     holder.tv_subCategoryDetails_productSize.setText("("+entry.getProductUnit()+")"); 
     holder.tv_subCategoryDetails_MRPValue.setText("₹"+entry.getProductOrMrpPrice()); 
     holder.tv_subCategoryDetails_OrPrice.setText("₹"+entry.getProductOrPrice()); 

     holder.tv_subCategoryDetails_productId.setText(entry.getProductId()+""); 
     holder.tv_subCategoryDetails_productDescription.setText(entry.getProductDescription()); 
     holder.tv_subCategoryDetails_productCategoryId.setText(entry.getCategoryId()); 
     holder.tv_subCategoryDetails_productSubCategoryId.setText(entry.getSubCategoryId()); 
     holder.tv_subCategoryDetails_productStoreId.setText(entry.getStoreId()+""); 

     float save = entry.getProductOrMrpPrice()-entry.getProductOrPrice();    
     holder.tv_subCategoryDetails_productName.setTypeface(App_VolleyExamples.tf_roboto_regular); 
     holder.tv_subCategoryDetails_productSize.setTypeface(App_VolleyExamples.tf_roboto_regular); 
     holder.tv_subCategoryDetails_OrPrice.setTypeface(App_VolleyExamples.tf_roboto_medium); 
     holder.tv_subCategoryDetails_MRPValue.setTypeface(App_VolleyExamples.tf_roboto_light); 
     holder.tv_subCategoryDetails_MRPTitle.setTypeface(App_VolleyExamples.tf_roboto_light); 

     if(product.get(position).isFirstRowSelected()) 
     { 
      holder.r1.setVisibility(View.GONE); 
      holder.r2.setVisibility(View.VISIBLE); 
     } 
     else 
     { 
      holder.r1.setVisibility(View.VISIBLE); 
      holder.r2.setVisibility(View.GONE); 
     } 
     holder.r1.setTag("first"); 
     holder.r1.setTag(holder.r1.getId(),position); 
     holder.r2.setTag(holder.r2.getId(),position); 
     holder.r1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       // TODO Auto-generated method stub 
       if (v.getTag().toString().equals("first")){ 
        product.get(position).setFirstRowSelected(true); 
        int indx = (Integer) v.getTag(v.getId()); 
         View v1=(View) v.getParent(); 
         RelativeLayout Image=(RelativeLayout) v1.findViewById(R.id.relativeLayoutPlist2); 
         Log.i("visible", "first "+v.getVisibility()); 
         if(v.getVisibility() == View.VISIBLE){ 
          ObjectAnimator animation = ObjectAnimator.ofFloat(v, "rotationY", 0.0f, 90f); 
          animation.setDuration(3600); 
          animation.setInterpolator(new AccelerateDecelerateInterpolator()); 
          animation.start(); 
          v.setVisibility(View.GONE); 
          ObjectAnimator animation1 = ObjectAnimator.ofFloat(Image, "rotationY", -90.0f, 0.0f); 
          animation.setDuration(3600); 
          animation1.start(); 
          animation.setInterpolator(new AccelerateDecelerateInterpolator()); 
          Image.setVisibility(View.VISIBLE); 
         } 
       }  

      } 

     }); 
     holder.r2.setTag("second"); 
     holder.r2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (v.getTag().toString().equals("second")){ 
        product.get(position).setFirstRowSelected(false); 
        int indx = (Integer) v.getTag(v.getId()); 
         View v1=(View) v.getParent(); 
         RelativeLayout Image=(RelativeLayout) v1.findViewById(R.id.relativeLayoutPlist1); 
         Log.i("visible", "second "+v.getVisibility()); 
         if(v.getVisibility() == View.VISIBLE){ 
          ObjectAnimator animation = ObjectAnimator.ofFloat(v, "rotationY", 0.0f, 360f); 
          animation.setDuration(3600); 
          animation.setInterpolator(new AccelerateDecelerateInterpolator()); 
          animation.start(); 
           v.setVisibility(View.GONE); 
           ObjectAnimator animation1 = ObjectAnimator.ofFloat(Image, "rotationY", -90.0f, 0.0f); 
           animation.setDuration(3600); 
           animation1.start(); 
           animation.setInterpolator(new AccelerateDecelerateInterpolator()); 
           Image.setVisibility(View.VISIBLE); 

         } 
       }     
      } 

     }); 

     return view; 
    } 


    }; 

ответ

3
  Mange selected row value in object as defined below. 

      Add below things in "EntrySubCategoryDetails" class. 
      private boolean isFirstRowSelected; 

      // define getter setter here. 
      public boolean getFirstRowSelected() 
       { 
        return isFirstRowSelected; 
       } 

       public void setFirstRowSelected(boolean isFirstRowSelected) 
       { 
        this.isFirstRowSelected= isFirstRowSelected; 
       } 

    In Adapter make below changes. 

    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if (v.getTag().toString().equals("first")){ 
    product.get(position).setFirstRowSelected(true); 
    int indx = (Integer) v.getTag(v.getId()); 
    View v1=(View) v.getParent(); 
    RelativeLayout Image=(RelativeLayout)v1.findViewById(R.id.relativeLayoutPlist2); 
     Log.i("visible", "first "+v.getVisibility()); 
     if(v.getVisibility() == View.VISIBLE){ 
     v.setVisibility(View.GONE); 
     Image.setVisibility(View.VISIBLE); 
           } 

         }else if (v.getTag().toString().equals("second")){ 
          product.get(position).setFirstRowSelected(false); 
          int indx = (Integer) v.getTag(v.getId()); 
           View v1=(View) v.getParent(); 
           RelativeLayout Image=(RelativeLayout) v1.findViewById(R.id.relativeLayoutPlist1); 
           Log.i("visible", "second "+v.getVisibility()); 
           if(v.getVisibility() == View.VISIBLE){ 
            v.setVisibility(View.GONE); 
            Image.setVisibility(View.VISIBLE); 
           } 
         } 

        } 
    ===================== 
In getview() add below code. 
{ 
    if(product.get(position).getFirstRowSelected) 
    { 
    holder.r1.setVisibility(View.VISIBLE); 
    holder.r2.setVisibility(View.GONE); 
    } 
    else 
    { 
    holder.r1.setVisibility(View.GONE); 
    holder.r2.setVisibility(View.VISIBLE); 
    } 
} 
+0

Спасибо за быстрый ответ Урваши. Он решил мою проблему. – Vignesh

+0

Я добавил флип-анимацию, чтобы скрыть и показать представления в том же коде выше, но я получаю ту же самую проблему. Обновленный класс адаптера @ Урваши Хирани – Vignesh

0

удалите строки

holder.r1.setTag (holder.r1.getId(), должность) ;

holder.r2.setTag (holder.r2.getId(), position);

тогда код будет

holder.r1.setTag ("первый");

holder.r1.setOnClickListener (this);

holder.r2.setTag ("second");

holder.r2.setOnClickListener (this);

в функции OnClick изменить код, как

@Override

общественной ничтожной OnClick (View V) {

if (v.getTag().toString().equals("first")) { 

    View v1=(View) v.getParent(); 
    RelativeLayout r1=(RelativeLayout) v1.findViewById(R.id.relativeLayoutPlist1); 
    RelativeLayout r2=(RelativeLayout)v1.findViewById(R.id.relativeLayoutPlist2); 

    if (r1.getVisibility() == View.VISIBLE) { 
     r1.setVisibility(View.GONE); 
     r2.setVisibility(View.VISIBLE); 
    } 

} else if (v.getTag().toString().equals("second")) { 

     View v1=(View) v.getParent(); 
     RelativeLayout r1=(RelativeLayout) v1.findViewById(R.id.relativeLayoutPlist1); 
     RelativeLayout r2=(RelativeLayout) v1.findViewById(R.id.relativeLayoutPlist2); 

     if (r2.getVisibility() == View.VISIBLE) { 
      r2.setVisibility(View.GONE); 
      r1.setVisibility(View.VISIBLE); 
      } 

}

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