Andy.Zhao Andy.Zhao - 1 month ago 12
Android Question

Why my onClickListener should double click?

I added onClickListener to NavLinearLayout which I wrote by extend LinearLayout,and everytime it should double click then the functions inside can work.But that function means it can work when click only once.Why?
This is my NavLinearLayout

public class NavLinearLayout extends LinearLayout{

public NavLinearLayout(Context context)
{
super(context);
}

public NavLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
// do whatever you want with the event
// and return true so that children don't receive it
return true;
}}


This is the MainActivity code:

boutiqueLL.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
setFlagTrue(Navigation.Boutique.getPosition());
setBtnLine();
}

});private void setFlagTrue(int position) {
sousuoFlag = position == 4 ? true : false;
dingyueFlag = position == 3 ? true : false;
fenleiFlag = position == 2 ? true : false;
boutiqueFlag = position == 1 ? true : false;
}private void setBtnLine() {
Log.d("test",(boutiqueFlag?"true":"false")+" : "
+(fenleiFlag?"true":"false")+" : "
+(dingyueFlag?"true":"false")+" : "
+(sousuoFlag?"true":"false"));
if (sousuoFlag) {
sousuoLine.setBackgroundColor(Color.RED);
ft = fm.beginTransaction();
ft.replace(R.id.main_page, new SearchFragment(mdb));
ft.addToBackStack(null);
ft.commit();

} else {
sousuoLine.setBackgroundColor(Color.argb(0, 0, 0, 0));
}
if (dingyueFlag) {
dingyueLine.setBackgroundColor(Color.RED);
ft = fm.beginTransaction();
ft.replace(R.id.main_page, new SubscribeFragment(mdb));
ft.addToBackStack(null);
ft.commit();
} else {
dingyueLine.setBackgroundColor(Color.argb(0, 0, 0, 0));
}
if (fenleiFlag) {
fenleiLine.setBackgroundColor(Color.RED);
ft = fm.beginTransaction();
ft.replace(R.id.main_page, new FenLeiFragment(hanlder,mdb));
ft.addToBackStack(null);
ft.commit();
} else {
fenleiLine.setBackgroundColor(Color.argb(0, 0, 0, 0));
}
if (boutiqueFlag) {
boutiqueLine.setBackgroundColor(Color.RED);
ft = fm.beginTransaction();
ft.replace(R.id.main_page, new BoutFragmentNew(mdb));
ft.commit();
} else {
boutiqueLine.setBackgroundColor(Color.argb(0, 0, 0, 0));
}

}

Answer

I used OnTouchListener instead of OnClickListener.It works,but I don't know why my OnClickListener needs double click now.