【发布时间】:2014-04-05 12:57:19
【问题描述】:
我在单击片段中的 ListView 项目时遇到问题。我编写了代码,因此片段指向所有内容的布局文件,其中 2 个布局内是 ListView,它从 strings.xml 中的字符串数组中的项目检索它们的条目。由于我编写代码的方式,我找不到为每个 ListView 项目实现 setOnItemClickListener 的方法。
MainActivity.java
package com.example.test_app;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity{
ViewPager ViewPager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager= (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager=getSupportFragmentManager();
ViewPager.setAdapter(new AdapterActivity(fragmentManager));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AdapterActivity.java
package com.example.test_app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AdapterActivity extends FragmentStatePagerAdapter{
public AdapterActivity(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int i){
Fragment fragment=null;
if(i==0){
fragment=new HomeFragment();
}
if(i==1){
fragment=new CommunityFragment();
}
if(i==2){
fragment=new ResourcesFragment();
}
return fragment;
}
@Override
public int getCount(){
return 3;
}
@Override
public CharSequence getPageTitle(int position){
if(position==0){
return "Home Page";
}
if(position==1){
return "Community";
}
if(position==2){
return "Resources";
}
return null;
}
}
class HomeFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.home_fragment, container, false);
}
}
class CommunityFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.community_fragment, container, false);
}
}
class ResourcesFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.resources_fragment, container, false);
}
}
然后在resources_fragment.xml布局中,有一个ListView
<ListView
android:id="@+id/resourcesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/categories" >
</ListView>
它从 strings.xml 中获取条目
<string-array name="categories">
<item>Category 1</item>
<item>Category 2</item>
<item>Category 3</item>
<item>Category 4</item>
<item>Category 5</item>
<item>Category 6</item>
<item>Category 7</item>
<item>Category 8</item>
</string-array>
我使用了this link to create my ListView,但 setOnItemClickListener 对我不起作用。我还能如何将每个列表视图项连接到其他活动?
【问题讨论】:
标签: android listview android-listview android-fragments onclicklistener