使用CWAC-Merge使用TextView和ExpendableListView使LinearLayout可滚动

时间:2014-04-08 17:00:02

标签: android expandablelistview expandablelistadapter commonsware-cwac

我的问题是,我在TextView下面有一个大ExpandableListView和一个TextView的活动。不幸的是TextView的文字太长了,所以当我展开ExpandableListView时我无法看到细节。

所以我的问题是,如何在我的代码中使用CWAC-Merge来获得一个可滚动的大型活动。

我想,从scrollView1到bottom的activity_contact.xml中的所有内容都是可滚动的!我已经将必要的libs(merge-1.0.1.jar和sacklist-1.0.0.jar)复制到我的Project中,但我不知道如何应用MergeAdapter。我真的很感激,如果有人能告诉我在哪里以及如何使用MergeAdapter!

这是我的activity_contact.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/titlebar" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        android:fillViewport="true" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/contact"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:text="@string/contact"
                    android:textSize="@dimen/textSize" />
            </TableRow>


            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <ExpandableListView
                    android:id="@+id/listView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" >
                </ExpandableListView>
            </TableRow>
        </TableLayout>
    </ScrollView>
</LinearLayout>

这是我的Contact.java类:

public class Contact extends Activity {

    private SparseArray<Group> groups = new SparseArray<Group>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
        createData();
        ExpandableListAdapter adapter = new ExpandableListAdapter(this, groups,
                false, null);
        listView.setAdapter(adapter);
    }

    private void createData() {
        Group group1 = new Group(getString(R.string.stringGroup));
        group1.children.add(getString(R.string.stringGroup2));
        groups.append(0, group1);
    }
}

这是我的ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private final SparseArray<Group> groups;
    private LayoutInflater inflater;
    private Activity activity;
    private boolean setOnClickListener;
    private Class<?> onClickClass;

    public ExpandableListAdapter(Activity act, SparseArray<Group> groups,
            boolean setOnClickListener, Class<?> onClickListenerClass) {
        activity = act;
        this.groups = groups;
        inflater = act.getLayoutInflater();
        this.setOnClickListener = setOnClickListener;
        this.onClickClass = onClickListenerClass;

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groups.get(groupPosition).children.get(childPosition);
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String children = (String) getChild(groupPosition, childPosition);
        TextView text = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listrow_details, null);
        }

        text = (TextView) convertView.findViewById(R.id.copyrightTextView);
        text.setText(Html.fromHtml(children));


        if (setOnClickListener) {
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, onClickClass);
                    intent.putExtra("name", children);
                    activity.startActivity(intent);
                }
            });
        }
        return convertView;
    }

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

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

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

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listrow_group, null);
        }
        Group group = (Group) getGroup(groupPosition);
        ((CheckedTextView) convertView).setText(group.string);
        ((CheckedTextView) convertView).setChecked(isExpanded);
        return convertView;
    }

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

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

listrow_details.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:clickable="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/copyrightTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="15dp"
        android:gravity="center_vertical"
        android:linksClickable="true"
        android:padding="10dp"
        android:textStyle="bold" >
    </TextView>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/black" />

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

MergeAdapterListAdapter,而不是ExpandableListAdapter。您无法使用MergeAdapter来解决问题。欢迎您阅读源代码MergeAdapter,并尝试确定是否有办法创建MergeExpandableListAdapter或类似的东西。

或者,在TextView之后设置一个没有很长ExpandableListView的用户界面。