【问题标题】:Hide button in ExpandableListViewExpandableListView 中的隐藏按钮
【发布时间】:2023-03-06 04:21:01
【问题描述】:

我刚刚获得了 ExpandableListView 设置,到目前为止一切正常。在组/父级上,我有一个 TextView 和 Button。该列表的目的是让人们对应用程序中包含的不同声音进行采样,然后他们单击按钮,然后声音将保存到 SD 卡中。这是我目前所拥有的链接:http://imgur.com/djSCIrG

我的问题是,是否有可能在有人单击按钮并选择购买包后,是否可以仅隐藏一个按钮而不是每个组中的所有按钮。

这是我的主要布局(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;
        }
    }
}

我将不胜感激有关此事的任何指导。谢谢

【问题讨论】:

    标签: android button hide expandablelistview


    【解决方案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: 我完全忽略了子视图使用相同布局的事实(呃!脑筋急转弯 :))。

    您仍然可以使用共享首选项来跟踪已下载的样本(您可以为此使用 Set)。您还需要创建一种方法来为每个样本分配“标识符”。从那里您所要做的就是在每次调用 getChildView() 时执行检查,如果 Set 包含选定的样本标识符,则将按钮可见性设置为消失。这应该注意在未下载示例时显示按钮,并且在下载示例时不显示按钮。在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);
    }
    

    【讨论】:

    • 是的,在 getGroupView 的上述类中,我使用的是 buypackbutton.setVisibility(View.INVISIBLE)。但是当我展开列表或退出并返回屏幕时,按钮会重新出现。
    • 感谢您到目前为止的帮助。如果它不在 ExpandableListView 中,这将起作用。如果您查看第二个 XML 文件,它就是每一行的组成部分。所以我没有3个单独的按钮。这只是一个按钮。因此,如果我改变它的可见性,它们就会全部消失。
    • 不客气。对不起。我忽略了这一点。我再次更新了我的答案。我对 BaseExpandableListAdapter 不是很熟悉,如果我错了,请纠正我。每次展开列表项时,都会调用 getChildView,对吗?如果是这样,那么每次展开列表项时,您应该能够检查是否已下载示例(如在我的示例中)并相应地设置按钮的可见性。
    • 再次感谢。我在想我要从组中移动按钮,也许把它放在底部,这样它就不会包含在 ExpandableList 中。同样,因为当我更改它的可见性时它只是一个按钮,所以其他组中的所有按钮都会受到我所知道的影响。如果您有兴趣,那么我从 ExpandableListView 的这个示例中得到了很多用处 - en.wikicode.org/index.php/Custom_ExpandableListView
    • 啊,我明白你在说什么。对于那个很抱歉。希望我有一个更好的答案。感谢您的链接!最美好的祝愿!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多