Problem with replace fragment in ViewPage - java

I am trying replace fragment first on second in ViewPager. And I have small problem becouse after replace still i see in background fragment first and they are working listner from fragment first. What am i doing wrong ? I replace first_fragment view but it doesn't work. In other project i were done somthing simular and it's working well !
public class FirstFragment extends BaseFragment {
private RecyclerView pathwaysRecyclerView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pathways, container, false);
initializeViews(view);
initRecyclerView();
return view;
}
private void initializeViews(View view) {
pathwaysRecyclerView = view.findViewById(R.id.pathways_recyclerview);
}
private void initRecyclerView(){
pathwaysRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
PathwaysAdapter adapter = new PathwaysAdapter(new OnPathwayClickListner(),requireContext());
pathwaysRecyclerView.setAdapter(adapter);
}
private class OnPathwayClickListner implements View.OnClickListener {
#Override
public void onClick(View view) {
navigateToSecondFragment();
}
}
private void navigateToSecondFragment(){
Fragment fragment = new SecondFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_pathways_view, fragment)
.addToBackStack(null)
.commit();
}
#Override
public void updateView() {
}
}
fragment_first.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_pathways_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/greyAccent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ebpHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/greyAccent"
android:orientation="vertical" >
<TextView
android:id="#+id/pathways_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/list_of_all_paths"
android:layout_margin="15sp"
app:fontFamily="sans-serif-light"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/vertical_margin"
android:layout_marginStart="#dimen/vertical_margin"
android:layout_marginEnd="#dimen/vertical_margin"
android:layout_marginBottom="#dimen/beetwen_content_line_margin"
android:textSize="#dimen/textSizeMedium"
android:text="#string/list_of_all_paths_description"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/evaluation_background">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/pathways_recyclerview"
android:layout_marginTop="#dimen/beetwen_content_line_margin"
android:layout_marginStart="#dimen/vertical_margin"
android:layout_marginEnd="#dimen/vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
second fragment
public class SecondFragment extends BaseFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_path, container, false);
return view;
}
}
fragment_second.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/mainAppBackgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/mainAppBackgroundColor"
android:orientation="vertical">
<TextView
android:id="#+id/path_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15sp"
app:fontFamily="sans-serif-light"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/list_of_skills"
android:layout_marginTop="15sp"
android:layout_marginStart="15sp"
android:layout_marginEnd="15sp"
app:fontFamily="sans-serif-light"
android:textSize="18sp"
android:textStyle="bold"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/vertical_margin"
android:layout_marginTop="5sp"
android:layout_marginEnd="#dimen/vertical_margin" />
</LinearLayout>
</RelativeLayout>
ViewPager:
<com.habitcoach.android.activity.util.NonSwipeableViewPager
android:id="#+id/hsFragmentsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingBottom="50dp"
android:layout_alignParentLeft="true" />
PageAdapter:
class MainViewViewPageAdapter extends FragmentStatePagerAdapter {
MainViewViewPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
BaseFragment f = (BaseFragment) object;
if (f != null) {
f.updateView();
}
return super.getItemPosition(object);
}
#Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
f = new OneFragment();
break;
case 1:
f = new TwoFragment();
break;
case 2:
f = new ThreeeFragment();
break;
case 3:
f = new FourFragment();
break;
}
return f;
}
#Override
public int getCount() {
return 4;
}
}

