片段中ListView的setOnItemClickListener

时间:2014-03-04 06:15:32

标签: android listview android-listview android-fragments onclicklistener

我在点击片段中的ListView项目时遇到问题。我编写了代码,因此片段指向所有内容的布局文件,而2个布局内部是ListViews,它从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对我不起作用。我还能如何连接每个listview项目的其他活动?

2 个答案:

答案 0 :(得分:9)

在您提供布局之后,添加ListView.SetOnItemClickListener的位置在您的片段创建代码中。使用findViewById查找ListView,并添加setOnItemClick侦听器。

class ResourcesFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View view = inflater.inflate(R.layout.resources_fragment, container, false);
        ListView lv = (ListView) view.findViewById(R.id.mylistviewid);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub

                        String category = categories[position];
                        Class activityClass = lookupActivityClass_byName(category);
                        //You could lookup by position, but "name" is more general

                        Intent intent = new Intent(getActivity(), activityClass);
                        startActivity(intent);


            }
        });

        return view;

    }

答案 1 :(得分:3)

使用Fragment时,您可以在onCreateView()内找到观看次数。简而言之,它与onCreate()的{​​{1}}相同。

因此,在这种情况下,请在Activity内找到ListView并仅在那里指定onCreateView()