【问题标题】:Android expandable list view with two Text views带有两个文本视图的 Android 可扩展列表视图
【发布时间】:2014-09-08 22:00:37
【问题描述】:

我正在尝试创建一个带有两个单独文本视图的自定义可扩展列表视图。一个文本视图需要左对齐,另一个右对齐。谁能帮帮我。

这是我的布局文件。

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

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:text="@string/click"
        android:textColor="@color/Red" />

</LinearLayout>

这是我的适配器类

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);
        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);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

这是我的主要活动

public class HomeFragment extends Fragment {

    public HomeFragment(){}
    TextView Username;
    TextView Acct, Paid, Email;
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    List<String> horse;
    List<String> owner ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        expListView = (ExpandableListView) rootView.findViewById(R.id.list);

        // preparing list data
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Rider and Trainer Info");
        listDataHeader.add("Horse and Owner Info");

        // Adding child data
        horse = new ArrayList<String>();
        horse.add("horse");

        owner = new ArrayList<String>();




        listDataChild.put(listDataHeader.get(0), horse); // Header, Child data
        listDataChild.put(listDataHeader.get(1), owner);

        Context context = getActivity().getApplicationContext();
        listAdapter = new ExpandableListAdapter(context, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
        final GlobalClass globalVariable = (GlobalClass) getActivity().getApplicationContext();
        String Name = globalVariable.getName();
        String acct = globalVariable.getUsersid();
        String paid = globalVariable.getPaid();
        String email = globalVariable.getEmail();

        Username = (TextView)rootView.findViewById(R.id.textView2);
        Acct = (TextView)rootView.findViewById(R.id.textView4);
        Paid = (TextView)rootView.findViewById(R.id.textView3);
        Email = (TextView)rootView.findViewById(R.id.textView5);
        Username.setText("Username: "+ Name);
        Acct.setText("Acct # "+ acct);
        Paid.setText("Expired On: "+ paid);
        Email.setText("Email: " + email);
        return rootView;
    }
}

我希望输出布局在每一行都像这样。

示例文本日期

【问题讨论】:

  • 我知道如何对齐文本视图。但我怎么能以编程方式。使用更新两个文本视图的数组加载列表视图。
  • 对不起,我想我听不懂你。确实,我不认为理解这个问题。查看您的代码,您已经在更新一个 TextView。你为什么不以同样的方式更新另一个? TextView 日期 = (TextView) convertView .findViewById(date); date.setText(childText);
  • 您好,感谢您的帮助。我添加了那行代码。但它不起作用我很确定我必须添加不止一个 linebof 代码。但我不确定在哪里添加其他代码。

标签: java android expandablelistview


【解决方案1】:

只需从您的布局中找到另一个视图

    @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);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        TextView lblListDate = (TextView) convertView
                .findViewById(R.id.lblListDate);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(date);

        return convertView;
    }

【讨论】:

    【解决方案2】:

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:padding="10dp"
        android:orientation="horizontal"
        android:weightSum="2">
    
        <TextView
            android:id="@+id/lblListItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="lblListItem"
            android:textSize="17sp" />
    
        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Test"
            android:textSize="17sp" />
    
    </LinearLayout>
    

    【讨论】:

    • 我知道如何对齐文本视图。但我怎么能以编程方式。使用更新两个文本视图的数组加载列表视图。
    【解决方案3】:

    如果您改为使用 RelativeLayout。您可以使用 layout_align(left/right) xml 属性将 TextViews 对齐到左/右。

    这样

    android:layout_alignRight = "true"
    

    【讨论】:

    • 我知道如何对齐文本视图。但我怎么能以编程方式。使用更新两个文本视图的数组加载列表视图。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多