2013-02-12 93 views
3

我剛剛得到了一個ExpandableListView設置,目前一切正常。在組/父母我有一個TextView和和按鈕。列表的目的是讓人們對應用中包含的不同聲音進行採樣,然後他們單擊按鈕,然後將聲音保存到SD卡中。這裏有一個鏈接到我到目前爲止:http://imgur.com/djSCIrGExpandableListView中的隱藏按鈕

我的問題是,是否有可能在某人點擊按鈕並選擇購買包,如果可以隱藏只有一個按鈕,而不是所有的按鈕在每個組中。

這裏是我的主要佈局(expandablelistview_main.xml):

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

     <TextView 
      android:id="@+id/soundpacktitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/expandablelistview_main_soundpacktitle_topmargin" 
      android:layout_centerHorizontal="true" 
      android:text="@string/soundpacktitle" 
      android:textSize="@dimen/expandablelistview_main_soundpacktitle_textsize" /> 

     <ExpandableListView 
      android:id="@+id/soundpacklist" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_below="@+id/soundpacktitle" 
      android:layout_above="@+id/soundpackbottombar" 
      android:layout_marginTop="@dimen/expandablelistview_main_soundpacklist_topmargin" 
      android:transcriptMode="disabled" 
      android:cacheColorHint="#00000000" 
      android:listSelector="@android:color/transparent" /> 
    </RelativeLayout> 

這裏是我的羣/父佈局(expandablelistview_group.xml):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/grouptextview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical" 
     android:layout_marginLeft="@dimen/expandablelistview_group_grouptextview_leftmargin"   
     android:textSize="@dimen/expandablelistview_group_grouptextview_textsize" /> 

    <Button 
     android:id="@+id/buypackbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:layout_alignParentRight="true" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:text="@string/buypack" 
     android:padding="@dimen/expandablelistview_group_buypackbutton_padding" 
     android:textSize="@dimen/expandablelistview_group_buypackbutton_textsize" 
     android:textStyle="bold" /> 

</RelativeLayout> 

這裏是我的java類:

public class InAppSounds extends Activity { 
    private ExpandableListView soundpacklist; 

    private ArrayList<String> groups; 
    private ArrayList<ArrayList<ArrayList<String>>> childs; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.expandablelistview_main); 

     TextView soundpacktitle = (TextView) findViewById(R.id.soundpacktitle); 
     soundpacktitle.setTypeface(printbold); 

     // Declare the ExpandableListView and set's the indicator to the list arrows 
     soundpacklist = (ExpandableListView) findViewById(R.id.soundpacklist); 
     soundpacklist.setGroupIndicator(getResources().getDrawable(R.drawable.list_groupselector)); 

     LoadData(); 

     myExpandableAdapter adapter = new myExpandableAdapter(this, groups, childs); 
     soundpacklist.setAdapter(adapter); 
    } 

    // Loads the ExpandableListView with parent and children groups 
    private void LoadData() { 
     groups = new ArrayList<String>(); 
     childs = new ArrayList<ArrayList<ArrayList<String>>>(); 

     // String array that stores the parent and child names 
     String[] soundpackgroups = getResources().getStringArray(R.array.soundpackgroups); 
     String[] soundpack1 = getResources().getStringArray(R.array.soundpack1); 
     String[] soundpack2 = getResources().getStringArray(R.array.soundpack2); 
     String[] soundpack3 = getResources().getStringArray(R.array.soundpack3); 

     // First Sound Pack and their songs 
     groups.add(soundpackgroups[0]); 
     childs.add(new ArrayList<ArrayList<String>>()); 
     for (int a = 0; a < soundpack1.length; a++) { 
      childs.get(0).add(new ArrayList<String>()); 
      childs.get(0).get(a).add(soundpack1[a]); 
     } 

     // Second Sound Pack and their songs 
     groups.add(soundpackgroups[1]); 
     childs.add(new ArrayList<ArrayList<String>>()); 
     for (int a = 0; a < soundpack2.length; a++) { 
      childs.get(1).add(new ArrayList<String>()); 
      childs.get(1).get(a).add(soundpack2[a]); 
     } 

     // Third Sound Pack and their songs 
     groups.add(soundpackgroups[2]); 
     childs.add(new ArrayList<ArrayList<String>>()); 
     for (int a = 0; a < soundpack3.length; a++) { 
      childs.get(2).add(new ArrayList<String>()); 
      childs.get(2).get(a).add(soundpack3[a]); 
     } 
    } 

    public class myExpandableAdapter extends BaseExpandableListAdapter { 

     private final ArrayList<String> groups; 

     private final ArrayList<ArrayList<ArrayList<String>>> children; 

     private final Context context; 

     public myExpandableAdapter(Context context, ArrayList<String> groups, 
       ArrayList<ArrayList<ArrayList<String>>> children) { 
      this.context = context; 
      this.groups = groups; 
      this.children = childs; 
     } 

     @Override 
     public boolean areAllItemsEnabled() { 
      return true; 
     } 

     @Override 
     public ArrayList<String> getChild(int groupPosition, int childPosition) { 
      return children.get(groupPosition).get(childPosition); 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
      return childPosition; 
     } 

     @Override 
     public View getChildView(int groupPosition, final int childPosition, 
       boolean isLastChild, View convertView, ViewGroup parent) { 

      String child = getChild(groupPosition, childPosition).get(0); 

      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.expandablelistview_child, null); 
      } 

      // TypeFace variable for the PrintBold 
      printbold = Typeface.createFromAsset(getAssets(), "fonts/PrintBold.otf"); 

      TextView childtxt = (TextView) convertView.findViewById(R.id.childtextview); 
      childtxt.setTypeface(printbold); 
      childtxt.setText(child); 

      return convertView; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
      return children.get(groupPosition).size(); 
     } 

     @Override 
     public String getGroup(int groupPosition) { 
      return groups.get(groupPosition); 
     } 

     @Override 
     public int getGroupCount() { 
      return groups.size(); 
     } 

     @Override 
     public long getGroupId(int groupPosition) { 
      return groupPosition; 
     } 

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

      final String group = getGroup(groupPosition); 

      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.expandablelistview_group, null); 
      } 

      // TypeFace variable for the PrintBold 
      printbold = Typeface.createFromAsset(getAssets(), "fonts/PrintBold.otf"); 

      TextView grouptxt = (TextView) convertView.findViewById(R.id.grouptextview); 
      grouptxt.setTypeface(printbold); 
      grouptxt.setText(group); 

      final Button buypackbutton = (Button) convertView.findViewById(R.id.buypackbutton); 
      buypackbutton.setClickable(true); 

      buypackbutton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        AlertDialog.Builder alert = new AlertDialog.Builder(InAppSounds.this); 
        if (group.equals("Pack #1")) { 
         alert.setCancelable(false); 
         alert.setTitle(getString(R.string.buypacktitle)); 
         alert.setIcon(getResources().getDrawable(R.drawable.ic_audioicon)); 
         alert.setMessage(getString(R.string.buypackmsg)); 
         alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           // check to make sure the SD card is mounted 
           // if not display an AlertDialog 
           if (!isSDPresent()) { 
            sdcardalert(); 
           } 
           else { 
            // this will erase the button in all the groups, not just this group 
            buypackbutton.setVisibility(View.INVISIBLE); 
           } 
          } 
         }); 
         alert.setNegativeButton("No", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.cancel(); 
          } 
         }); 
         alert.show(); 
        } 
       } 
      }); 

      return convertView; 
     } 

     @Override 
     public boolean hasStableIds() { 
      return true; 
     } 

     @Override 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 
    } 
} 

