0

我有一个带有自定义适配器的列表视图,它扩展了 Baseadapter。在列表视图的每一行中,我都有一个文本视图和一个按钮,默认情况下,该按钮处于不可见状态。我在 Adapter 的 getView 方法中调用 view.OntouchListener,当该行被滑动时,我在该位置使按钮可见。当另一行被刷卡时,其他行中的所有按钮都应该变得不可见,除了当前位置一个。

任何帮助表示赞赏。

谢谢,普拉桑斯。

4

1 回答 1

0

I think you have to keep the visibility state of a button in adapter. Below you I added an example (not tested yet) about how to show the button when the text is clicked (any other buttons will be hidden).

import com.test.data.ExportOption;

public class ExportOptionsAdapter extends BaseAdapter{

// Constants
private final static String TAG = "ExportOptionsAdapter";

// Data
private ArrayList<ExportOption> mItems;
private Context mContext; 
private LayoutInflater mLf;
private ListView mParent;

public ExportOptionsAdapter(Context pContext) {
    mItems = new ArrayList<ExportOption>(); 
    mContext = pContext;
    mLf = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public ExportOptionsAdapter add(ExportOption pOption){
    mItems.add(pOption);
    return this;
}

public ExportOptionsAdapter add(ArrayList<ExportOption> exportOptions) {
    mItems.addAll(exportOptions);
    return this;
}

public int getCount() {
    return mItems.size();
}

public ExportOption getItem(int position) {
    return mItems.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder vh;

    if(null == mParent){
        mParent = (ListView) parent;
    }

    if(null == convertView){
        convertView = (View)mLf.inflate(R.layout.export_option, parent, false);
        vh = new ViewHolder();
        vh.option = (TextView)convertView.findViewById(R.id.option_name);
        vh.btn = (Button)convertView.findViewById(R.id.btn);


        convertView.setTag(vh);
    } else {
        vh = (ViewHolder)convertView.getTag();
    }

    vh.option.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = mParent.getPositionForView(v);
            if(AdapterView.INVALID_POSITION != position){                   
                for(int i = 0; i < mItems.size(); i++){
                    if(i == position){
                        // mark the selected one as visible
                        mItems.get(position).markButtonAsVisible(true);
                    } else {
                        // hide the other items
                        mItems.get(position).markButtonAsVisible(false);
                    }
                }
            }
        }
    });

    vh.option.setText(mItems.get(position).getType());

    if(mItems.get(position).buttonMarkedAsVisible()){
        vh.btn.setVisibility(View.VISIBLE);
    } else {
        vh.btn.setVisibility(View.GONE);
    }
    return convertView;
}

private class ViewHolder{
    public TextView option;
    public Button btn;
}   

}

于 2013-08-07T08:18:23.393 回答