我有三种布局: 1. calculate_actionbar_layout.xml 2. purchasingmanager_actionbar_layout.xml 3. usermanagement_layout.xml
当我从操作栏中选择一个选项卡时,此选项卡的相关布局与操作栏中的项目显示。我想要处理动作栏中的项目事件。例如,当我点击加号图标进行操作时。
这是MainActivity:
public class MainActivity extends FragmentActivity implements
TabListener,OnPageChangeListener,OnClickListener {
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager=(ViewPager)findViewById(R.id.pager);
viewpager.setAdapter(new customefragmentAdapterPager(getSupportFragmentManager()));
viewpager.setOnPageChangeListener(this);
ActionBar actionbar = getActionBar();
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setDisplayHomeAsUpEnabled(false);
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(R.layout.usermanagement_layout);
ImageView i=(ImageView) getActionBar().getCustomView().findViewById(R.id.imageviewadduser);
i.setOnClickListener(this);
ActionBar.Tab tab1=actionbar.newTab();
tab1.setTabListener(this);
tab1.setCustomView(R.layout.custom_tab);
View v=tab1.getCustomView();
TextView t=(TextView)v.findViewById(R.id.tab_title);
t.setText("محاسبه");
ActionBar.Tab tab2=actionbar.newTab();
tab2.setText("خریدها");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionbar.newTab();
tab3.setText("کاربران");
tab3.setTabListener(this);
actionbar.addTab(tab1);
actionbar.addTab(tab2);
actionbar.addTab(tab3);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
final ActionBar actionbar = getActionBar();
switch (tab.getPosition()) {
case 0:
actionbar.setCustomView(R.layout.calculate_actionbar_layout);
break;
case 1:
actionbar.setCustomView(R.layout.purchasingmanager_actionbar_layout);
break;
case 2:
actionbar.setCustomView(R.layout.usermanagement_layout);
break;
default:
break;
}
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
int d=id;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == R.id.imageviewadduser){
// ( 1 ) add a new item
// ( 2 ) change add to remove
}
else{
// if a the new item is clicked show "Toast" message.
}
return super.onOptionsItemSelected(item);
}
}
所有布局都有此模板:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#0d93d2"
android:weightSum="4"
android:gravity="center"
android:layout_gravity="fill_horizontal"
tools:context="MainActivity"
>
<ImageView
android:id="@+id/imageviewusermanagment"
android:layout_width="0sp"
android:layout_weight="2.5"
android:layout_height="wrap_content"
android:src="@drawable/usermanagment"
android:scaleType="fitStart"
android:paddingLeft="20sp"
/>
<ImageView
android:id="@+id/imageviewdeleteuser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/deleteuser"
/>
<ImageView
android:id="@+id/imageviewedituser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/edituser"/>
<ImageView
android:id="@+id/imageviewadduser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/adduser"
/>
</LinearLayout>
和具有不同图标的所选标签的图像,我想处理它们的事件。您可以在以下链接中看到它们:
答案 0 :(得分:1)
1-更改此功能:
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
final ActionBar actionbar = getActionBar();
View cView=null;
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
switch (tab.getPosition()) {
case 0:
cView = getLayoutInflater().inflate(R.layout.calculate_actionbar_layout, null);
cView.setLayoutParams(param);
actionbar.setCustomView(cView);
//actionbar.setCustomView(R.layout.calculate_actionbar_layout);
break;
case 1:
cView = getLayoutInflater().inflate(R.layout.purchasingmanager_actionbar_layout, null);
cView.setLayoutParams(param);
actionbar.setCustomView(cView);
//actionbar.setCustomView(R.layout.purchasingmanager_actionbar_layout);
break;
case 2:
cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);
cView.setLayoutParams(param);
actionbar.setCustomView(cView);
//actionbar.setCustomView(R.layout.usermanagement_layout);
break;
default:
break;
}
viewpager.setCurrentItem(tab.getPosition());
}
2-添加android:onClick =“onClick”到布局中的每个imageview 3-改变对此的主动性 查看userView = getLayoutInflater()。inflate(R.layout.usermanagement_layout,null);
ImageView adduser=(ImageView) userView.findViewById(R.id.imageviewadduser);
adduser.setOnClickListener(this);
ImageView deleteuser=(ImageView) userView.findViewById(R.id.imageviewdeleteuser);
deleteuser.setOnClickListener(this);
ImageView edituser=(ImageView) userView.findViewById(R.id.imageviewedituser);
edituser.setOnClickListener(this);