【问题标题】:Android - Viewing a specific child per group in ExpandableListViewAndroid - 在 ExpandableListView 中查看每个组的特定子项
【发布时间】:2018-02-07 09:57:49
【问题描述】:

有没有办法在 ExpandableListView 中查看组内的特定子级,例如第一组的第三个子 layout,而不在组内按顺序显示子布局?与我遇到的其他 SO 帖子相比,我的情况非常复杂,以至于我的应用程序有一个 SQLite 数据库,允许用户在由 EditTexts 组成的每个组中添加任意数量的数据(组)以及可修改的数据(子组),因此,必须查看正确的子布局,以便将正确的数据设置为新会话的 EditTexts。但是,如下面的sample代码sn-ps所示,问题是设置getChildrenCount()返回1后为了查看特定的child,第一个childPosition , 或 0, 当我想查看某个组的特定子布局时,总是使用 getChildView() 作为索引来查看第一个子布局:

主活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.lvExp);
        List<String> groupHeadersList = new ArrayList<>();
        groupHeadersList.add("Header #1");
        groupHeadersList.add("Header #2");
        groupHeadersList.add("Header #3");
        List<String> childList = new ArrayList<>();
        childList.add("Child #1");
        childList.add("Child #2");
        childList.add("Child #3");
        MyExpandableListAdapter expandableListAdapter = new MyExpandableListAdapter(this,
                groupHeadersList, childList);
        expandableListView.setAdapter(expandableListAdapter);
    }
}

ExpandableListAdapter:

public class MyExpandableListAdapter implements ExpandableListAdapter {
    private static final String LOG_TAG = MyExpandableListAdapter.class.getSimpleName();
    private Context context;
    private List<String> groupHeadersList, childList;

    public MyExpandableListAdapter(Context context, List<String> groupHeadersList,
                                 List<String> childList) {
        this.context = context;
        this.groupHeadersList = groupHeadersList;
        this.childList = childList;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {}

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {}

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

    @Override
    public int getChildrenCount(int groupPos) {
        return 1; // One at most in order to view a specific child
    }

    @Override
    public Object getGroup(int groupPos) {
        return groupHeadersList.get(groupPos);
    }

    @Override
    public Object getChild(int groupPos, int childPos) {
        return 0;

        // return childList.get(groupPos); // OBVIOUS SOLUTION, BUT I WANT TO VIEW A CERTAIN CHILD LAYOUT INSTEAD
    }

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

    @Override
    public long getChildId(int groupPos, int childPos) {
        return childPos;
    }

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

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

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_group, parent, false);
        }

        String headerTitle = (String) getGroup(groupPosition);
        TextView headerTextView = (TextView) convertView.findViewById(R.id.lblListHeader);
        headerTextView.setText(headerTitle);

        return convertView;
    }

    @Override
    public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView,
                             ViewGroup parent) {

        Log.d(LOG_TAG, "groupPos is: " + groupPos);
        Log.d(LOG_TAG, "childPos is: " + childPos); // KEEPS RETURNING 0, THE FIRST CHILD AFTER RETURNING 1 FROM getChildrenCount()

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
        }

        String childTitle = (String) getChild(groupPos, childPos);
        TextView childTextView = (TextView) convertView.findViewById(R.id.lblListItem);
        childTextView.setText(childTitle);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPos, int childPos) {
        return true;
    }

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

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

    @Override
    public void onGroupExpanded(int groupPosition) {}

    @Override
    public void onGroupCollapsed(int groupPosition) {}

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }
}

... 膨胀的布局,list_group 和 list_item,只是 TextViews。如上所示,在 sn-ps 中,我知道 this sample 有一个解决方案,但我想知道如何为某个组引用特定的子布局,以便我的其他项目。

【问题讨论】:

  • 我想知道 ViewGroup 的 getChildAt(int index) 方法在这里是否有用......

标签: java android android-view expandablelistview expandablelistadapter


【解决方案1】:

在这里,您使用 R.layout.list_item 为 convertView 充气,这是一个列表视图 您需要使用只有一个文本视图的另一种布局

    @Override
    public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView,
                             ViewGroup parent) {

        Log.d(LOG_TAG, "groupPos is: " + groupPos);
        Log.d(LOG_TAG, "childPos is: " + childPos); // KEEPS RETURNING 0, THE FIRST CHILD AFTER RETURNING 1 FROM getChildrenCount()

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.third_list_item, parent, false);
        }

        String childTitle = (String) getChild(groupPos, 2);
        TextView childTextView = (TextView) convertView.findViewById(R.id.lblListItem);
        childTextView.setText(childTitle);

        return convertView;
    }

像这样的东西,应该可以工作。 请记住您正在膨胀的布局并使用 getChild() 方法获取所需的子节点并相应地继续

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多