I created a custom class containing the parameters for an ArrayList, the Container to the Fragments and the Fragment's classes.
Like you see:
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);
}
}
}
On main class, i added the ArrayList and the OnItemClickListener with an Intent to open the Container and Fragments:
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);
}
});
}
}
The problem is: on click in any item of list, ever uses the lasts fragments declared - not the fragments declared on respective clicked item.
I try:
CustomClass customClass = list.get(position);
and
Intent openFragment = new Intent(Hoteis.this, Container.class);
startActivity(openFragment);
But doesn't works.
Does anyone know how to do the "OnItemClick" recognizes the position?
I think its because your fragments are declared as static. Because of the properties of static, your fragments are the same across all instances of CustomClass. You should read up on the static keyword here