安卓ExbandableListView含按鈕點擊事件的應用實例

實例情況:ExbandableListView的父項帶一個或兩個按鈕,點擊實現相應功能。思路:在自定義的ExbandableListView適配器中的getGroupView()方法中,獲取按鈕控件,定義點擊監聽,在監聽方法中通過Intent跳轉至下一個Activity。

@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    // 取得用於顯示給定分組的視圖。 這個方法僅返回分組的視圖對象, 要想獲取子元素的視圖對象,
    // 就需要調用 getChildView(int, int, boolean, View, ViewGroup)。
    View v;
    if (convertView == null) {
        v = newGroupView(isExpanded, parent);
    } else {
        v = convertView;
    }
    bindGroupView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
    ImageView del = (ImageView) v.findViewById(R.id.b_delete);
    ImageView update = (ImageView) v.findViewById(R.id.b_update);
    final TextView tv = (TextView) v.findViewById(R.id.groupTextView);
    int a = tv.getText().toString().trim().indexOf(".");
    final String id = tv.getText().toString().trim().substring(0,a);
    del.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Resource r = new Resource() ;
            r.delete(id);
            notifyDataSetChanged();
            Intent intent = new Intent(mInflater.getContext(),Resource.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//因不是在Activity中跳轉,加這句即可
            notifyDataSetInvalidated();
            mInflater.getContext().startActivity(intent);

        }
    });
    update.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mInflater.getContext(),Update_resource.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("uid",id);
            mInflater.getContext().startActivity(intent);
        }
    });
    return v;
}

You May Also Like