列表视图点击该项目下方可见的另一个视图,另一个点击它可以折叠

时间:2017-07-29 10:04:19

标签: android listview

我正在使用Listview当我点击Listview的一个项目而另一个视图展开时,当我点击另一个项目之前点击的项目崩溃时 我想我选择的位置是真的,休息是假的我使用布尔类型列表它像扩展列表视图一样工作

这里是我执行的适配器类代码,单击

public class CurrentAdapter extends BaseAdapter {
    private Context context;
    private static LayoutInflater inflater=null;
    private ArrayList<Boolean> booleanList;
    private  boolean show=false;
    private String lastSelectPos;

    public CurrentAdapter(Context context) {
        this.context=context;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        booleanList = new ArrayList<Boolean>(10);
        for(int i=0; i<10; i++){
            booleanList.add(false);
        }
    }

    @Override
    public int getCount() {
        return booleanList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        final LinearLayout linearLayoutTop,linearLayoutBottom;
        view = inflater.inflate(R.layout.row_item_cuurent, viewGroup, false);
        linearLayoutTop = (LinearLayout) view.findViewById(R.id.ll_current);
        linearLayoutBottom = (LinearLayout) view.findViewById(R.id.ll_current_sub);
        linearLayoutTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(lastSelectPos==null){
                    lastSelectPos = String.valueOf(position);
                }else {
                    Integer lastPos = Integer.parseInt(lastSelectPos);
                    if(lastPos!=position){
                        booleanList.set(lastPos, false);
                        notifyDataSetChanged();
                    }
                }
                booleanList.set(position, !booleanList.get(position));
                notifyDataSetChanged();
            }
        });

        if(booleanList.get(position)){
            linearLayoutBottom.setVisibility(View.VISIBLE);
        }else {
            linearLayoutBottom.setVisibility(View.GONE);
        }

        return view;
    }
}

0 个答案:

没有答案