I modified your code.check below solution.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.hsFragmentsContainer);
viewPager.setAdapter(new MainViewViewPageAdapter(getSupportFragmentManager()));
}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v4.view.ViewPager
android:id="#+id/hsFragmentsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
MainViewViewPageAdapter.java
public class MainViewViewPageAdapter extends FragmentStatePagerAdapter {
public MainViewViewPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(#NonNull Object object) {
return super.getItemPosition(object);
}
#Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
f = new FirstFragment();
break;
case 1:
f = new SecondFragment();
break;
}
return f;
}
#Override
public int getCount() {
return 2;
}}
FirstFragment.java
public class FirstFragment extends Fragment {
private RecyclerView pathwaysRecyclerView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
initializeViews(view);
initRecyclerView();
return view;
}
private void initializeViews(View view) {
pathwaysRecyclerView = view.findViewById(R.id.pathways_recyclerview);
}
private void initRecyclerView(){
pathwaysRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
PathwaysAdapter adapter = new PathwaysAdapter(new OnPathwayClickListner(),requireContext());
pathwaysRecyclerView.setAdapter(adapter);
}
public class OnPathwayClickListner implements View.OnClickListener {
#Override
public void onClick(View view) {
navigateToSecondFragment();
}
}
private void navigateToSecondFragment(){
Fragment fragment = new SecondFragment();
getFragmentManager().beginTransaction().replace(R.id.fragment_pathways_view, fragment).addToBackStack(null).commit();
}}
SecondFragment.java
public class SecondFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container, false);
return view;
}
}
PathwaysAdapter.java
class PathwaysAdapter extends RecyclerView.Adapter<PathwaysAdapter.MyViewHolder>{
FirstFragment.OnPathwayClickListner onPathwayClickListner;
Context requireContext;
ArrayList<String> list;
public PathwaysAdapter(FirstFragment.OnPathwayClickListner onPathwayClickListner, Context requireContext) {
this.onPathwayClickListner=onPathwayClickListner;
this.requireContext=requireContext;
list=new ArrayList<>();
for (int i=1;i<=20;i++){
list.add("Pathway "+i);
}
}
#NonNull
#Override
public PathwaysAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row, viewGroup, false);
return new PathwaysAdapter.MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PathwaysAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.textview.setText(list.get(i));
}
#Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
RelativeLayout layout;
TextView textview;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
textview = itemView.findViewById(R.id.textview);
layout = itemView.findViewById(R.id.layout);
layout.setOnClickListener(onPathwayClickListner);
}}}
row.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#FF9800"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff" />
</RelativeLayout>

Related

Custom GridView Adapter not displaying

I'm creating a list of "channels" on an app that users can select. I want them in a grid view, (kind of like clickable tiles). There are supposed to be two separate Grids on this Fragment - default channels (channelListSupplied) and ones that the user will have to request subscriptions to (channelListEarned).
I used a custom adapter that I got from another answer here on SO, but I can't get it to work because it's in a Fragment instead of in the Activity, and I'm sure there's some reference I'm not passing correctly.
Below is a list of the relevant pieces of Java and XML...
FragmentChannels.java: (fragment to MainActivity.java)
public class FragmentChannels extends Fragment implements FragmentManager.OnBackStackChangedListener {
ViewGroup container;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
final JSONObject channelList = data.getJSONObject("channels");
final JSONArray channelListSupplied = channelList.getJSONArray("supplied");
final JSONArray channelListEarned = channelList.getJSONArray("earned");
GridView channelViewSupplied = (GridView) view.findViewById(R.id.channel_grid_supplied);
GridView channelViewEarned = (GridView) view.findViewById(R.id.channel_grid_earned);
if (Session.getSessionVar("canHasSpecial").equals("1")) {
FragmentChannels.this.createGridView(channelViewEarned, channelListEarned, FragmentChannels.this.container);
}
FragmentChannels.this.createGridView(channelViewSupplied, channelListSupplied, FragmentChannels.this.container);
...
return view;
}
public void createGridView(final GridView gridView, JSONArray list, final ViewGroup container) throws JSONException {
final String[] gridList = new String[list.length()];
final Activity activity = getActivity();
for (int i = 0; i < list.length(); i++) {
JSONObject channelData = (JSONObject) list.get(i);
gridList[i] = channelData.getString("source_name");
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
gridView.setAdapter(new GridViewAdapter(activity, gridList, container));
}
});
}
public boolean createChannelMenu(Menu menu) {
MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_channels, menu);
super.onCreateOptionsMenu(menu, inflater);
return true;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Activity activity = getActivity();
}
#Override
public void onBackStackChanged() {
}
}
GridViewAdapter.java:
public class GridViewAdapter extends BaseAdapter {
private Context context;
private final String[] textViewValues;
private ViewGroup container;
public GridViewAdapter(Context context, String[] textViewValues, ViewGroup container) {
this.context = context;
this.textViewValues = textViewValues;
this.container = container;
}
#Override
public int getCount() {
return this.textViewValues.length;
}
#Override
public Object getItem(int position) {
return textViewValues[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("CDFF", position+": "+this.textViewValues[position]);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.grid_item_layout, this.container);
TextView titleView = (TextView) gridView.findViewById(R.id.grid_item_title);
titleView.setText(textViewValues[position]);
}
else {
gridView = convertView;
}
return gridView;
}
}
fragment_channels.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:name="FragmentChannels"
android:id="#+id/fragment_channel_container"
tools:context="xx.xxx.xxxx.FragmentChannels"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="My Channels"
android:background="#color/peach"
android:textSize="20sp"
android:textColor="#color/midnightBlue"
android:gravity="center"
android:textAppearance="#style/TextAppearance.FontPath"
/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channel_grid_earned"
android:layout_margin="5dp"
android:columnWidth="180dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channel_grid_supplied"
android:layout_margin="5dp"
android:columnWidth="180dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</FrameLayout>
grid_item_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#color/white"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="150dp"
android:layout_height="100dp"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/grid_item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:ellipsize="marquee"
android:textSize="12sp" />
</RelativeLayout>
As of now, nothing displays on the Fragment except the "My Channels" TextView.
I greatly appreciate any help!
Fixed.
I changed the line:
gridView = inflater.inflate(R.layout.grid_item_layout, this.container);
To:
gridView = inflater.inflate(R.layout.grid_item_layout, null);
in the GridViewAdapter.java class.

