Hi I have a simple app that features a recyclerView with list items (each representing a "employee" object
i use this tuto https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/ using tabhost but when i click in item recycler view i want to pass some data in one of tabs
recycler view item ===> to fragment
this is xml fragment
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.iset.tse.Employee.OneFragment">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:text="Information de contact :"
android:textColor="?android:attr/statusBarColor"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.312"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/name_emp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="26dp"
android:text="username"
app:layout_constraintHorizontal_bias="0.204"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
</android.support.constraint.ConstraintLayout>
code Java
public class OneFragment extends Fragment
{
TextView txt_name;
public OneFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
txt_name=(TextView) txt_name.findViewById(R.id.name_emp);
String name = getArguments().getString("name");
txt_name.setText(name);
return inflater.inflate(R.layout.fragment_one, container, false);
}
public static OneFragment newInstance(String nameValue) {
String name;
Bundle args = new Bundle();
OneFragment fragment = new OneFragment();
name = nameValue;
fragment.setArguments(args);
return fragment;
}
Activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.iset.tse.Employee.Details_Employee">
<View
android:id="#+id/view"
android:layout_width="421dp"
android:layout_height="211dp"
android:background="#1ABC9C"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="70dp"
android:layout_height="57dp"
app:srcCompat="#drawable/employee"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="105dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.53" />
<TextView
android:id="#+id/name_emp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textAlignment="center"
android:textAllCaps="false"
android:textStyle="bold|italic"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/imageView9" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="0dp"
android:layout_height="69dp"
android:background="#color/wallet_bright_foreground_holo_dark"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="0dp"
android:layout_height="541dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tabs">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
java activity code
//tabs
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
/* Date_sys = (TextView) findViewById(R.id.date_sys);
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
Date_sys.setText(currentDateTimeString);
chechk_in.setText(chechkin);
*/
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_info, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("TWO");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_time, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
String name;
public void sendData(String nameAdapter) {
name = nameAdapter;
if (name != null) {
OneFragment med_frag = OneFragment.newInstance(name);
getSupportFragmentManager().beginTransaction()
.replace(R.id.viewpager, med_frag)
.commit();
}
}
this is My adapter
public class EmployeeAdapter extendsRecyclerView.Adapter<EmployeeAdapter.EmployeeHolder> {
final List<Employee> EmployeeList;
private Context context;
#Override
public EmployeeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row =
LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_row,parent,false);
EmployeeHolder holder= new EmployeeHolder(row);
return holder;
}
public EmployeeAdapter(List<Employee> EmployeeList) {
this.EmployeeList=EmployeeList;
}
#Override
public void onBindViewHolder(EmployeeHolder holder, int position) {
Employee employee=EmployeeList.get(position);
// holder.id.setText(employee.getId());
holder.name.setText(employee.getName());
}
#Override
public int getItemCount()
{
return EmployeeList.size();
}
public class EmployeeHolder extends RecyclerView.ViewHolder
{
private final Context context;
TextView name;
public EmployeeHolder(View itemView) {
super(itemView);
context = itemView.getContext();
name=(TextView)itemView.findViewById(R.id.name_emp);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Details_Employee)context).sendData(name.getText().toString());
}
So what you require is a model class for your data and sharing of data from one fragment to another.
You can share data using ArrayList(of your model class) as the argument for 2nd fragment from the first one and getting that ArrayList in your constructor.
eg: frag A and frag B and a model class named model.
Firstly a model class to store all your data eg
model class
public class model{
private String item1,item2,item3;
private int a,b,c;
public model(String item1, String item2, String item3, int a, int b, int c) {
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.a = a;
this.b = b;
this.c = c;
}
public String getItem1() {
return item1;
}
public void setItem1(String item1) {
this.item1 = item1;
}
public String getItem2() {
return item2;
}
public void setItem2(String item2) {
this.item2 = item2;
}
public String getItem3() {
return item3;
}
public void setItem3(String item3) {
this.item3 = item3;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
}
Now in
frag A
public class frag_A extends Fragment {
private RecyclerView mRecyclerView;
MainAdapter mAdapter;
ArrayList<model> data_list;//DEFINE AN ARRAYLIST OF YOUR MODEL CLASS
public frag_A() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_frag_A, container, false);
mRecyclerView = (RecyclerView)v.findViewById(R.id.recyclerview_service);
data_list = new ArrayList<>();
//THIS IS WHERE MODEL CLASS IS USED TO SET DATA IN ARRAYLIST
model new_data=new model(item1,item2,item3,a,b,c);
data_list.add(model);//add the model data to your arraylist
mAdapter = new MainAdapter(data_list);//SET ARRAYLIST IN ADAPTER
//set all recyclerview components you want
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setAdapter(mAdapter);
//onclicklistener for recyclerview items
mRecyclerView.addOnItemTouchListener(
new mehakmeet.startup.com.v1.home.room_services.food_order.Adapter.RecyclerItemClickListener(getActivity().getApplicationContext(), new mehakmeet.startup.com.v1.home.room_services.food_order.Adapter.RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, final int position) {
//THIS IS THE PLACE WHERE DATA IS SHARED
service_details hk = new service_details(data_list);
//YOU CAN SHARE THE POSITION CLICKED HERE using bundle
Bundle args=new Bundle();
args.putString("position",String.valueOf(position);
hk.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.main_home, hk).addToBackStack(null)
.commit();
}
})
);
return v;
}
}
Now to get that arraylist parameter , implement this arraylist in your frag_B constructor
frag_B
public class frag_B extends Fragment {
ArrayList<model> data_list;//Again define an arraylist of your model class
TextView item1,item2,item3
int a,b,c;
Button make_book;
//This is the constructor that uses the arraylist from previous fragment and can be used as arraylist in this fragment.
public frag_B(ArrayList<model> data_list) {
this.data_list=data_list;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_service_details, container, false);
item1=(TextView)v.findViewById(R.id.item_1);
item2=(TextView)v.findViewById(R.id.item_2);
item3=(TextView)v.findViewById(R.id.item_3);
//HERE GET THE POSITION CLICKED HERE
int position=Integer.parseInt(getArguments().getString("position");
item1.setText(data_list.get(position).getitem1());
item2.setText(data_list.get(position).getitem2());
item3.setText(data_list.get(position).getitem3());
a=data_list.get(position).getA();
b=data_list.get(position).getB();
c=data_list.get(position).getC();
return v;
}
}
You can use EventBus for that. Checkout the readme for how to use it.
Related
i am struggling since days to figure out why my click event is not firing up on items on a RecylerView.
i have the main Activity that uses a ViewPagerAdapter to show 4 fragments , one of the fragments uses a RecyclerView and an adapter to show items ( from Volley request ).
WelcomeActivity ---> ViewPagerAdapter ----> SearchListFragment ---> ProListAdapter
WelcomeActivity :
public class WelcomeActivity extends AppCompatActivity {
//View
private BottomNavigationView navigation;
//viewPager
private ViewPager viewPager;
private ViewPagerAdapter mViewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//Views
navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(0);
viewPager = findViewById(R.id.view_pager);
mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mViewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
navigation.getMenu().findItem(R.id.navigation_home).setChecked(true);
break;
case 1:
navigation.getMenu().findItem(R.id.navigation_search).setChecked(true);
break;
case 2:
navigation.getMenu().findItem(R.id.navigation_favorites).setChecked(true);
break;
case 3:
navigation.getMenu().findItem(R.id.navigation_settings).setChecked(true);
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
break;
case R.id.navigation_favorites:
viewPager.setCurrentItem(2);
break;
case R.id.navigation_search:
viewPager.setCurrentItem(1);
break;
case R.id.navigation_settings:
viewPager.setCurrentItem(3);
break;
}
return false;
}
};
}
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ProfessionalListFragment();
case 1:
return new SearchListFragment();
case 2:
return new FavoritesFragment();
case 3:
return new SettingsFragment();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
SearchListFragment :
public class SearchListFragment extends Fragment{
View rootView;
private RecyclerView recyclerView;
private ProListAdapter adapter;
ProgressDialog progressDialog;
String url;
String prof;
ArrayList<User> pros = new ArrayList<>();
public SearchListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Implementing View Pager
rootView = inflater.inflate(R.layout.fragment_search_list, container, false);
//RecyclerView
recyclerView = rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(rootView.getContext(), 3));
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getArguments() != null) {
url = getArguments().getString("url");
prof = getArguments().getString("pro");
sendRequest();
}
}
private void sendRequest() {
//Show ProgressDialog
progressDialog = new ProgressDialog(getContext());
progressDialog.setCancelable(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setTitle(getString(R.string.loading));
progressDialog.setMessage(getString(R.string.dialog_wait));
progressDialog.show();
Map<String, String> params = new HashMap();
params.put("prof", prof);
StringRequest jsonObjReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Response OK
....
adapter = new ProListAdapter(pros, getContext());
recyclerView.setAdapter(adapter);
....
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//error
progressDialog.dismiss();
}
}){
#Override
protected Map<String, String> getParams()
{
return params;
}
};
//Adding the request to the queue along with a unique string tag
MySingleton.getInstance(getContext()).addToRequestQueue(jsonObjReq);
}
}
fragment_search_list.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".WelcomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
tools:listitem="#layout/pro_card"/>
</LinearLayout>
ProListAdapter :
public class ProListAdapter extends RecyclerView.Adapter<ProListAdapter.ViewHolder>{
private ArrayList<User> itemsData;
private Context context;
public ProListAdapter(ArrayList<User> itemsData, Context context) {
this.itemsData = itemsData;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.pro_card, parent, false);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(context, itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.nom.setText(itemsData.get(position).getName());
holder.phone.setText(Long.toString(itemsData.get(position).getPhone()));
holder.img.setImageResource(R.drawable.avatar);
holder.card.setAnimation(AnimationUtils.loadAnimation(context, R.anim.zoom_in));
}
#Override
public int getItemCount() {
return itemsData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView nom, phone;
public ImageView img;
public CardView card;
private Context context;
public ViewHolder(Context context, View itemLayoutView) {
super(itemLayoutView);
card = itemLayoutView.findViewById(R.id.card_pro);
nom = itemLayoutView.findViewById(R.id.pro_nom);
phone = itemLayoutView.findViewById(R.id.pro_phone);
img = itemLayoutView.findViewById(R.id.pro_img);
this.context = context;
itemLayoutView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position = getLayoutPosition();
if (position != RecyclerView.NO_POSITION) {
Toast.makeText(context, "hi", Toast.LENGTH_LONG).show();
}
}
}
}
pro_card.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">
<androidx.cardview.widget.CardView
android:id="#+id/card_pro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
android:background="#color/color_white">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pro_img"
android:layout_width="140dp"
android:layout_height="130dp"
android:scaleType="centerInside"
android:src="#drawable/avatar"
android:background="?attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toTopOf="#id/pro_nom"
app:layout_constraintStart_toStartOf="parent"
android:focusableInTouchMode="false"
android:clickable="false"/>
<TextView
android:id="#+id/pro_nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="26sp"
app:layout_constraintBottom_toTopOf="#id/pro_phone"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="3dp"
tools:text="#tools:sample/first_names"
android:focusableInTouchMode="false"
android:clickable="false"/>
<TextView
android:id="#+id/pro_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="3dp"
tools:text="#tools:sample/us_phones"
android:focusableInTouchMode="false"
android:clickable="false"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
I tried many options but none is working , what is the best way and where is my mistake ?
My ListView item click is not working. I am not able to figure out why. It is simple listview with only one TextView in each row. Please help.
I tried different options but none of them worked.My fragment is part of drawer activity in which i am replacing fragments depending on user click.
fragment_first.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detailNoticeListView"/>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:text="#string/app_name"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content"
android:id="#+id/detailNoticeText" />
</LinearLayout>
Adapter:
public class MyAdapter extends android.widget.BaseAdapter {
private List<DataModel> dataModels;
private Activity activity;
public MyAdapter(List<DataModel> dataModels, Activity activity){
this.dataModels = dataModels;
this.activity = activity;
}
#Override
public int getCount() {
return dataModels.size();
}
#Override
public Object getItem(int i) {
return dataModels.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(activity).
inflate(R.layout.item, viewGroup, false);
}
DataModel currentItem = (DataModel) getItem(i);
// get current item to be displayed
TextView textViewItemName = (TextView)
view.findViewById(R.id.detailNoticeText);
textViewItemName.setText(currentItem.getText());
return view;
}
#android.support.annotation.Nullable
#Override
public CharSequence[] getAutofillOptions() {
return new CharSequence[0];
}
}
Fragment:
public class MyFragment extends Fragment{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
java.util.ArrayList<DataModel> dataModels;
ListView listView;
private static MyAdapter adapter;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public MyFragment() {
// Required empty public constructor
}
public static MyFragment newInstance(String param1, String param2) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
dataModels= new java.util.ArrayList<>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
listView=(ListView)view.findViewById(R.id.detailNoticeListView);
dataModels.add(new DataModel(
"1"));
dataModels.add(new DataModel(
"2"));
dataModels.add(new DataModel(
"3"));
dataModels.add(new DataModel(
"4"));
adapter= new MyAdapter(dataModels, getActivity());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(android.widget.AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
android.widget.Toast.makeText(getActivity(), "User logged out successfully", android.widget.Toast.LENGTH_SHORT).show();
}
});
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Add android:clickable="true" to your item layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<TextView
android:layout_width="match_parent"
android:text="#string/app_name"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content"
android:id="#+id/detailNoticeText" />
</LinearLayout>
I Have a problem with getItem() function why because it is called twice in FragmentStatePagerAdapter class.
Actually the main reason is in application having TextoSpeech functionality so getItem() function twice the text also speech twice. This is my code can u please assist me....Great thanks in advance.
Here is the code
This is MainActivity class:
public class MainActivity extends FragmentActivity{
PagerFragment pagerFragment;
Cursor mCursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rhymes_activity_main);
DBUtils utils = new DBUtils(getApplicationContext());
new DBUtils(getApplicationContext());
try {
DBUtils.createDatabase();
} catch (IOException e) {
Log.w(" Create Db "+e.toString(),"===");
}
DBUtils.openDatabase();
mCursor = utils.getResult("select * from Cflviewpagerdata order by title");
final ArrayList<PageData> myList = new ArrayList<PageData>();
while (mCursor.moveToNext()) {
myList.add(new PageData(mCursor.getInt(
mCursor.getColumnIndex("_id")),
mCursor.getString(mCursor.getColumnIndex("title")),
mCursor.getString(mCursor.getColumnIndex("view"))));
}
mCursor.close();
ListView lv = (ListView) findViewById(R.id.list_view);
ListViewAdapter lva = new ListViewAdapter(this, R.layout.list_item, myList);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//peace of code that create launch new fragment with swipe view inside
PagerFragment pagerFragment = new PagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("CURRENT_POSITION", position);
bundle.putParcelableArrayList("DATA_LIST", myList);
pagerFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, pagerFragment, "swipe_view_fragment").commit();
}
});
DBUtils.closeDataBase();
}
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentByTag("swipe_view_fragment");
if(f!=null){
fm.beginTransaction().remove(f).commit();
}else{
super.onBackPressed();
}
}
}
This is PagerFragment class:
public class PagerFragment extends Fragment{
private ArrayList<PageData> data;
private int currentPosition;
private String mTitle;
private FragmentActivity context;
#Override public void onAttach(Activity activity) {
context = (FragmentActivity) activity;
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager, container, false);
ViewPager mViewPager = (ViewPager) v.findViewById(R.id.pager_view);
currentPosition = getArguments().getInt("CURRENT_POSITION");
mTitle = getArguments().getString("T_TITLE");
data = getArguments().getParcelableArrayList("DATA_LIST");
FragmentItemPagerAdapter fragmentItemPagerAdapter = new FragmentItemPagerAdapter(getFragmentManager(), data);
mViewPager.setAdapter(fragmentItemPagerAdapter);
mViewPager.setCurrentItem(currentPosition);
return v;
}
}
This is FragmentItemPagerAdapter class:
public class FragmentItemPagerAdapter extends FragmentStatePagerAdapter{
private static ArrayList<PageData> data;
public FragmentItemPagerAdapter(FragmentManager fm, ArrayList<PageData> data){
super(fm);
this.data = data;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(PageFragment.TITLE, data.get(position).getTitle());
args.putString(PageFragment.DESCRIPTION, data.get(position).getDes());
args.putInt("CURRENT_POSITION", position);
fragment.setArguments(args);
return fragment;
}
void deletePage(int position) {
if (canDelete()) {
data.remove(position);
notifyDataSetChanged();
}
}
boolean canDelete() {
return data.size() > 0;
}
#Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return PagerAdapter.POSITION_NONE;
}
#Override
public int getCount() {
return data.size();
}
public static class PageFragment extends Fragment implements OnInitListener{
public static final String TITLE = "title";
public static final String DESCRIPTION = "view";
String om;
TextToSpeech tts;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
((TextView) rootView.findViewById(R.id.item_label)).setText(getArguments().getString(TITLE));
View tv = rootView.findViewById(R.id.item_des);
((TextView) tv).setText(getArguments().getString(DESCRIPTION));
Bundle bundle = getArguments();
int currentPosition = bundle.getInt("CURRENT_POSITION");
tts = new TextToSpeech( getActivity(), PageFragment.this);
om = data.get(currentPosition).getDes();
tts.speak(om, TextToSpeech.QUEUE_FLUSH, null);
return rootView;
}
#Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
tts.speak(om, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
This is PageData class:
This class is Object class
public class PageData implements Parcelable{
private String title;
private String view;
public PageData(){
}
public PageData(Parcel in){
title = in.readString();
view = in.readString();
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(view);
}
public PageData(int picture, String title, String description){
this.title = title;
this.view = description;
}
public String getTitle() {
return title;
}
public String getDes() {
return view;
}
}
This is Xml code
fragment_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10sp"
android:text="Image Name"
android:textColor="#android:color/black"
android:textSize="18sp"
android:layout_gravity="center_horizontal" />
<TextView
android:id="#+id/item_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10sp"
android:text="Image Name"
android:textColor="#android:color/black"
android:textSize="18sp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
This is fragment_pager.xml: This is viewpager layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<android.support.v4.view.ViewPager
android:id="#+id/pager_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
This is rhymes_activity_main.xml: This is for listview of application.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="#+id/list_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
This images are my application look
This image is logcat of my application
Highlighted is my problem.
This is my code can you please help me any one.
I am new one of Android so please help
FragmentStatePagerAdapter preloads always at least 1 page.
You can try to use setUserVisibleHint to handle your logic only for visible fragment:
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Your code
}
}
I am new to android.I want to design a page in android which contains a Tab view inside the fragment .
Fragment :
For that I have a Fragment named fragment_home.xml .
Layout Resource files for tabs:
And three layout resource file named
today.xml
this-week.xml
this_month.xml
and java classes for the resource file is
Today.java
ThisWeek.java
Thismonth.java
What is my doubt is how can I implement the tab view inside the fragment .I have referred some SO files but nothing is helped for me .
Home.java (Fragment):
public class Home extends Fragment{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
private OnFragmentInteractionListener mListener;
public Home() {
}
public static Home newInstance(String param1, String param2) {
Home fragment = new Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
Pager adapter = new Pager(getChildFragmentManager());
adapter.addFragment(new Today(), "Today");
adapter.addFragment(new Reminders(), "Reminders");
adapter.addFragment(new Settings(), "Settings");
viewPager.setAdapter(adapter);
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Here I want to implement something but I don't know ,How to ?
Please help me to get this.
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Home">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
PagerAdapter
public class Pager extends FragmentStatePagerAdapter {
int tabCount;
public Pager(FragmentManager fragmentManager, int tabCount) {
super(fragmentManager);
this.tabCount= tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Today todayObj = new Today();
return todayObj;
case 1:
ThisWeek thisWeekObj = new ThisWeek();
return thisWeekObj;
case 2:
ThisMonth thisMonthObj = new ThisMonth();
return thisMonthObj;
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
public void addFragment(Today today, String today1) {
}
public void addFragment(Reminders reminders, String reminders1) {
}
public void addFragment(Settings settings, String settings1) {
}
}
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class Home extends Fragment{
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new FragmentOne(), "Fragment name one"); //Need to call first tab fragment
adapter.addFragment(new FragmentTwo(), "Fragment name two"); // Need second tab fragment and so on...
viewPager.setAdapter(adapter);
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
FragmentOne.class
public class FragmentOne extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
}
fragment_one.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="#color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment One Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
FragmentTwo.class
public class FragmentTwo extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
}
}
fragment_two.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="#color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
use this code , this is the complete example of tablayout using view pager
My main objective is to create a listview, in each row a viewpager with a "ImageWordFragment" in each page, which cotains an Imageview.
Please DON'T tell me not to use viewpager inside a listview. A lots of apps are doing it, just help get to my goal.
I tried setting "setId()" but it prompts a null exception in the ViewPager variable inside the "getView" function.
The problem is the the first viewpager works in the first row of the listview. But the rest of it doesn't.
This is the xml of "fragment_word_image.xml":
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="LALA"/>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_grey"/>
</LinearLayout>
This is the xml of "item_word.xml":
<LinearLayout
android:id="#+id/moreInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
This is the adapter:
public class WordAdapter extends BaseAdapter {
private AppCompatActivity activity;
private LayoutInflater inflater;
private List<Word> wordItems;
public WordAdapter(AppCompatActivity activity, List<Word> wordItems) {
this.activity = activity;
this.wordItems = wordItems;
}
#Override
public int getCount() {
return wordItems.size();
}
#Override
public Word getItem(int position) {
return wordItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_word, parent, false);
}
FrameLayout container = (FrameLayout) convertView.findViewById(R.id.container);
TextView type = (TextView) convertView.findViewById(R.id.type);
TextView name = (TextView) convertView.findViewById(R.id.name);
ImageView progress = (ImageView) convertView.findViewById(R.id.progress);
final LinearLayout moreInfo = (LinearLayout) container.findViewById(R.id.moreInfo);
ViewPager pager = (ViewPager) convertView.findViewById(R.id.viewPager);
// Getting word data
final Word w = wordItems.get(position);
//Pager
WordPagerAdapter adapter = new WordPagerAdapter(activity.getSupportFragmentManager(), activity, pager);
Bundle args1 = new Bundle();
args1.putString(ImageWordFragment.ARG_LINK, w.getImage());
adapter.addTab(R.string.title_section1, ImageWordFragment.class, args1);
Bundle args2 = new Bundle();
args2.putString(ImageWordFragment.ARG_LINK, w.getImage());
adapter.addTab(R.string.title_section1, ImageWordFragment.class, args2);
Bundle args3 = new Bundle();
args3.putString(ImageWordFragment.ARG_LINK, w.getImage());
adapter.addTab(R.string.title_section1, ImageWordFragment.class, args3);
//Notify Changes
adapter.notifyTabsChanged();
return convertView;
}
private class WordPagerAdapter extends FragmentStatePagerAdapter {
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final Context mContext;
private final FragmentManager mFragmentManager;
private final ViewPager mViewPager;
final class TabInfo {
private final Class<?> mClass;
private final Bundle mArgs;
private final int mTitleRes;
TabInfo(Class<?> fragmentClass, Bundle args, int titleRes) {
mClass = fragmentClass;
mArgs = args;
mTitleRes = titleRes;
}
}
public WordPagerAdapter(FragmentManager fm, Context context, ViewPager pager) {
super(fm);
mFragmentManager = fm;
mContext = context;
//Setup view pager
mViewPager = pager;
mViewPager.setAdapter(this);
}
public void addTab(int titleRes, Class<?> fragmentClass, Bundle args) {
mTabs.add(new TabInfo(fragmentClass, args, titleRes));
}
public void notifyTabsChanged() {
notifyDataSetChanged();
}
public void setCurrentItem(int position) {
mViewPager.setCurrentItem(position);
}
public void setOffscreenPageLimit(int pages) {
mViewPager.setOffscreenPageLimit(pages);
}
#Override
public Fragment getItem(int position) {
TabInfo tab = mTabs.get(position);
return Fragment.instantiate(mContext, tab.mClass.getName(), tab.mArgs);
}
#Override
public int getCount() {
return mTabs.size();
}
}
}
Thsi is the fragment:
public class ImageWordFragment extends Fragment {
public static final String ARG_LINK = "ARG_LINK";
#InjectView(R.id.imageView)
ImageView imageView;
String imageUrl;
public ImageWordFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageUrl = getArguments().getString(ARG_LINK);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_word_image, container, false);
ButterKnife.inject(this, rootView);
new ImageLoadTask(imageUrl, imageView);
return rootView;
}
}
A couple of experiments indicate this is probably caused by your ViewPagers all having the same id (from the XML). Have you tried generating ids at runtime using View.generateViewId()?