Java 如何在片段中使用ListView和onClickListener?
如何将listview和onclicklistener添加到tab1fragment.xml布局中 我有多个标签,但如果我有一个例子,我想我可以解决它的休息。。我尝试过其他答案,但到目前为止还没有成功 这是我的作品。。谢谢各位 MyTrans.java tab1fragment.xmlJava 如何在片段中使用ListView和onClickListener?,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,如何将listview和onclicklistener添加到tab1fragment.xml布局中 我有多个标签,但如果我有一个例子,我想我可以解决它的休息。。我尝试过其他答案,但到目前为止还没有成功 这是我的作品。。谢谢各位 MyTrans.java tab1fragment.xml tabs_viewpager_layout.xml 您可以在文本视图下面添加一个列表视图,如下所示: <?xml version="1.0" encoding="utf-8"?> <Line
tabs_viewpager_layout.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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Fragment 1" />
<ListView
android:id="+@id/myList1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
有关
onItemClickListener
的教程,请参见。您可以在文本视图下添加列表视图
,如下所示:
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Fragment 1" />
<ListView
android:id="+@id/myList1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
有关onItemClickListener
的教程,请参见。“3个选项卡,我想为它们添加一个Listview和多个onClickListener,每个“链接”指向不同的活动”-我想你应该试着一步一步地解决问题,这个问题太广泛了。@0x0nosugar谢谢你的回答,“我编辑过,试图让它不那么宽泛”3个选项卡,我想给它们添加一个Listview和多个onClickListener,每个“链接”指向不同的活动”-我想你应该尝试一步一步地解决你的问题,这个问题太宽泛了。@0x0nosugar谢谢你的回答,我编辑的目的是让它不那么宽泛。请告诉我这是否是你想要的?谢谢你在列表中给出的好答案,它很有效!但是关于McClickListener,我如何让“textView1”打开一个新活动?正如我在指南上看到的,它只会激活一个祝酒词,谢谢@Storey-感谢您的反馈:)该指南(包括该页后面的示例)更能让您对如何使用“onItemClicked()”的参数有一个第一印象。我想给你们指出我发布的另一个关于如何开始一项活动的例子的答案。(这就是“可重用性”:我们尝试一次回答一个问题,并尽可能链接到现有的答案:D)请告诉我这是否是您想要的答案?感谢列表中的伟大答案,它很有效!但是关于McClickListener,我如何让“textView1”打开一个新活动?正如我在指南上看到的,它只会激活一个祝酒词,谢谢@Storey-感谢您的反馈:)该指南(包括该页后面的示例)更能让您对如何使用“onItemClicked()”的参数有一个第一印象。我想给你们指出我发布的另一个关于如何开始一项活动的例子的答案。(这就是“可重用性”:我们尝试一次回答一个问题,并尽可能链接到现有答案:D)
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Fragment 1" />
</LinearLayout>
<?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:orientation="vertical" >
<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" >
<HorizontalScrollView
android:id="@+id/hScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Fragment 1" />
<ListView
android:id="+@id/myList1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
ListView lvList1 = (ListView) v.findViewById(R.id.myList1);
lvList1.setOnItemClickListener(new OnItemClickListener{... });