How can I add a ViewPager to a Navigation Drawer Item in Android application?

I want to add a viewPager for main fragment in my android app.
This is the xml file for main fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
This is the Java code for Main Fragment
public class MainFragment extends ListFragment {
private static final String TAG = MainFragment.class.getSimpleName();
private StudentsDBHandler studentsDBHandler;
public MainFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
studentsDBHandler = new StudentsDBHandler(getActivity());
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
displayStudents();
return rootView;
}
private void displayStudents() {
List<Student> studentList = studentsDBHandler.getAll();
ArrayAdapter<Student> list = new ArrayAdapter<>(getActivity(), R.layout.item_student, studentList);
setListAdapter(list);
}
}
You have a DrawerLayout page. It's about your drawer contains.
İn the DrawerLayout XML
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
add this code below
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
write this code and setup your View pager like example
viewPager=(ViewPager)findViewById(R.id.viewPager);
FragmentManager fm=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fm));
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position==0)
fragment= new Home1();
if(position==1)
fragment=new Home2();
return fragment;
}
#Override
public int getCount() {
return 2;
}
}
}
I hope Help you

Add FragmentTabHost inside fragment

I have an application with a side menu created from fragments.
The problem comes when I want to create tabs in one of the fragments.
I have tried many examples and not work for me.
fragment_home.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
</LinearLayout>
FragmentHome.java
public class FragmentInicio extends Fragment {
private FragmentTabHost mTabHost;
public FragmentInicio() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_home, container, false);
try{
//HERE TABHOST????
}catch(Exception e){
Log.e("UDI", e.getMessage());
}
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("EVENT", "OnCreate");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
((HomeActivity) activity).onSectionAttached(1);
}
}
What can I do?
Example:
http://webdemo.com.es/sample2.jpg
Thank you!
I know I'm late but this might help someone else.
To use FragmentTabHost inside fragment you can do this.
Create layout resource file
fragment_tab_host.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
FrameLayout with id realtabcontent is which will show current tab content which is in this case FirstFragment, SecondFragment and ThirdFragment
TabHostWidgetFragment.java code will be
public class TabHostWidgetFragment extends Fragment {
private static final String TAG = TabHostWidgetFragment.class.getSimpleName();
public TabHostWidgetFragment() { }
private FragmentTabHost fragmentTabHost;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_frag_tab_host_widget, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
fragmentTabHost = view.findViewById(android.R.id.tabhost);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated: ");
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab"), FirstFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab"), SecondFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab"), ThirdFragment.class, null);
}
public class FirstFragment extends Fragment {
private static final String TAG = FirstFragment.class.getSimpleName();
public FirstFragment() { }
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_first, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
public class SecondFragment extends Fragment {
private static final String TAG = SecondFragment.class.getSimpleName();
public SecondFragment() { }
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_second, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
public class ThirdFragment extends Fragment {
private static final String TAG = ThirdFragment.class.getSimpleName();
public ThirdFragment() { }
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_third, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
}
Layout resources for FristFragment, SecondFragment and ThirdFragment
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/nature1"/>
</LinearLayout>
fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/secondTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/nature2"/>
</LinearLayout>
fragment_third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/thirdTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="#drawable/nature3"/>
</LinearLayout>
If you want to make it swipeable then add ViewPager after FrameLayout with id realtabcontent as written below and implement viewPager logic.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
And TabHostWidgetFragment.java class will be
Create separate or inner MyTabFactory.java class
public class MyTabFactory implements TabHost.TabContentFactory {
private final Context context;
public MyTabFactory(Context context) {
this.context = context;
}
#Override
public View createTabContent(String s) {
View v = new View(context);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
And then add tabs to tabhost like this. This is essential to setContent with MyTabFactory. If you add tabs like I mentioned above in code snippet, there will be issue which is, It will add fragments two times, one because of TabHost and one because of ViewPager.
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab").setContent(new MyTabFactory(getActivity())));
You can either use android.support.v4.app.FragmentTabHost or TabHost It will work same for both.

String Array data not showing in TextView

I'm in the process of learning android app development however I have come to an impasse. My RecyclerView is picking up how many items are in my String array however it is not showing the actual data in text1, any help would be much appreciated.
This is my adapter
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
static String[] fakeData = new String[] {
"One","Two","Three","Four","Five","Ah..Ah..Ah"
};
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.titleView.setText(fakeData[i]);
}
#Override
public int getItemCount() {
return fakeData.length;
}
This is the corresponding XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:event_view="http://schemas.android.com/apk/res-auto"
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="#+id/event_view"
android:layout_marginTop="#dimen/task_card_half_spacing"
android:layout_marginBottom="#dimen/task_card_half_spacing"
android:layout_marginLeft="#dimen/gutter"
android:layout_marginRight="#dimen/gutter"
android:layout_gravity="center"
android:elevation="#dimen/task_card_elevation"
android:foreground="?android:attr/selectableItemBackground"
event_view:cardCornerRadius="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium.Inverse"
android:id="#+id/text1"
android:maxLines="1"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignTop="#+id/image"
android:layout_toRightOf="#+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#android:style/TextAppearance.Inverse"
android:id="#+id/text2"
android:maxLines="3"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignStart="#+id/text1"
android:layout_below="#+id/text1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is my fragment
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
Hm... everything looks ok, can you try setting this to it:
android:textColor="#000000"

ViewPager no swipe issue

I'm tring to create a ViewPager non-swipable. I've wrote this code
MainActivity
public class MainActivity extends FragmentActivity {
ViewPagerAdattatore adattatoreViewPager;
DrawerLayout drawerLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedIstanceState) {
super.onCreate(savedIstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewPager);
drawerLayout=(DrawerLayout)findViewById(R.id.navDrawer);
listaNavDrawer=(ListView)findViewById(R.id.listaDrawer);
adattatoreViewPager=new ViewPagerAdattatore(getSupportFragmentManager());
viewPager.setAdapter(adattatoreViewPager);
}
}
ViewPagerAdattatore
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdattatore extends FragmentPagerAdapter {
public final static int PAGINE=1;
Fragment fragment;
public ViewPagerAdattatore(FragmentManager fragmentManager) {
super(fragmentManager);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0: return new HomePage();
case 1: return new Informazioni();
// case 3: return new TerzaPagina();
}
fragment=new HomePage();
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PAGINE;
}
}
Informazioni
public class Informazioni extends Fragment {
TextView textView;
View view;
public View onCreateView(Bundle savedIstanceState, LayoutInflater inflater, ViewGroup parent) {
view=inflater.inflate(R.layout.home_page, parent, false);
textView=(TextView)view.findViewById(R.id.textView1);
textView.setText("welcome");
return view;
}
}
HomePage as the same code of informazioni
ViewPagerNoSwipe
public class ViewPagerNoSwipe extends ViewPager {
public ViewPagerNoSwipe(Context context) {
super(context);
}
public ViewPagerNoSwipe(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return false;
}
}
activity_main
<?XML version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/navDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mypackage.name.utils.ViewPagerNoSwipe
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ListView
android:id="#+id/listaDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#null"
android:dividerHeight="0dp"
android:background="#2F2F2F"/>
</android.support.v4.widget.DrawerLayout>
HomePage layout
<?XML version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:layout_marginTop="30dp"
android:fontFamily="sans-serif-light"/>
</RelativeLayout>
when I execute this code I don't see the TextView! why? where is the error(s) in my code?
Try this-
public class Informazioni extends Fragment {
TextView textView;
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.home_page, container, false);
textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("welcome");
return view;
}
}

Categories

Resources