繁体   English   中英

OnItemClick不起作用

[英]OnItemClick doesn't works

我创建了一个自定义类,其中包含ArrayList的参数,Fragments的Container和Fragment的类。

就像你看到的:

 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并打算打开容器和片段:

 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); } }); } } 

问题是:在列表中的任何项目上单击时,都使用声明的最后片段-而不是在相应单击的项目上声明的片段。

我尝试:

CustomClass customClass = list.get(position);

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

但是不起作用。

有人知道“ OnItemClick”如何识别位置吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM