2017-02-06 5 views
0

Создал пользовательский класс, содержащий параметры для ArrayList, Container для фрагментов и классов Fragment.OnItemClick не работает

Как и вы увидите:

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 с Intent, чтобы открыть контейнер и фрагменты:

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» распознает позицию?

+0

Если мое решение помогло вам, обязательно примите его! Спасибо :) – Zach

ответ

0

Я думаю, это потому, что ваши фрагменты объявлены как статические. Из-за свойств static ваши фрагменты одинаковы во всех экземплярах CustomClass. Вы должны прочитать статическое ключевое слово here

+0

Спасибо за помощь !! –

Смежные вопросы