3

我正在尝试制作一个可扩展的列表视图,其中父组上有一个按钮,按下该按钮时将允许用户将项目添加到子组。但是我在添加按钮时遇到问题,父下拉子列表功能停止工作。(理想情况下,单击按钮会添加一个元素,单击其他任何地方会导致菜单下拉)我复制了一个可扩展的列表模板并在尝试其他任何操作之前让它工作。

<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout android:id="@+id/groupname" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="40dip">
     <TextView android:id="@+id/tvGroupName" 
        android:layout_width="225dp" 
        android:layout_height="40dip" 
        android:textSize="16sp" 
        android:textStyle="bold" 
        android:paddingLeft="30dip" 
        android:gravity="center_vertical">
     </TextView>

     <Button
     android:id="@+id/addPhrase"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="+" />

</LinearLayout>

这就是父组的 xml(我全部更改)。

设置展开器和折叠器的 onCreate 方法。(组和儿童已预先填充)

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Drawable d = getResources().getDrawable(R.drawable.group_indicator);

    expList = getExpandableListView();
    metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    width = metrics.widthPixels;
    //this code for adjusting the group indicator into right side of the view
    expList.setIndicatorBounds(width - GetDipsFromPixel(50), width -   GetDipsFromPixel(10));
    expList.setGroupIndicator(d);
    expList.setAdapter(new ExpAdapter(this));

expList.setOnGroupExpandListener(new OnGroupExpandListener()
{
@Override
public void onGroupExpand(int groupPosition) 
{
 Log.e("onGroupExpand", "OK");
}
});

expList.setOnGroupCollapseListener(new OnGroupCollapseListener()
{
@Override
public void onGroupCollapse(int groupPosition) 
{
 Log.e("onGroupCollapse", "OK");
}
});

expList.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
  int groupPosition, int childPosition, long id) {
 Log.e("OnChildClickListener", "OK");
 return false;
}
});
}

提前致谢。

4

1 回答 1

0

我遇到了同样的问题,当我按下按钮时它终于起作用了(在你的情况下,addPhrase,因为不可聚焦。

这是我在 ExpandableListAdapter 的 getGroupView 中的代码:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, final 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_pathologies_header, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListPathologiesHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    lblListHeader.setCompoundDrawablePadding(30);
    lblListHeader.setPadding(20, 0, 0, 0);
    lblListHeader.setCompoundDrawablesWithIntrinsicBounds(isExpanded ? R.drawable.up50 : R.drawable.down50, 0, 0, 0);


    FloatingActionButton fabDrug = (FloatingActionButton) convertView.findViewById(R.id.fab_drug);
    fabDrug.setFocusable(false);
    if (isExpanded) {
        fabDrug.setVisibility(View.VISIBLE);
        fabDrug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewDrugDialogFragment newFragment = NewDrugDialogFragment.newInstance(_patientId);
                newFragment.show(((Activity) _context).getFragmentManager(), "Title");
            }
        });
    } else {
        fabDrug.setVisibility(View.GONE);
    }


    return convertView;
}

它工作正常,现在组崩溃和扩展应该。

于 2017-05-15T21:49:18.487 回答