【问题标题】:how to set footer at bottom of expandablelist?如何在可扩展列表底部设置页脚?
【发布时间】:2018-10-11 08:05:19
【问题描述】:

我使用了可扩展列表,我想将其页脚设置在屏幕底部,但它不起作用。我附上了它的代码和截图。

 <ExpandableListView
        android:layout_alignParentBottom="true"
        android:id="@+id/list_slidermenu"
        android:layout_width="260dp"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:background="@color/colorAccent"
        android:choiceMode="singleChoice"
        android:divider="@color/colorPrimary"
        android:dividerHeight="1dp"
        android:groupIndicator="@null"
        android:listSelector="@drawable/list_selector" >
    </ExpandableListView>

expListView = (ExpandableListView) findViewById(R.id.list_slidermenu);
        TextView textView = new TextView(this);
        textView.setText("footer");
        expListView.addFooterView(textView);

已编辑

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<String>> _listDataChild;

        public ExpandableListAdapter(Context context,
                                     List<String> listDataHeader,
                                     HashMap<String, List<String>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }

        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(
                    this._listDataHeader.get(groupPosition))
                    .get(childPosititon);
        }

        @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) {

            final String childText = (String) getChild(groupPosition,
                    childPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }

            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);

            txtListChild.setText(childText);
            Drawable drawable = getDrawable(R.drawable.ic_menu_gallery);
            txtListChild.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return this._listDataChild.get(
                    this._listDataHeader.get(groupPosition)).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return this._listDataHeader.get(groupPosition);
        }

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

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }
            // changing the +/- on expanded list view
            TextView txt_plusminus = (TextView) convertView
                    .findViewById(R.id.plus_txt);
            if (isExpanded) {
                txt_plusminus.setText("-");
                Drawable plusDrawable = getDrawable(android.R.drawable.ic_menu_more);
                plusDrawable.setBounds(0, 0, 0, 0);             txt_plusminus.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.btn_dropdown, 0, 0, 0);
                txt_plusminus.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, plusDrawable, null);
//                txt_plusminus.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,plusDrawable,null);
            } else {
                txt_plusminus.setText("+");
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            // lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);
            lblListHeader.setTextColor(getColor(R.color.black));

            // nav drawer icons from resources
            // navMenuIcons =
            // getResources().obtainTypedArray(R.array.nav_drawer_icons);
            // imgListGroup.setImageDrawable(navMenuIcons.getDrawable(groupPosition));

            // adding icon to expandable list view
            ImageView imgListGroup = (ImageView) convertView
                    .findViewById(R.id.ic_txt);
            imgListGroup.setImageResource(SubMenuActivity.icon[groupPosition]);
            return convertView;
        }

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

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

【问题讨论】:

  • Footer 视图将添加到列表的最后一项之后。所以这是addFooterView() 的预期输出。如果您需要 View 始终存在于底部,您可以使用 View 并在 ListView 之外的底部对齐。或者使用Footer View 方法,您可以让您ListView 填充高度,使其外观相同。

标签: android expandablelistview footer


【解决方案1】:

您可以使用以下代码在列表中添加页脚:

View v= inflator.inflate(R.layout.your_view, null);
expandableList.addFooterView(v);

【讨论】:

  • 请同时设置适配器,您可以通过该适配器查看页脚视图。
  • expandableList.setAdapter(new ExpanadableListAdapter());
  • 我也添加了它的适配器,还是同样的问题
  • 请注意,您用于页脚视图的布局不应是空布局;只需为您需要的任何高度添加一个视图。
【解决方案2】:

查看以下代码:

公共类 MainActivity 扩展 AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandableListView list = findViewById(R.id.list_slidermenu);

        TextView v = new TextView(this);
        v.setText("Footer");
        list.addFooterView(v);
        list.setAdapter(new ExpanadableListAdapter());
    }

    class ExpanadableListAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return 0;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 0;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

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

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

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

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

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            return null;
        }

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

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 2012-04-02
    • 1970-01-01
    • 2012-10-31
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多