onTouch()方法取消了groupItems的ExpandableView的效果

时间:2016-08-11 09:10:55

标签: android expandablelistview ontouchlistener ontouch

我有一个名为CustomAdapter的类,方法getGroupView()来自它。我有一个ExpandableListView我想做的是:当我点击一个组项时,它显示子项,就像一个ExpandableListView会行动;如果我按下一个组项目更长的时间,它会打开一个不同的活动。     问题是,由于OnTouchListener,现在它不会在点击时显示子项,只有在较长的时间内,它才会访问不同的活动。 我的解决方案是在OnTouch()方法中返回false,但它不起作用。有什么想法吗?

注意:我是Android开发中的新手。谢谢:d

@Override

public View getGroupView(int groupPosition, boolean isExpanded, View convertview, ViewGroup parent) {
    final ViewGroup getAct = parent;
    if(convertview == null)
        convertview = inflater.inflate(R.layout.explistgroupview, null);
    TextView textView = (TextView) convertview.findViewById(R.id.groupTextView);
    textView.setText(tables.get(groupPosition));
    ImageView imageView = (ImageView) convertview.findViewById(R.id.groupImageView);
    imageView.setImageResource(R.drawable.plus_sign);



    convertview.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch(motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startTime = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_UP:
                    long time = System.currentTimeMillis() - startTime;
                    duration += time;

                    if(duration >= MAX_DURATION) {
                        Context context = getAct.getContext();
                        i = new Intent(context, beer.class);
                        context.startActivity(i);

                    }
                    else {
                        return false;
                    }


            }
            duration = 0;
            return true;
        }
    });
    return convertview;

}

2 个答案:

答案 0 :(得分:0)

我在你的问题上有两个选择。

1,

case MotionEvent.ACTION_UP:
                long time = System.currentTimeMillis() - startTime;
                duration += time;
                if(duration <= MIN_DURATION){
                    // TODO: do onClick()
                    return true;
                }

                if(duration >= MAX_DURATION) {
                    Context context = getAct.getContext();
                    i = new Intent(context, beer.class);
                    context.startActivity(i);

                }
                else {
                    return false;
                }

2,

convertview.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                long time = System.currentTimeMillis() - startTime;
                duration += time;

                if(duration >= MAX_DURATION) {
                    Context context = getAct.getContext();
                    i = new Intent(context, beer.class);
                    context.startActivity(i);

                }
                else {
                    return false;
                }


        }
        duration = 0;

        //next line has change
        return false;
    }
});

你可以尝试一下,但我不确定它是否可以正常工作。

答案 1 :(得分:0)

getChildView:

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    tabelOrdersOnEachTable = (ArrayList<String>) tableOrders.get(groupPosition);
    TextView textView = null;
    if (convertView == null)
        convertView = inflater.inflate(R.layout.explistchildview, null);
textView = (TextView) convertView.findViewById(R.id.textView1);
    textView.setText(tabelOrdersOnEachTable.get(childPosition));

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("ChildClick", "Merge");
            Toast.makeText(activity, "Clicked on: " + tabelOrdersOnEachTable.get(childPosition), Toast.LENGTH_LONG).show();
        }
    });


    return convertView;
}