我希望在這個問題上的任何指導。謝謝

回答

1

是的。這很容易。所有你需要做的就是獲得對你的按鈕的引用,並設置可見性消失。像這樣:

Button sampleButton = (Button) findViewById(R.id.sample_button); 
sampleButton.setVisiblity(View.GONE); 

注意:當您將其設置爲View.GONE時,最初給予它的佈局空間也會被刪除。如果您只想移除按鈕並保留佈局空間,請改用View.INVISIBLE。

編輯:下面是我將如何讓按鈕不再出現:首先,我將使用布爾值跟蹤活動處於活動狀態時按鈕的狀態。然後在你的覆蓋getChildView我會檢查這個布爾值並相應地設置可見性。也許插入這樣的事情到getChildView回調從單擊列表中的項目重新出現時保持按鈕:

if (!showButton) { 
Button button = (Button) findViewById(R.id.sample_button); 
button.setVisibility(View.GONE); 
} 

至於回來的畫面。要跟蹤是否不顯示按鈕,我會使用布爾值並將其存儲在SharedPreferences中。然後,在getChildView回調中,檢查布爾的狀態並相應地進行設置。類似這樣的:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
boolean showButtonStatusPref = settings.getBoolean("showButton", true); 

if(!showButtonStatusPref) { 
Button button = (Button) findViewById(R.id.sample_button); 
button.setVisibility(View.GONE); 
} 

您唯一需要做的其他事情就是管理每個按鈕的狀態。

編輯2:我完全忽略了同一個佈局用於兒童視圖(duh!brain cramp :))的事實。

您仍然可以使用共享偏好來跟蹤哪些樣本已下載(您可以使用Set進行此設置)。您還需要創建一種爲每個樣本分配「標識符」的方法。從那裏你所要做的就是每次調用getChildView()時執行一次檢查,並且如果集合包含選定的樣本標識符,則將按鈕可見性設置爲消失。當樣品未下載時,應該注意顯示按鈕,並且樣品已下載時不顯示按鈕。也許這樣的事情在getChildView()

Set<String> defaultSet = new SortedSet<String>(); 
defaultSet.add("Nothing downloaded"); 
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
SortedSet<String> listOfDowloaded = settings.getStringSet("isDownloadedList",); 

if (listOfDownLoaded.contains(sampleDownloadIdentifier)) { 
Button button = (Button) findViewById(R.id.some_id); 
button.setVisiblity(View.GONE); 
} 
+0

是在上面的類中getGroupView我使用buypackbutton.setVisibility(View.INVISIBLE)。但是,當我展開列表或出去返回到屏幕時,該按鈕會再次出現。 – joelreeves 2013-02-12 22:57:49

+0

好的。我舉了一個簡單的例子來說明如何完成這個任務。 – Don 2013-02-12 23:50:57

+0

感謝您的幫助。這將工作,如果它不在ExpandableListView。如果你看看每一行所包含的第二個XML文件。所以我沒有3個單獨的按鈕。這只是一個按鈕。所以如果我改變它的可見性,那麼它們都會消失。 – joelreeves 2013-02-13 03:40:54