android 布局 viewstub 获取listview id 值问题
在android开发中,我用viewstub 模拟tab选项卡,我希望按下tab1选项卡后出现的是通过listview 获取的值 要怎么做的?public class MainActivity extends Activity implements OnItemClickListener{
private ListView mylv;
private Listapp listapp;
private ViewStub[] viewStub = new ViewStub[3];
private Button currentBtn;
private Button lastBtn;
private int[] tabBtnIds = {R.id.btn1, R.id.btn2,
R.id.btn3};
private Button[] tabBtn = new Button[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewStub [0] = (ViewStub) findViewById(R.id.btn1ViewStub);
viewStub [1] = (ViewStub) findViewById(R.id.btn2ViewStub);
viewStub [2] =(ViewStub) findViewById(R.id.btn3ViewStub);
currentBtn = (Button) findViewById(R.id.btn2);
TabBtnClickListener tabBtnListener = new TabBtnClickListener();
for(int i=0; i<tabBtnIds.length; i++) {
tabBtn[i] = (Button) findViewById(tabBtnIds[i]);
tabBtn[i].setOnClickListener(tabBtnListener);
}
listapp = new Listapp(this);
mylv = (ListView) findViewById(R.id.listbody);
mylv.setAdapter(listapp);
mylv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
PackageInfo info = (PackageInfo) listapp.getItem(position);
Toast.makeText( this,info.applicationInfo.packageName + " "
+ info.applicationInfo.loadLabel(getPackageManager()), Toast.LENGTH_SHORT).show();
}
class TabBtnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
lastBtn = currentBtn;
currentBtn = (Button) v;
if(currentBtn.getId() == lastBtn.getId()) {
return;
}
currentBtn.setBackgroundColor(Color.BLUE);
lastBtn.setBackgroundColor(Color.GRAY);
int currentIndex = -1;
switch(currentBtn.getId()) {
case R.id.btn1:
currentIndex = 0;
break;
case R.id.btn2:
currentIndex = 1;
break;
case R.id.btn3:
currentIndex = 2;
break;
}
for(int i=0; i<viewStub.length; i++) {
viewStub[i].setVisibility(View.INVISIBLE);
}
for(int i=0; i<viewStub.length; i++) {
if(currentIndex == -1) {
break;
}
if(currentIndex != i) {
viewStub[i].setVisibility(View.INVISIBLE);
} else {
viewStub[i].setVisibility(View.VISIBLE);
}
}
}
}
}
xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff">
<ViewStub
android:id="@+id/btn1ViewStub"
android:layout="@layout/btn1_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ViewStub
android:id="@+id/btn2ViewStub"
android:layout="@layout/btn2_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ViewStub
android:id="@+id/btn3ViewStub"
android:layout="@layout/btn3_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<!-- <ListView
android:id="@+android:id/listbody"
android:layout_width="fill_parent"
android:layout_height="376dp"
android:layout_weight="0.14" /> -->
<TableLayout android:layout_width="fill_parent"
android:layout_height="54dip"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="54dip"
>
<Button
android:id="@+id/btn1"
android:background="#888888"
android:layout_width="70dip"
android:layout_height="54dip"
android:layout_weight="1"
android:text="所有软件"
/>
<Button
android:id="@+id/btn2"
android:background="#888888"
android:layout_width="70dip"
android:layout_height="54dip"
android:layout_weight="1"
android:text="已处理软件"
/>
<Button
android:background="#888888"
android:id="@+id/btn3"
android:layout_width="70dip"
android:layout_height="54dip"
android:layout_weight="1"
android:text="未处理软件"
/>
</TableRow>
</TableLayout>
</RelativeLayout>
Android 布局 listview stubview
补充:移动开发 , Android