2017-10-20 108 views
0

我從數據庫中使用適配器和內容佈局使用這些元素和4個按鈕從網格視圖加載一些元素(textview/imageview)。我試圖做的是當用戶點擊其中一個時,用相對佈局隱藏這4個按鈕。在我的xml中,我已經將相對佈局的可見性默認設置爲GONE。以編程方式將可見性狀態更改爲可見。 它工作正常。當我點擊一個按鈕時,會顯示一個對話框,此對話框的確定​​按鈕將相對佈局可見性更改爲可見(如我的代碼所示)。問題是每次我滾動gridview時,可見性消失。
請問我做錯了什麼?RelativeLayout在滾動Gridview的變化的可見性

XML(gridview的模型)

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="2dp"> 


<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/layout_grid" 
    android:background="@drawable/title_back" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true"> 

<TextView 
    android:layout_below="@+id/criteria_pic" 
    android:id="@+id/criteria_text" 
    android:textColor="#000" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="List of criteria" 
    android:textAlignment="center" 
    android:textSize="12dp" 
    android:textStyle="bold"/> 

<ImageView 
    android:id="@+id/criteria_pic" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:foregroundGravity="center" 
    android:src="@mipmap/ic_launcher" 
    android:layout_margin="4dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:id="@+id/rank_layout" 
     android:layout_margin="2dp" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:layout_below="@+id/criteria_text" 
     android:layout_centerHorizontal="true"> 

     <ImageButton 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:id="@+id/btn1" 
      android:clickable="true" 
      android:background="#fff" 
      android:layout_marginRight="4dp" 
      android:foregroundGravity="center" 
      android:src="@drawable/rank_btn1" 
      android:scaleType="fitCenter" 
      android:layout_below="@+id/criteria_text" 
      android:layout_centerHorizontal="true" /> 
     <ImageButton 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:id="@+id/btn2" 
      android:background="#fff" 
      android:layout_marginRight="4dp" 
      android:foregroundGravity="center" 
      android:src="@drawable/rank_btn2" 
      android:scaleType="fitCenter" 
      android:layout_below="@+id/criteria_text" 
      android:layout_centerHorizontal="true" /> 
     <ImageButton 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:id="@+id/btn3" 
      android:background="#fff" 
      android:layout_marginRight="4dp" 
      android:foregroundGravity="center" 
      android:src="@drawable/rank_btn4" 
      android:scaleType="fitCenter" 
      android:layout_below="@+id/criteria_text" 
      android:layout_centerHorizontal="true" /> 
     <ImageButton 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:id="@+id/btn4" 
      android:background="#fff" 
      android:layout_marginRight="4dp" 
      android:foregroundGravity="center" 
      android:src="@drawable/rank_btn5" 
      android:scaleType="fitCenter" 
      android:layout_below="@+id/criteria_text" 
      android:layout_centerHorizontal="true" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="gone" 
     android:background="@android:color/holo_green_light" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:id="@+id/check_view" 
     android:layout_alignBottom="@+id/rank_layout" 
     android:layout_alignTop="@+id/rank_layout"> 

     <ImageView 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:foregroundGravity="top|right" 
      app:srcCompat="@drawable/check_ok" 
      android:id="@+id/check_image" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" /> 
    </RelativeLayout> 


</RelativeLayout> 

適配器

public class GridviewAdapter extends BaseAdapter { 

Context c; 

RelativeLayout RL; 
ArrayList<criteria> Critere; 
LayoutInflater inflater; 


boolean clicked1=false; 
boolean clicked2=false; 
boolean clicked3=false; 
boolean clicked4=false; 


private int count = 0; 

public GridviewAdapter(Context c, ArrayList<criteria> critere) { 
    this.c = c; 
    Critere = critere; 
    inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 




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

@Override 
public Object getItem(int position) { 
    return Critere.get(position); 
} 

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

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

    View v; 
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE); 
    v = inflater.inflate(R.layout.gridview_model, parent,false); 

     TextView nameTxt = v.findViewById(R.id.criteria_text); 
     ImageView image = v.findViewById(R.id.criteria_pic); 

    final RelativeLayout checked = v.findViewById(R.id.check_view); 
    final LinearLayout ranked = v.findViewById(R.id.rank_layout); 


    final String name = Critere.get(position).getName(); 

    criteria cr = Critere.get(position); 

     nameTxt.setText(cr.getName()); 
     PicassoClient.downloadImage(c, cr.getImageurl(),image); 

