0

У меня есть фрагмент, который я использую в качестве навигационного меню между моими действиями.Добавление onclicklistener в linearlayout во фрагменте

enter image description here

Каждый из значков в отдельном LinearLayout со своим ID. Я пытаюсь внедрить прослушиватель onclick в фрагменте для каждого linearlayout, чтобы при нажатии на него открывается соответствующая активность, а фон линейного макета, а также изображение в изображении просматриваются, чтобы обозначить щелчок. Однако, когда я нажимаю на linearlayout, ничего не происходит. Я уже установил атрибут clickable в true, так что это не проблема. Это мой код:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_menu, container, false); 
    } 

// TODO: Rename method, update argument and hook method into UI event 
public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
    LinearLayout betslayout = (LinearLayout) this.getView().findViewById(R.id.betnowlayout); 
    betslayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LinearLayout layout = (LinearLayout) v; 
      layout.setBackgroundResource(R.drawable.selected); 
      ImageView image = (ImageView) layout.findViewById(R.id.betnowimageview); 
      image.setImageResource(R.drawable.footballsell); 
      LinearLayout parent = (LinearLayout) v.getParent(); 
      LinearLayout mybetslayout = (LinearLayout) parent.findViewById(R.id.viewbetslayout); 
      LinearLayout rankingslayout = (LinearLayout) parent.findViewById(R.id.rankingslayout); 
      LinearLayout coinfrenzylayout = (LinearLayout) parent.findViewById(R.id.coinfrenzylayout); 
      LinearLayout shoplayout = (LinearLayout) parent.findViewById(R.id.shoplayout); 
      mybetslayout.setBackgroundResource(R.drawable.notselected); 
      rankingslayout.setBackgroundResource(R.drawable.notselected); 
      coinfrenzylayout.setBackgroundResource(R.drawable.notselected); 
      shoplayout.setBackgroundResource(R.drawable.notselected); 
      ImageView mybetsimage = (ImageView) mybetslayout.findViewById(R.id.betnowimageview); 
      ImageView rankingsimage = (ImageView) rankingslayout.findViewById(R.id.rankingsimageview); 
      ImageView coinfrenzyimage = (ImageView) coinfrenzylayout.findViewById(R.id.coinfrenzyimageview); 
      ImageView shopimage = (ImageView) shoplayout.findViewById(R.id.shopimageview); 
      mybetsimage.setImageResource(R.drawable.chipp); 
      rankingsimage.setImageResource(R.drawable.medal); 
      coinfrenzyimage.setImageResource(R.drawable.clover); 
      shopimage.setImageResource(R.drawable.moneybag); 
      Intent i = new Intent(getActivity(),AllGameslistActivity.class); 
      startActivity(i); 
      getActivity().finish(); 
     } 
    }); 
} 
+0

Когда 'onButtonPressed «позвонил? – tachyonflux

+0

Это не кнопка, ее линейная компоновка, мне еще нужно это назвать? – Alk

ответ

0

Проблема была в этой строке кода

return inflater.inflate(R.layout.fragment_menu, container, false);

Это должно быть вместо этого: return myfragment;

Это правильный способ сделать это:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View myfragment = inflater.inflate(R.layout.fragment_menu, container, false); 


    // TODO: Rename method, update argument and hook method into UI event 

    LinearLayout betslayout = (LinearLayout) myfragment.findViewById(R.id.betnowlayout); 
    betslayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d("isitworkign", "yesyes"); 
      LinearLayout layout = (LinearLayout) v; 
      layout.setBackgroundResource(R.drawable.selected); 
      ImageView image = (ImageView) layout.findViewById(R.id.betnowimageview); 
      image.setImageResource(R.drawable.footballsell); 
      LinearLayout parent = (LinearLayout) v.getParent(); 
      LinearLayout mybetslayout = (LinearLayout) parent.findViewById(R.id.viewbetslayout); 
      LinearLayout rankingslayout = (LinearLayout) parent.findViewById(R.id.rankingslayout); 
      LinearLayout coinfrenzylayout = (LinearLayout) parent.findViewById(R.id.coinfrenzylayout); 
      LinearLayout shoplayout = (LinearLayout) parent.findViewById(R.id.shoplayout); 
      mybetslayout.setBackgroundResource(R.drawable.notselected); 
      rankingslayout.setBackgroundResource(R.drawable.notselected); 
      coinfrenzylayout.setBackgroundResource(R.drawable.notselected); 
      shoplayout.setBackgroundResource(R.drawable.notselected); 
      ImageView mybetsimage = (ImageView) parent.findViewById(R.id.viewbetsimageview); 
      ImageView rankingsimage = (ImageView) parent.findViewById(R.id.rankingsimageview); 
      ImageView coinfrenzyimage = (ImageView) parent.findViewById(R.id.coinfrenzyimageview); 
      ImageView shopimage = (ImageView) parent.findViewById(R.id.shopimageview); 
      mybetsimage.setImageResource(R.drawable.chipp); 
      rankingsimage.setImageResource(R.drawable.medal); 
      coinfrenzyimage.setImageResource(R.drawable.clover); 
      shopimage.setImageResource(R.drawable.moneybag); 
      Intent i = new Intent(getActivity(), AllGameslistActivity.class); 
      startActivity(i); 
      getActivity().finish(); 
     } 
    }); 
    return myfragment; 
} 
0

Прежде всего, вы должны создавать пользовательские кнопки. См. this. и onClickListener для Иконки.

+0

Я не хочу менять свой дизайн, чтобы использовать что-либо еще, чем то, что я сейчас использую, я просто хочу заставить работать onclicklistener, который, как я знаю, возможен – Alk

Смежные вопросы