【问题标题】:Expandable list view with image as view group header以图像作为视图组标题的可扩展列表视图
【发布时间】:2015-01-12 15:47:03
【问题描述】:

我有一个可展开的列表视图。视图组标题是一个图像这是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">


<ImageView
    android:id="@+id/lblListHeader"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
 />

</LinearLayout>

当我将 targetSdkVersion 设置为 20 时,图像会缩小,并且在 API 级别 17 的手机会遇到同样的问题。任何帮助将不胜感激。!

【问题讨论】:

  • 如果应用程序的目标 API 级别为 17 或更低,adjustViewBounds 将允许可绘制对象缩小视图边界,但在所有情况下都不会增长以填充可用的测量空间。这是为了与旧版 MeasureSpec 和 RelativeLayout 行为兼容。
  • 我希望 API 级别 >16。
  • stackoverflow.com/questions/10917388/…检查这个答案。希望它有帮助
  • 我在代码中尝试了 setadjustviewbounds(true) 但没有帮助。
  • 尝试将 ScaleType 设置为 FIT_CENTER

标签: android expandablelistview android-4.2-jelly-bean


【解决方案1】:

我参考关注post解决了

int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
    iv.getViewTreeObserver().removeOnPreDrawListener(this);
    finalHeight = iv.getMeasuredHeight();
    finalWidth = iv.getMeasuredWidth();
    tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
    return true;
}
});

【讨论】:

    【解决方案2】:

    列出 Group.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:textSize="17dp"
            android:textColor="#821418" />
    
    
    </LinearLayout>
    

    list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"  >
    
        <TextView
            android:id="@+id/lblListItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="17dip"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
    
    
    
    </LinearLayout>
    

    主要活动.java

     //Deifine the all variable for the expandable platform **//
         ExpandableListAdapter listAdapter;
         ExpandableListView expListView;
         List<String> listDataHeader;
         HashMap<String, List<String>> listDataChild;
    
      // get the listview
            expListView = (ExpandableListView) findViewById(R.id.lvExp);
    
         // preparing list data
            prepareListData();
    
            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
    
            // setting list adapter
            expListView.setAdapter(listAdapter);
    
         // Listview Group click listener
            expListView.setOnGroupClickListener(new OnGroupClickListener() {
    
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                    // Toast.makeText(getApplicationContext(),
                    // "Group Clicked " + listDataHeader.get(groupPosition),
                    // Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
    
         // Listview Group expanded listener
            expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
    
                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Expanded",
                            Toast.LENGTH_SHORT).show();
    
    
                }
            });
    
    
         // Listview Group collasped listener
            expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
    
                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Collapsed",
                            Toast.LENGTH_SHORT).show();
    
    
    
                }
            });
    
    
         // Listview on child click listener
            expListView.setOnChildClickListener(new OnChildClickListener() {
    
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),listDataHeader.get(groupPosition)+ " : "+
                    listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT)
                    .show();
     }
            });
    

    ExpandableListAdapter.java

    package com.example.lcofieldmanagement;
    
    import java.util.HashMap;
    import java.util.List;
    
    import android.content.Context;
    import android.graphics.Typeface;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.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 int getGroupCount() {
            // TODO Auto-generated method stub
            return this._listDataHeader.size();
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
             return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                        .size();
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return this._listDataHeader.get(groupPosition);
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .get(childPosition);
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
             return childPosition;
        }
    
        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            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);
            }
    
            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);
    
            return convertView;
        }
    
        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
             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);
                return convertView;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }
    
    }
    

    【讨论】:

    • 这很简单。我想问题是关于向它添加图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多