在main Activity中用fragment 实现了一个tab(tab1),tab1 中用listview显示数据,listview中每行的layout 有单独xml定义。
1.xml部分代码
1.1 ,main.xml中tabhost部分的定义
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:name="省略"
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</TabHost>
1.2.tab1的xml定义
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
1.3.listview 中每行的xml定义
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget32"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/listImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:src="@android:drawable/btn_star" />
<LinearLayout
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="9"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="9"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal">
<TextView
android:id="@+id/textListInfo1"
android:layout_width="fill_parent"
android:layout_weight="3"
android:layout_height="25dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:text="家电 等" />
</LinearLayout>
<LinearLayout
<TextView
android:id="@+id/textListInfo2"
android:layout_width="fill_parent"
android:layout_weight="3"
android:layout_height="25dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:text="机器 等" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2.java代码
2.1 main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
private void setupViews() {
TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
tabhost.setup();
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));
tab_fragment1 tab1 = new tab_fragment1();
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(tab1, "tab1");
ft.commit();
}
2.1 tab1代码
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
Log.v("MyFragment1","OnCreateView.......");
View v = inflater.inflate(R.layout.activity_tab_fragment1, container,false);
listView = (ListView) v.findViewById(R.id.listView1);
adapter = new LoaderAdapter(10, v.getContext(),R.layout.activity_ent001000_list_item, URLS);
listView.setAdapter(adapter);
listView.setOnScrollListener(mScrollListener);
listView.setClickable(true);
listView.setOnItemClickListener(mItemClickListener);
return v;
}
OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
listView.getSelectedView().setBackgroundColor(Color.GRAY);
System.out.println("你点击的是第" + arg3 + "项");
}
};
在虚拟机上运行后,鼠标点在listview的明细上没有任何反应也么有报错信息,在onitemclick中设的断点也走不进去
1.xml部分代码
1.1 ,main.xml中tabhost部分的定义
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:name="省略"
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</TabHost>
1.2.tab1的xml定义
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
1.3.listview 中每行的xml定义
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget32"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/listImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:src="@android:drawable/btn_star" />
<LinearLayout
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="9"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="9"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal">
<TextView
android:id="@+id/textListInfo1"
android:layout_width="fill_parent"
android:layout_weight="3"
android:layout_height="25dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:text="家电 等" />
</LinearLayout>
<LinearLayout
<TextView
android:id="@+id/textListInfo2"
android:layout_width="fill_parent"
android:layout_weight="3"
android:layout_height="25dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:text="机器 等" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2.java代码
2.1 main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
private void setupViews() {
TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
tabhost.setup();
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));
tab_fragment1 tab1 = new tab_fragment1();
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(tab1, "tab1");
ft.commit();
}
2.1 tab1代码
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
Log.v("MyFragment1","OnCreateView.......");
View v = inflater.inflate(R.layout.activity_tab_fragment1, container,false);
listView = (ListView) v.findViewById(R.id.listView1);
adapter = new LoaderAdapter(10, v.getContext(),R.layout.activity_ent001000_list_item, URLS);
listView.setAdapter(adapter);
listView.setOnScrollListener(mScrollListener);
listView.setClickable(true);
listView.setOnItemClickListener(mItemClickListener);
return v;
}
OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
listView.getSelectedView().setBackgroundColor(Color.GRAY);
System.out.println("你点击的是第" + arg3 + "项");
}
};
在虚拟机上运行后,鼠标点在listview的明细上没有任何反应也么有报错信息,在onitemclick中设的断点也走不进去