在我的应用中,我使用Toolbar
。现在我的工具栏里面有两个ImageViews
。当用户点击任何ImageView时,我会打开不同的Fragment
或Activity
,但就我而言,我无法点击任何ImageView。
*。XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_home"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@drawable/background"
android:elevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_home_about_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="@drawable/about" />
<ImageView
android:id="@+id/iv_home_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/iv_home_about_us"
android:layout_marginRight="10dp"
android:src="@drawable/search" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<TextView
style="@style/text_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="05dp"
android:gravity="center"
android:text="Trending around me" />
<com.techmorphosis.wheretoday.SlidingTabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"></android.support.v4.view.ViewPager>
</LinearLayout>
完整代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_screen, container, false);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (!bundle.isEmpty()) {
position = bundle.getInt(FROM_WHERE_ERROR_OCCURED);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
latitude = SettingPreffrences.getLatitude(contextFrag);
longitude = SettingPreffrences.getLongitude(contextFrag);
if (latitude.equals("") && longitude.equals("")) {
commonFunctions.isLocationAvailable();
commonFunctions.replaceFragment(getActivity(), new ErrorFrag().newInstance(100, 0), false);
} else {
initialize();
}
}
private void initialize() {
toolbar = (Toolbar) view.findViewById(R.id.toolbar_home);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
// actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
// actionBar.setDisplayShowTitleEnabled(false);
ivAboutUs = (ImageView) toolbar.findViewById(R.id.iv_home_about_us);
ivSearch = (ImageView) toolbar.findViewById(R.id.iv_home_search);
pager = (ViewPager) view.findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));
tabLayout = (SlidingTabLayout) view.findViewById(R.id.tabs);
tabLayout.setDistributeEvenly(true);
tabLayout.setSelectedIndicatorColors(Color.TRANSPARENT);
tabLayout.setViewPager(pager);
if (position == 0) {
pager.setCurrentItem(0, true);
} else if (position == 1) {
pager.setCurrentItem(1, true);
} else if (position == 2) {
pager.setCurrentItem(2, true);
}
ivAboutUs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(contextFrag, "Yess", Toast.LENGTH_SHORT).show();
}
});
ivSearch.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_home_about_us:
commonFunctions.replaceFragment(getActivity(), new AboutUsFrag(), false);
break;
case R.id.iv_home_search:
Intent intent = new Intent(contextFrag, SearchScreen.class);
contextFrag.startActivity(intent);
break;
}
}
}
class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new TodayFrag();
}
if (position == 1) {
fragment = new TommorowFrag();
}
if (position == 2) {
fragment = new LaterFrag();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
答案 0 :(得分:0)
您无法访问onCreateView
方法中的UI元素 - EVER
使用onActivityCreated
方法,告诉片段活动已完全创建并准备好进行交互。
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ImageView ivAboutUs = (ImageView) view.findViewById(R.id.iv_home_about_us);
ivAboutUs .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
答案 1 :(得分:0)
这可能是触摸事件永远不会到达ImageViews的问题,因为它们被其他东西吸收了。我碰到了类似的东西,我试图在嵌入在列表项中的Imageview上放置onClickListener。无论我多少点击了imageView,其他东西都在吃掉触摸事件(很可能是构成ListView项目的视图。我通过将ImageView更改为ImageButton并将其背景设置为null来解决问题(所以imageButton看起来像是一个imageView而不是一个图像按钮。)一旦我这样做,点击图像就可以了。尝试将ImageViews改为:
<ImageButton
android:id="@+id/iv_home_about_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/about" />
<ImageView
android:id="@+id/iv_home_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/iv_home_about_us"
android:layout_marginRight="10dp"
android:background="@null"
android:src="@drawable/search" />
我希望这会有所帮助。祝好运。