列表视图没有响应点击

时间:2015-06-09 12:02:41

标签: android

我有一个实现下面显示的viewpager的活动

public class MainActivity extends FragmentActivity 

  {

    MyAdapter adapter;
    ViewPager pager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pager);
    adapter = new MyAdapter(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

}


public static class MyAdapter extends FragmentPagerAdapter {

private final String[] TITLES = {"All", "Popular","Social","departmental","religious","Academics","School","Weekend"}; // title for the tab

public MyAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public int getCount() {
    return TITLES.length;
}

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

@Override
public Fragment getItem(int position) {
    return SuperAwesomeCardFragment.newInstance(position);

}


}


}

在我的R.layout.fragment_pager中我做了这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE5E5E5" >



<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tabs"
    tools:context=".MainActivity" ></android.support.v4.view.ViewPager>

问题是我正在尝试实现我在片段布局中使用的列表视图项单击侦听器,但它没有响应。如下所示

  public class SuperAwesomeCardFragment extends Fragment

{

private static final String ARG_POSITION = "position";
private int position;

public static SuperAwesomeCardFragment newInstance(int position) {
    SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    position =  getArguments().getInt(ARG_POSITION) ;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.main_content_list, container, false);
    ListView lv = (ListView)v.findViewById(R.id.listView);
    lv.setAdapter(new MyAdapter(getActivity()));
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // does not work
        }
    });
    return v;
}

}

可能出现什么问题

1 个答案:

答案 0 :(得分:0)

尝试实施此代码可能会对您有所帮助。

lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Log.e("text","");
            }
        });