     final ImageButton btn1 = v.findViewById(R.id.btn1); 
     final ImageButton btn2 = v.findViewById(R.id.btn2); 
     final ImageButton btn3 = v.findViewById(R.id.btn3); 
     final ImageButton btn4 = v.findViewById(R.id.btn4); 

     btn1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(final View view) { 

       clicked1=true; 
       // inflate alert dialog xml 
       LayoutInflater li = LayoutInflater.from(c); 
       View dialogView = li.inflate(R.layout.custom_dialog_rank, null); 

       AlertDialog.Builder dialog = new AlertDialog.Builder(c); 

       dialog.setView(dialogView); 
       dialog.setIcon(R.drawable.btn_press_rank1); 
       dialog.setCancelable(false); 
       dialog.setTitle(R.string.crit1); 
       dialog.setMessage("The " + name + " was very unsatisfiying"); 
       final EditText userInput = (EditText) dialogView 
         .findViewById(R.id.comment_field); 

       dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         checked.setVisibility(View.VISIBLE); 
         ranked.setVisibility(View.INVISIBLE); 

        } 
       }) 
         .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           checked.setVisibility(View.GONE); 
           ranked.setVisibility(View.VISIBLE); 
          } 
         }); 

       final AlertDialog alert = dialog.create(); 
       alert.show(); 
      } 
     }); 

     btn2.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       clicked2=true; 
       // inflate alert dialog xml 
       LayoutInflater li = LayoutInflater.from(c); 
       View dialogView = li.inflate(R.layout.custom_dialog_rank, null); 

       AlertDialog.Builder dialog = new AlertDialog.Builder(c); 

       dialog.setIcon(R.drawable.btn_press_rank2); 
       dialog.setView(dialogView); 
       dialog.setCancelable(false); 
       dialog.setTitle(R.string.crit2); 
       dialog.setMessage("The " + name + " was unsatisfiying"); 
       final EditText userInput = (EditText) dialogView 
         .findViewById(R.id.comment_field); 

       dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 

         checked.setVisibility(View.VISIBLE); 
         ranked.setVisibility(View.INVISIBLE); 
        } 
       }) 
         .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           //   alert.cancel(); 
           checked.setVisibility(View.INVISIBLE); 
           ranked.setVisibility(View.VISIBLE); 
          } 
         }); 

       final AlertDialog alert = dialog.create(); 
       alert.show(); 

      } 
     }); 

     btn3.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       clicked3=true; 

       // inflate alert dialog xml 
       LayoutInflater li = LayoutInflater.from(c); 
       View dialogView = li.inflate(R.layout.custom_dialog_rank, null); 

       AlertDialog.Builder dialog = new AlertDialog.Builder(c); 

       dialog.setIcon(R.drawable.btn_press_rank4); 
       dialog.setView(dialogView); 
       dialog.setCancelable(false); 
       dialog.setTitle(R.string.crit3); 
       dialog.setMessage("The " + name + " was satisfiying"); 
       final EditText userInput = (EditText) dialogView 
         .findViewById(R.id.comment_field); 

       dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 

         checked.setVisibility(View.VISIBLE); 
         ranked.setVisibility(View.INVISIBLE); 
        } 
       }) 
         .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           //   alert.cancel(); 
           checked.setVisibility(View.INVISIBLE); 
           ranked.setVisibility(View.VISIBLE); 
          } 
         }); 

       final AlertDialog alert = dialog.create(); 
       alert.show(); 

      } 
     }); 

     btn4.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       clicked4=true; 

       // inflate alert dialog xml 
       LayoutInflater li = LayoutInflater.from(c); 
       View dialogView = li.inflate(R.layout.custom_dialog_rank, null); 

       AlertDialog.Builder dialog = new AlertDialog.Builder(c); 

       dialog.setIcon(R.drawable.btn_press_rank5); 
       dialog.setView(dialogView); 
       dialog.setCancelable(false); 
       dialog.setTitle(R.string.crit4); 
       dialog.setMessage("The " + name + " was very satisfiying"); 
       final EditText userInput = (EditText) dialogView 
         .findViewById(R.id.comment_field); 

       dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 

         checked.setVisibility(View.VISIBLE); 
         ranked.setVisibility(View.INVISIBLE); 
        } 
       }) 
         .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           //   alert.cancel(); 
           checked.setVisibility(View.INVISIBLE); 
           ranked.setVisibility(View.VISIBLE); 
          } 
         }); 

       final AlertDialog alert = dialog.create(); 
       alert.show(); 

      } 
     }); 

    return v; 

}} 

