片段中ListView的setOnItemClickListener

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了片段中ListView的setOnItemClickListener相关的知识,希望对你有一定的参考价值。

我在点击片段中的ListView项目时遇到问题。我编写了代码,因此片段指向所有内容的布局文件,而2个布局内部是ListViews,它从strings.xml中的字符串数组中的项检索其条目。由于我编写代码的方式,我找不到在我的情况下为每个ListView项实现setOnItemClickListener的方法。

main activity.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;
    }
}

adapter activity.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项目的其他活动?

答案

在您提供布局后,添加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;

    }
另一答案

当您使用Fragment时,您可以在onCreateView()中找到视图。简单来说,它与onCreate()Activity相同。

所以在这种情况下,在ListView中找到onCreateView()并仅在那里分配onItemClickListener

以上是关于片段中ListView的setOnItemClickListener的主要内容,如果未能解决你的问题,请参考以下文章

在android中,当我按了listview中的一个子项时,利用ContextMenu弹出菜单,那怎么获取这个子项的数据呢?

片段中的ListView不显示

在片段中添加 ListView

ListView 未显示在片段中

为啥我的 ListView 显示在一个片段中而不是另一个片段中?

片段中ListView的setOnItemClickListener