【问题标题】:Multiple click events on expandablelistview's group elementexpandablelistview 的组元素上的多次点击事件
【发布时间】:2016-05-06 07:25:49
【问题描述】:

我有两个关于 groupView(不是子)上可展开列表视图点击事件的问题

  1. 当我的 GroupView 中有孩子时,我可以使用下面的 sn-p 获取 groupPosition

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            Log.d("Clicked on item: ", ""+groupPosition);
            return false;
        }
    });
    

但当我没有孩子时,不幸的是,“MyApp”在鼠标点击时已停止。在这种情况下,如何获取 groupPosition?

  1. 我的可展开列表视图的 groupView 布局中有两个按钮。现在我需要为两个按钮注册两个不同的点击事件。这个 groupView 也没有孩子。我在 SO 中进行了彻底的搜索,但我对此一无所知。

        <LinearLayout
            android:layout_below="@id/qtd_prc_cont"
            android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center">
            <Textview
                android:layout_width="wrap_content" android:layout_height="wrap_content"  android:id="@+id/group_text" android:text="Group 1" />
            <Button
                android:id="@+id/btn_reject"
                android:layout_width="90dp" android:layout_height="30dp" android:text="Reject" android:textAllCaps="false" android:background="@drawable/reject_button_styles"
                android:layout_marginRight="60dp" android:textColor="#a1a2a1" android:textSize="13dp"/>
            <Button
                android:id="@+id/btn_accept"
                android:layout_width="90dp" android:layout_height="30dp" android:text="Accept" android:textAllCaps="false" android:background="@drawable/button_radius"
                android:textColor="@color/colorWhite" android:textSize="13dp"/>
        </LinearLayout>
    

这是我的适配器类:

public class OfferedTrucksAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<QuotedListGroup> groups;

public OfferedTrucksAdapter(Context context, ArrayList<QuotedListGroup> groups) {
    this.context = context;
    this.groups = groups;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<QuotedListItem> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);
}

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

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

    QuotedListItem child = (QuotedListItem) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.activity_quoted_list_item, null);
    }
    TextView container_label = (TextView) convertView.findViewById(R.id.container);
    container_label.setText(child.getContainer());

    TextView truck_number = (TextView) convertView.findViewById(R.id.truck_number);
    truck_number.setText(child.getTruckNumber().toString());

    TextView truck_weight = (TextView) convertView.findViewById(R.id.truck_weight);
    truck_weight.setText(child.getTruckWeight().toString());

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<QuotedListItem> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    QuotedListGroup group = (QuotedListGroup) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.activity_offered_trucks_group, null);
    }

    if(groupPosition%2 == 0) {
        convertView.findViewById(R.id.offered_trucks_group).setVisibility(View.VISIBLE);
        convertView.findViewById(R.id.offered_price_group).setVisibility(View.GONE);

        TextView destination = (TextView) convertView.findViewById(R.id.offered_destination);
        destination.setText(group.getDestination());

        TextView trucks = (TextView) convertView.findViewById(R.id.offered_trucks);
        trucks.setText(group.getTrucks() + "");

        TextView date_label = (TextView) convertView.findViewById(R.id.offered_date_label);
        date_label.setText(group.getDateLabel().toString());

        TextView material = (TextView) convertView.findViewById(R.id.offered_material);
        material.setText(group.getMaterial().toString());

        TruckaroIconView see_hide_icon = (TruckaroIconView) convertView.findViewById(R.id.offered_see_hide_icon);
        TextView see_hide_label = (TextView) convertView.findViewById(R.id.offered_see_hide_label);
        if(isExpanded) {
            see_hide_label.setText("Hide Trucks");
            see_hide_icon.setRotation(180);
        } else {
            see_hide_label.setText("See Trucks");
            see_hide_icon.setRotation(0);
        }
    } else {
        convertView.findViewById(R.id.offered_trucks_group).setVisibility(View.GONE);
        convertView.findViewById(R.id.offered_price_group).setVisibility(View.VISIBLE);

        TextView offeredPrice = (TextView) convertView.findViewById(R.id.offered_price);
        offeredPrice.setText(group.getOfferedPrice());

        TextView quotedPrice = (TextView) convertView.findViewById(R.id.quoted_price);
        quotedPrice.setText(group.getQuotedPrice());

    }
    return convertView;
}

