将 onclicklistener 添加到片段中的线性布局
adding onclicklistener to linearlayout in fragment
我有一个片段,用作我的活动之间的导航菜单。
每个图标都在一个单独的线性布局中,有自己的 ID。我正在尝试在每个线性布局的片段中实现一个 onclick 侦听器,以便在单击它时打开适当的 activity 并更改线性布局的背景以及 imageview 中的图像以表示点击。但是,当我单击线性布局时,什么也没有发生。我已经将 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();
}
});
}
首先,您应该创建自定义按钮。参见 this。
并为 图标 .
设置 onClickListener
问题出在这行代码
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;
}
我有一个片段,用作我的活动之间的导航菜单。
每个图标都在一个单独的线性布局中,有自己的 ID。我正在尝试在每个线性布局的片段中实现一个 onclick 侦听器,以便在单击它时打开适当的 activity 并更改线性布局的背景以及 imageview 中的图像以表示点击。但是,当我单击线性布局时,什么也没有发生。我已经将 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();
}
});
}
首先,您应该创建自定义按钮。参见 this。 并为 图标 .
设置onClickListener
问题出在这行代码
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;
}