首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnItemClick不工作

OnItemClick不工作
EN

Stack Overflow用户
提问于 2017-02-06 22:47:54
回答 1查看 59关注 0票数 0

我创建了一个自定义类,它包含ArrayList的参数、片段的容器和片段的类。

就像你看到的:

代码语言:javascript
复制
package com.example.android.testeclickitem;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CustomClass {

    private int mImage;

    private String mName;

    private String mLocalization;

    private static int mFragmentI;

    private static int mFragmentII;

    private static int mFragmentIII;

    public CustomClass (int image, String name, String localization, int fragmentI, int fragmentII, int fragmentIII){
        mImage = image;
        mName = name;
        mLocalization = localization;
        mFragmentI = fragmentI;
        mFragmentII = fragmentII;
        mFragmentIII = fragmentIII;
    }

    public int getImage() {
        return mImage;
    }

    public void setImage(int mImage) {
        this.mImage = mImage;
    }

    public String getName() {
        return mName;
    }

    public void setName(String mName) {
        this.mName = mName;
    }

    public String getLocalization() {
        return mLocalization;
    }

    public void setLocalization(String mLocalization) {
        this.mLocalization = mLocalization;
    }

    public static int getFragmentI() {return mFragmentI;}

    public  void setFragmentI(int mFragmentI) {
        this.mFragmentI = mFragmentI;
    }

    public static int getFragmentII() {
        return mFragmentII;
    }

    public void setFragmentII(int mFragmentII) {
        this.mFragmentII = mFragmentII;
    }

    public static int getFragmentIII() {
        return mFragmentIII;
    }

    public void setFragmentIII(int mFragmentIII) {
        this.mFragmentIII = mFragmentIII;
    }

    public static class FragmentInflaterI extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(getFragmentI(), container, false);
        }
    }

    public static class FragmentInflaterII extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(getFragmentII(), container, false);
        }
    }

    public static class FragmentInflaterIII extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(getFragmentIII(), container, false);
        }
    }

    static class Fragments extends FragmentPagerAdapter {

        public Fragments (android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return new FragmentInflaterI();
            } else if (position == 1){
                return new FragmentInflaterII();
            } else {
                return new FragmentInflaterIII();
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

    public static class Container extends AppCompatActivity {

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

            // Set the content of the activity to use the activity_main.xml layout file
            setContentView(R.layout.layout_container);

            // Find the view pager that will allow the user to swipe between fragments
            ViewPager viewPager = (ViewPager) findViewById(R.id.layout_container);

            // Create an adapter that knows which fragment should be shown on each page
            Fragments adapter = new Fragments(getSupportFragmentManager());

            // Set the adapter onto the view pager
            viewPager.setAdapter(adapter);
        }
    }
}

在主类中,我添加了ArrayList和OnItemClickListener,目的是打开容器和片段:

代码语言:javascript
复制
package com.example.android.testeclickitem;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

import static com.example.android.testeclickitem.CustomClass.Container;

public class Hoteis extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hoteis);

        final ArrayList <CustomClass> lista = new ArrayList<>();
        CustomClass hoteis = new CustomClass(0, "", "", 0, 0, 0);
        hoteis.setImage(R.mipmap.ic_hotel_white_48dp);
        hoteis.setName(getString(R.string.name_hotel));
        hoteis.setLocalization(getString(R.string.local_hotel));
        hoteis.setFragmentI(R.layout.fragment_hotel1_perfil);
        hoteis.setFragmentII(R.layout.fragment_hotel1_preco);
        hoteis.setFragmentIII(R.layout.fragment_hotel1_contato);
        lista.add(hoteis);

        CustomClass hoteis2 = new CustomClass(0, "", "", 0, 0, 0);
        hoteis2.setImage(R.mipmap.ic_hotel_white_48dp);
        hoteis2.setName(getString(R.string.name_hotel2));
        hoteis2.setLocalization(getString(R.string.local_hotel2));
        hoteis.setFragmentI(R.layout.fragment_hotel2_perfil);
        hoteis.setFragmentII(R.layout.fragment_hotel2_preco);
        hoteis.setFragmentIII(R.layout.fragment_hotel2_contato);
        lista.add(hoteis2);

        CustomClassAdapter itemAdapter = new CustomClassAdapter(this, lista);

        ListView listView = (ListView) findViewById(R.id.lista_hoteis);

        listView.setAdapter(itemAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Intent openFragment = new Intent(Hoteis.this, Container.class);
                startActivity(openFragment);
            }
        });
    }
}

问题是:单击列表中的任何项目时,都要使用已声明的持久片段--而不是在各个单击项上声明的片段。

我试着:

代码语言:javascript
复制
CustomClass customClass = list.get(position);

代码语言:javascript
复制
Intent openFragment = new Intent(Hoteis.this, Container.class);
startActivity(openFragment);

但不管用。

有谁知道如何做"OnItemClick“来识别这个职位吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-06 23:00:24

我想是因为你的碎片被宣布为静态的。由于静态的属性,您的片段在所有CustomClass实例中都是相同的。您应该阅读静态关键字这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42078672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档