protected static class RowViewHolder {
    public Button buttonReject, buttonAccept;
}

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

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

}

基于接受/拒绝按钮点击,我需要通过意图将 Textview 的数据传递给下一个活动。

这是我的可扩展列表视图的样子:

在上图中,有四个列表项。 1号和3号有孩子,但2号和4号没有孩子。当我单击第二个元素接受按钮时,我需要将第一个元素数据传递给下一个活动。我无法在这些按钮上设置点击事件。

请帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 当您的应用崩溃时,您收到了什么错误?
  • @McAwesomville 我没有收到任何错误。它只是回到以前的活动。当我将孩子添加到同一组时,它工作正常。可展开列表视图的组元素不能为空(没有子元素)吗?
  • 什么是不幸的是'MyApp'已停止
  • 对不起。这有点令人困惑。据我了解,当特定的 groupView 没有 childViews 时,您在检索 groupPosition 值时遇到问题?
  • @McAwesomville 没有任何错误,它只是留下消息不幸的是,我的应用程序已停止。(仅供参考“我的应用程序”是我的应用程序名称)

标签: android expandablelistview expandablelistadapter


【解决方案1】:

好的。如果我正确理解您的担忧,您的目标是传递上项的元素(在您的屏幕截图中,如果单击 Accept 按钮,则会检索上项的值并通过intent 传递给另一个活动。

这是我得到的:

@Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        QuotedListGroup group = (QuotedListGroup) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.activity_offered_trucks_group, null);
        }

        if (groupPosition % 2 == 0) {

            // Removed some code here.

        } else {
            convertView.findViewById(R.id.offered_trucks_group).setVisibility(View.GONE);
            convertView.findViewById(R.id.offered_price_group).setVisibility(View.VISIBLE);

            TextView offeredPrice = (TextView) convertView.findViewById(R.id.offered_price);
            offeredPrice.setText(group.getOfferedPrice());

            TextView quotedPrice = (TextView) convertView.findViewById(R.id.quoted_price);
            quotedPrice.setText(group.getQuotedPrice());

            // Added code here ----

            // Initialize the buttons.
            Button btnAccept = (Button) convertView.findViewById( < insert_btn_id_here >);
            Button btnReject = (Button) convertView.findViewById( < insert_btn_id_here >);

            // Set onClickListeners for the buttons.
            btnAccept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    /*
                    Modify with the object you have, then pass the group position - 1
                    (double check this, you just need to pass the position of the upper item).
                    */
                    Object groupObject = getGroup(groupPosition - 1);

                    /*
                    Get the value from the groupObject that you want to include in the intent.
                    NOTE: I'm just using some sample codes, modify it to what you need.
                    */
                    String sampleStr = groupObject.destination.getText().toString();

                    /*
                    Intent stuff, I think you know this already.
                     */

                    Intent intent = new Intent(context, SampleActivity.class);
                    intent.putExtra("SAMPLE_KEY", sampleStr);
                    context.startActivity(intent);

                }
            });

            btnReject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // your code for reject button here.
                }
            });

            // Until here ----

        }

        return convertView;
    }

我只是在else 语句中为您的按钮(接受、拒绝)添加了侦听器。您需要将其修改为您需要的内容,因为我无法查看您的其余代码。如果您有任何问题,请告诉我。干杯! :D

【讨论】:

  • PS: 看看我的 cmets 里面的代码 sn-p。 ;)
  • 非常感谢您的帮助,我有疑问。现在我得到了 groupObject,但我怎样才能得到目标属性?使用 .(dot) 表示法,我收到此错误:找不到符号变量目标
  • 如我所见。您已经拥有 RowViewHolder 静态类。你可以利用它。看到你的适配器,我想你可以进一步优化它。这个post 看起来不错。在您设法使用ViewHolder 对象后,您可以像往常一样调用它:ViewHolder vh = getGroup(groupPosition - 1); 然后vh.destination.getText().toString();。类似的东西。祝你好运朋友。干杯! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
相关资源
最近更新 更多