主要活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_criteria); 
    new CriteriaActivity.FetchCountTask().execute(); 

    final GridView gv = (GridView) findViewById(R.id.gv); 


    toolbar = (Toolbar) findViewById(R.id.toolbar3); 
    setSupportActionBar(toolbar); 

    // get the references of buttons 
    btnSelectDate=(Button)findViewById(R.id.buttonSelectDate); 
    btnSelectTime=(Button)findViewById(R.id.buttonSelectTime); 

    // Set ClickListener on btnSelectDate 
    btnSelectDate.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      // Show the DatePickerDialog 
      showDialog(DATE_DIALOG_ID); 
     } 
    }); 

    // Set ClickListener on btnSelectTime 
    btnSelectTime.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      // startFeedTime(); 
      // Show the TimePickerDialog 
      showDialog(TIME_DIALOG_ID); 
     } 
    }); 

      new Downloader_review(CriteriaActivity.this, urlAddress, gv).execute(); 
       } 

     // Register DatePickerDialog listener 
private DatePickerDialog.OnDateSetListener mDateSetListener = 
     new DatePickerDialog.OnDateSetListener() { 
      // the callback received when the user "sets" the Date in the DatePickerDialog 
      public void onDateSet(DatePicker view, int yearSelected, 
            int monthOfYear, int dayOfMonth) { 
       year = yearSelected; 
       month = monthOfYear + 1; 
       day = dayOfMonth; 
       // Set the Selected Date in Select date Button 
       btnSelectDate.setText("Date selected : "+day+"/"+month+"/"+year); 
      } 
     }; 

// Register TimePickerDialog listener 
private TimePickerDialog.OnTimeSetListener mTimeSetListener = 
     new TimePickerDialog.OnTimeSetListener() { 
      // the callback received when the user "sets" the TimePickerDialog in the dialog 
      public void onTimeSet(TimePicker view, int hourOfDay, int min) { 
       hour = hourOfDay; 
       minute = min; 
       // Set the Selected Date in Select date Button 
       btnSelectTime.setText("Time selected : "+hour+":"+minute); 
      } 
     }; 


// Method automatically gets Called when you call showDialog() method 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case DATE_DIALOG_ID: 
      // create a new DatePickerDialog with values you want to show 
      date = true; 
      DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); 
      dialog.getDatePicker().setMaxDate(System.currentTimeMillis()); 

      return dialog; 
      // return new DatePickerDialog(this, 
      //   mDateSetListener, 
      //   mYear, mMonth, mDay); 

     // create a new TimePickerDialog with values you want to show 
     case TIME_DIALOG_ID: 
      time = true; 
      return new TimePickerDialog(this, 
        mTimeSetListener, mHour, mMinute, false); 

    } 
    return null; 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main_menu, menu); 

    // Get the notifications MenuItem and 
    // its LayerDrawable (layer-list) 

    MenuItem item = menu.findItem(R.id.action_notifications); 
    LayerDrawable icon = (LayerDrawable) item.getIcon(); 
    // BitmapDrawable iconBitmap = (BitmapDrawable) item.getIcon(); 
    // LayerDrawable icon = new LayerDrawable(new Drawable [] { iconBitmap }); 

    // Update LayerDrawable's BadgeDrawable 
    Utils.setBadgeCount(this, icon, mNotificationsCount); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == R.id.action_notifications) { 
     // TODO: display unread notifications. 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/* 
Updates the count of notifications in the ActionBar. 
*/ 
private void updateNotificationsBadge(int count) { 
    mNotificationsCount = count; 

    // force the ActionBar to relayout its MenuItems. 
    // onCreateOptionsMenu(Menu) will be called again. 
    invalidateOptionsMenu(); 
} 

/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
public Action getIndexApiAction() { 
    Thing object = new Thing.Builder() 
      .setName("Main Page") // TODO: Define a title for the content shown. 
      // TODO: Make sure this auto-generated URL is correct. 
      .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
      .build(); 
    return new Action.Builder(Action.TYPE_VIEW) 
      .setObject(object) 
      .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
      .build(); 
} 


/* 
Sample AsyncTask to fetch the notifications count 
*/ 
class FetchCountTask extends AsyncTask<Void, Void, Integer> { 

    @Override 
    protected Integer doInBackground(Void... params) { 
     // example count. This is where you'd 
     // query your data store for the actual count. 
     return 5; 
    } 

    @Override 
    public void onPostExecute(Integer count) { 
     updateNotificationsBadge(count); 
    } 
} 

回答

0

嘗試RecyclerView而不是GridView。您可以使用StaggeredGridLayoutManager獲得相同的網格效果。你也將有更多的控制每個項目