I'm working on an application that needs to have a Custom list in a fragment.
I try a lot of thing in vain... This is my best result:
MainActivity.java :
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_list);
if(findViewById(R.id.fragment_container) != null )
{
if(savedInstanceState != null)
return;
}
ListFragment listFragment = new ListFragment();
listFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, listFragment).commit();
}
}
stops.java:
public class stops {
private String name;
private String destination;
private String schedule;
private int iconID;
public stops(String name, String destination, String schedule, int iconID)
{
super();
this.name = name;
this.destination = destination;
this.schedule = schedule;
this.iconID = iconID;
}
public String getName(){ return name; }
public String getDestination(){ return destination; }
public String getSchedule(){ return schedule; }
public int getIconID(){ return iconID; }
}
ListFragment.java:
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListFragment extends Fragment{
private List<stops> mystops = new ArrayList<>();
ArrayAdapter<stops> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
populateStopList();
View V = inflater.inflate(R.layout.fragment_list, container, false);
Context context = getActivity().getApplicationContext();
adapter = new myListAdapter(context);
ListView list = (ListView) V.findViewById(R.id.stopsList);
list.setAdapter(adapter);
return V;
}
private void populateStopList() {
mystops.add(new stops("Corum", "Odysseum", "5min", R.drawable.green_tram));
mystops.add(new stops("Corum", "Mosson", "2min", R.drawable.orange_tram));
mystops.add(new stops("Corum", "Saint-Jean de Vedas", "1min", R.drawable.red_tram));
}
private class myListAdapter extends ArrayAdapter<stops> {
public myListAdapter(Context context) {
super(context, R.layout.item_view, mystops);
add(new stops("Corum", "Mosson", "2min", R.drawable.green_tram));
populateStopList();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null)
itemView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false);
stops currentStop = mystops.get(position);
ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentStop.getIconID());
TextView nameText = (TextView)itemView.findViewById(R.id.item_nameTextView);
nameText.setText(currentStop.getName());
TextView destinationText = (TextView) itemView.findViewById(R.id.item_destinationTextView);
destinationText.setText(currentStop.getDestination());
TextView timeText = (TextView) itemView.findViewById(R.id.item_timeTextView);
timeText.setText(currentStop.getSchedule());
return itemView;
}
}
}
fragment_list.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" tools: context="com.example.msa_m.listsstops.ListFragment"
android:id= "#+id/fragment_container"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT DU LAYOUT FRAGMENT"
android:id="#+id/textView"
android:layout_gravity="center_horizontal|top" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stopsList"
android:layout_alignParentEnd="true"
/>
</FrameLayout>
item_view.xml:
<?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"
android:id ="#+id/my_list_item">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_icon"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:src="#drawable/green_bus"
android:maxWidth="80dp"
android:minHeight="80dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nom de l'arret"
android:id="#+id/item_nameTextView"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/item_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Destination"
android:layout_marginTop="5dp"
android:layout_marginStart="10dp"
android:id="#+id/item_destinationTextView"
android:layout_below="#+id/item_nameTextView"
android:layout_toEndOf="#+id/item_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Temps"
android:id="#+id/item_timeTextView"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout><?xml version="1.0" encoding="utf-8"?>
But when I run it, this endless load with a ProgressBar in the middle of the screen...
Does someone can help me ?
Ok, since your code works fine for me, heres a modified version of your project.
Your Activity Layout could probably look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then, in your activity, you just inflate the activity layout and replace the container with your fragment:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListFragment listFragment = new ListFragment();
listFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, listFragment)
.commit();
}
}
And your fragment takes care of inflating the list ui and managing all the data:
public class ListFragment extends Fragment{
ListView listView;
ArrayAdapter<stops> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<stops> mystops = new ArrayList<>();
mystops.add(new stops("Corum", "Odysseum", "5min", R.drawable.green_tram));
mystops.add(new stops("Corum", "Mosson", "2min", R.drawable.orange_tram));
mystops.add(new stops("Corum", "Saint-Jean de Vedas", "1min", R.drawable.red_tram));
adapter = new myListAdapter(getActivity(), mystops);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
listView = (ListView) view.findViewById(R.id.stopsList);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView.setAdapter(adapter);
}
private static class myListAdapter extends ArrayAdapter<stops> {
public myListAdapter(Context context, ArrayList<stops> stopList) {
super(context, R.layout.item_view, stopList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null)
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
stops currentStop = getItem(position);
ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentStop.getIconID());
TextView nameText = (TextView)itemView.findViewById(R.id.item_nameTextView);
nameText.setText(currentStop.getName());
TextView destinationText = (TextView) itemView.findViewById(R.id.item_destinationTextView);
destinationText.setText(currentStop.getDestination());
TextView timeText = (TextView) itemView.findViewById(R.id.item_timeTextView);
timeText.setText(currentStop.getSchedule());
return itemView;
}
}
Related
Edit: I changed my code as per the suggestion, but my app still crashes. I posted the error to the bottom of the question
I have been trying for 2 days to send an edittext data from a fragment to the main activity with a click of a button that is not in the fragment (it is in the host activity), and having trouble doing this even though I feel like the answer is probably simple!
There are answers on here that suggest an interface but I can not find any examples that don't use a button from in the fragment. When I attempt to find a button outside of the fragment and then send the data I get a runtime error.
My code
FormActivity.java, the part of the code that is supposed to receive the string is at the bottom in submitFormEntries
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.material.tabs.TabLayout;
public class FormActivity extends AppCompatActivity{
private static final String TAG = "PROCESS";
TabLayout tabLayout;
ViewPager viewPager;
oneFragment fragA = new oneFragment(); // this line can cause crashes
//or set to 0 and change it dynamically in the onPageSelected method
private int numFrags = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formactivity);
viewPager = findViewById(R.id.viewPager);
tabLayout = findViewById(R.id.tabLayout);
getTabs();
//Add onPageChangeListener to get the position of the current tab and change the button text based on the position
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
Button back = findViewById(R.id.backButton);
Button next = findViewById(R.id.nextButton);
Button mainReturn = findViewById(R.id.mainReturn);
Button submit = findViewById(R.id.submitButton);
PagerAdapter adapter = viewPager.getAdapter();
//numFrags = viewPager.getAdapter().getCoBunt() -1; - dynamically get new frags
if (position == 0) {
back.setVisibility(View.GONE);
next.setVisibility(View.VISIBLE);
mainReturn.setVisibility(View.VISIBLE);
}
else if (adapter != null && position == numFrags) {
back.setVisibility(View.VISIBLE);
next.setVisibility(View.GONE);
submit.setVisibility(View.VISIBLE);
}
else {
back.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
mainReturn.setVisibility(View.GONE);
submit.setVisibility(View.GONE);
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
//Add fragments to viewPager using the object ViewPagerAdapter
public void getTabs(){
final ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
new Handler().post(new Runnable() {
#Override
public void run() {
viewPagerAdapter.addFragment(oneFragment.getInstance(),null); // `oneFragment.getInstance()` should be inside `FragmentPagerAdapter.getItem()`
viewPagerAdapter.addFragment(twoFragment.getInstance(),null); // `twoFragment.getInstance()` should be inside `FragmentPagerAdapter.getItem()`
viewPagerAdapter.addFragment(threeFragment.getInstance(),null); // `threeFragment.getInstance()` should be inside `FragmentPagerAdapter.getItem()`
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
});
}
//METHODS FOR THE BUTTON LISTENERS
public void goBack(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem()-1);
}
public void goNext(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
public void submitFormEntries(View view) {
Intent intent = getIntent();
String message = intent.getStringExtra("Answer One");
Log.d(TAG, message);
}
public void returnToMainMenu(View view) {
Log.d(TAG, "returnToMainMenu: ");
}
}
My fragment code oneFragment.java:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class oneFragment extends Fragment{
private EditText answer_One;
public static oneFragment getInstance() {
oneFragment oneFragment = new oneFragment();
return oneFragment;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page_one, container, false);
final Button submit = (Button) getActivity().findViewById(R.id.submitButton);
answer_One = (EditText) view.findViewById(R.id.answerOne);
if(answer_One != null){
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(requireActivity().getBaseContext(),
FormActivity.class);
intent.putExtra("Answer One", answer_One.toString());
getActivity().startActivity(intent);
}
});
}
return view;
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}
ViewPagerAdaper.java
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragmentList = new ArrayList<>(); // this line can cause crashes
private List<String> stringList = new ArrayList<>();
public ViewPagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return stringList.get(position);
}
public void addFragment(Fragment fragment, String title){
fragmentList.add(fragment); // this line can cause crashes
stringList.add(title);
}
}
My error messages, edited to reflect answer
2021-01-07 19:08:07.767 8786-8786/net.larntech.tablayout E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.larntech.tablayout, PID: 8786
java.lang.IllegalStateException: Fragment oneFragment{9ca3c26} (7a20c5f2-8918-4de4-98a6-bed63353b415)} not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:833)
at net.larntech.tablayout.oneFragment$1.onClick(oneFragment.java:37)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
EDIT: Here are the xmls
The main activity: activity_formctivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".FormActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="goBack"
android:visibility="gone"/>
<Button
android:id="#+id/mainReturn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mainReturn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="returnToMainMenu"
android:visibility="visible"/>
<Button
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="goNext"/>
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="submitFormEntries"
android:visibility="gone"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="#drawable/tab_selector"
app:tabIndicatorHeight="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
fragment xml file page_one.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="#+id/titleOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="#string/title_one"
android:textColor="#color/colorPrimaryDark"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/questionOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="#string/questionOne"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/descriptionOne"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/descriptionOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="#string/descriptionOne"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/answerOne"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/answerOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/answerHintOne"
android:inputType="textMultiLine"
android:maxLines="4"
android:minLines="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I need to do this with quite a few fragments by the way, so solutions like bundles and sharedprefs are not ideal. If the answer is to set up an interface, how do I go about doing this?
In your fragmentOne, you can't call getView() in onCreateView as the fragment view is not created yet; actually onCreateView() creates the fragment View and returns it.
So to solve this, you need to use the inflated view itself instead.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page_one, container, false);
// answer_One = (EditText) getView().findViewById(R.id.answerOne); // returns NPE
answer_One = (EditText) view.findViewById(R.id.answerOne); // << Use this
...
UPDATE
You need to access activity's button in onResume of the fragment as below
#Override
public void onResume() {
super.onResume();
final Button submit = (Button) (FormActivity getActivity()).findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(requireActivity().getBaseContext(),
FormActivity.class);
intent.putExtra("Answer One", answer_One.toString());
getActivity().startActivity(intent);
}
});
}
And remove the button and its listener from onCreateView() method
The correct way to implement a regular FragmentPagerAdapter is:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
if(position == 0) return new OneFragment();
if(position == 1) return new TwoFragment();
if(position == 2) return new ThreeFragment();
throw new IllegalStateException("Unexpected position " + position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0) return "ONE";
if(position == 1) return "TWO";
if(position == 2) return "THREE";
throw new IllegalStateException("Unexpected position " + position);
}
}
Then, to communicate from Fragment to Activity, the current Google-recommended approach is to use an Activity-scoped ViewModel that hosts a MutableLiveData.
public class MyViewModel extends ViewModel {
private final SavedStateHandle handle;
public MyViewModel(SavedStateHandle handle) {
this.handle = handle;
}
public final MutableLiveData<String> data = handle.getLiveData("data", "");
}
Then
public class MyActivity extends AppCompatActivity {
private MyViewModel myViewModel;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
}
}
And
public class MyFragment extends Fragment {
private MyViewModel myViewModel;
#Override
protected void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
}
}
Then you can mutate the LiveData and observe it for changes.
I am following this tutorial on how to create Swipe Views using a View Pager and am trying to convert it to work with fragments.
My issue is that the cards are not appearing when I select my fragment.
I think the issue lies in this line of code
adapter = new Adapter(models, getActivity());
(originally adapter = new Adapter(models, this); in the tutorial), but I could be wrong.
I am relatively new to android studio and this is my first time using fragments, any help would be greatly appreciated!
Here is my code:
fragment_cards.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#android:color/background_light"
>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:foregroundGravity="center"
android:overScrollMode="never"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"></androidx.viewpager.widget.ViewPager>
<Button
android:id="#+id/button_shuffle"
android:text="SHUFFLE"
android:textColor="#fff"
android:background="#drawable/round"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_marginBottom="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:layout_constraintBottom_toBottomOf="#+id/viewPager"
app:layout_constraintEnd_toEndOf="#+id/viewPager"
app:layout_constraintStart_toStartOf="#+id/viewPager">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:id="#+id/image"
android:src="#drawable/brochure"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:id="#+id/title"
android:textColor="#262626"
android:layout_below="#id/image"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:text="Brochure"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/desc"
android:layout_below="#id/title"
android:layout_marginTop="3dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:maxLines="3"
android:drawablePadding="10dp"
android:ellipsize="end"
android:text="Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
CardsFragment.java
package com.example.musicassistant;
import android.animation.ArgbEvaluator;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class CardsFragment extends Fragment {
ViewPager viewPager;
Adapter adapter;
List<Model> models;
Integer[] colors = null;
ArgbEvaluator argbEvaluator = new ArgbEvaluator();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
models = new ArrayList<>();
models.add(new Model(R.drawable.brochure, "Brochure", "oooooo"));
models.add(new Model(R.drawable.sticker, "Sticker", "oooooo"));
models.add(new Model(R.drawable.poster, "Poster", "oooooo"));
models.add(new Model(R.drawable.namecard, "Namecard", "oooooo"));
adapter = new Adapter(models, getActivity());
viewPager = inflater.inflate(R.layout.fragment_cards, container, false).findViewById(R.id.viewPager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);
Integer[] colors_temp = {
getResources().getColor(R.color.color1),
getResources().getColor(R.color.color2),
getResources().getColor(R.color.color3),
getResources().getColor(R.color.color4)
};
colors = colors_temp;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position < (adapter.getCount() -1) && position < (colors.length -1)) {
viewPager.setBackgroundColor(
(Integer) argbEvaluator.evaluate(
positionOffset,
colors[position],
colors[position + 1]
)
);
} else {
viewPager.setBackgroundColor(colors[colors.length - 1]);
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return inflater.inflate(R.layout.fragment_cards, container, false);
}
}
Adapter.java
package com.example.musicassistant;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class Adapter extends PagerAdapter {
private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;
public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
}
#Override
public int getCount() {
return models.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.item, container, false);
ImageView imageView;
TextView title, desc;
imageView = view.findViewById(R.id.image);
title = view.findViewById(R.id.title);
desc = view.findViewById(R.id.desc);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDesc());
container.addView(view, 0);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
}
Model.java
package com.example.musicassistant;
public class Model {
private int image;
private String title;
private String desc;
public Model(int image, String title, String desc) {
this.image = image;
this.title = title;
this.desc = desc;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
It is worth noting that the fragment itself is working, as the button in it is appearing correctly.
The only issue is that my cards are not showing up at all.
Any help would be greatly appreciated as this is driving me absolutely insane!
Thank you very much for your time I greatly appreciate it,
Roman
Inflate will create a new layout object(view object) eveytime you use it so this is one view layout
viewPager = inflater.inflate(R.layout.fragment_cards, container, false).findViewById(R.id.viewPager);
and you are returning a new view object which has nothing
return inflater.inflate(R.layout.fragment_cards, container, false);
so you need to store a single reference of view after inflating then use it and return it, as
View root = inflater.inflate(R.layout.fragment_cards, container, false).findViewById(R.id.viewPager);
viewPager = root.findViewById(R.id.viewPager);
// ... code
return root;
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>
So, for a personal project I'm making a stream app for Android which are separated by three fragments in the main screen where each of those fragment will be a list of a type of show in our archive, like movies, TV series and animations by using a RecyclerView inside of them.
That being said, after some errors that I managed to fix, the app is not showing what was expected, by skipping the RecyclerView because it has no "adapter".
Here is the code so far:
The main activity:
public class MenuPrincipal extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_principal);
AbasMenuPrincipalAdapter adapter = new AbasMenuPrincipalAdapter( getSupportFragmentManager() );
adapter.adicionar( new FilmesFragment() , "Filmes");
adapter.adicionar( new SeriesFragment() , "Series");
adapter.adicionar( new AnimacoesFragment() , "Animações");
ViewPager viewPager = (ViewPager) findViewById(R.id.abas_view_pager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.abas);
tabLayout.setupWithViewPager(viewPager);
}
}
One of the fragments used on the main screen:
public class AnimacoesFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<Item> exemploItemList = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.animacoes_fragment, container, false);
TextView tv = view.findViewById(R.id.text1);
tv.setText("Você está na terceira aba");
exemploItemList.add(new Item(R.drawable.ic_android, "Line1", "line2"));
exemploItemList.add(new Item(R.drawable.ic_desktop_mac, "Line2", "line3"));
exemploItemList.add(new Item(R.drawable.ic_laptop_windows, "Line4", "line5"));
mRecyclerView = (RecyclerView) view.findViewById(R.id.AnimacaoRecyclerView);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
ItemAdapter adapter = new ItemAdapter(exemploItemList);
mAdapter = adapter;
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
And its layout:
<?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"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/AnimacaoRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:scrollbars="vertical" />
</LinearLayout>
This is what I'm trying to show on that RecyclerView (later will be the real thing, now it's just a test):
public class Item {
private int mImageResource;
private String text1;
private String text2;
public Item(int mImageResource, String text1, String text2) {
this.mImageResource = mImageResource;
this.text1 = text1;
this.text2 = text2;
}
public int getmImageResource() {
return mImageResource;
}
public void setmImageResource(int mImageResource) {
this.mImageResource = mImageResource;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
It's layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/imagem"
android:padding="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="0dp"
android:text="#string/line1"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/textView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="28dp"
android:text="#string/line2"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/textView2"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Finally, the adapter used to convert the list:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private ArrayList<Item> mItemList;
public static class ItemViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
ItemViewHolder ivh = new ItemViewHolder(v);
return ivh;
}
public ItemAdapter(ArrayList<Item> itemList){
mItemList = itemList;
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item currentItem = mItemList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
#Override
public int getItemCount() {
return mItemList.size();
}
}
First of all, check your fragment layout file. The linear layout parent orientation set to 'horizontal' and width of it's child views ('textview' & 'recyclerview') is match parent. This won't work.
Try to initialize your recyclerview adapter in 'onCreate' and use 'notifyDataSetChanged()' after setting up the adapter with recyclerview.
I am a beginner, new to android technology. Actually I tried to solve already existing question through different approach. I took 3 fragments in single activity, and in 1st fragment i took editText, in 2nd fragment I took button and after clicking on that button I tried to display the fragment1 data in 3rd fragment By passing value through Bundle and I got stuck. How to do this task by this approach.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1"
>
</FrameLayout>
<FrameLayout
android:id="#+id/mid"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1"
>
</FrameLayout>
<FrameLayout
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1"
>
</FrameLayout>
</LinearLayout>
MainActivity
package com.example.com.dynamicfragmentproject;
import android.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Top_fragment frg1=new Top_fragment();
transaction.add(R.id.top,frg1);
transaction.commit();
}
public void cacheData(String str) {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Mid_fragment frg2=new Mid_fragment();
transaction.add(R.id.mid,frg2);
transaction.commit();
Bundle bundle = new Bundle();
bundle.putString("editText",str);
frg2.setArguments(bundle);
}
public void cacheData1(String name) {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Bottom_fragment frg3=new Bottom_fragment();
transaction.add(R.id.bottom,frg3);
transaction.commit();
Bundle bundle = new Bundle();
bundle.putString("editText",name);
frg3.setArguments(bundle);
}
}
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Top_fragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/hello_blank_fragment"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/colorPrimary"/>
</RelativeLayout>
Fragment1.java
package com.example.com.dynamicfragmentproject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class Top_fragment extends Fragment {
private EditText editText;
View view;
public Top_fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_top_fragment, container, false);
editText = view.findViewById(R.id.editText1);
String str = editText.getText().toString();
MainActivity main = (MainActivity) getActivity();
main.cacheData(str);
return view;
}
}
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Mid_fragment">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:text="Submit"
android:textAllCaps="false"
android:textColor="#color/colorPrimary"
android:layout_centerVertical="true"
android:layout_centerHorizontal="false"
/>
</RelativeLayout>
Fragment2.java
package com.example.com.dynamicfragmentproject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Mid_fragment extends Fragment {
private Button buttonSubmit;
View view;
public Mid_fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_mid_fragment, container, false);
buttonSubmit = view.findViewById(R.id.button1);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity main1 = (MainActivity) getActivity();
Bundle bundle = getArguments();
String name = bundle.getString("editText");
main1.cacheData1(name);
}
});
return view;
}
}
fragment3.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottom"
tools:context=".Bottom_fragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimary"
android:padding="10dp"
/>
</FrameLayout>
Fragment3.java
package com.example.com.dynamicfragmentproject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class Bottom_fragment extends Fragment {
private TextView viewText;
View view;
public Bottom_fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_bottom_fragment, container, false);
viewText = view.findViewById(R.id.textView1);
Bundle bundle = getArguments();
String name = bundle.getString("editText");
viewText.setText(name);
return view;
}
}
In first fragment create a object of second fragment.
FragmentTwo fragmenttwo=new FragementTwo();
Bundle bundle = new Bundle();
bundle.putSerializable(object,"data")
fragmenttwo.setArguments(bundle);
In the second fragment
Bundle bundle = getArguments();
if(bundle != null){
SampleModel model = (SampleModel) bundle.getSerializable("data");
}
You need to change code in MainActivity, you commit() before set args. Change code in whole app like below
public void cacheData(String str) {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Mid_fragment frg2=new Mid_fragment();
Bundle bundle = new Bundle();
bundle.putString("editText",str);
frg2.setArguments(bundle);
transaction.add(R.id.mid,frg2);
transaction.commit();
}
And get data on Fragment onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
You can commit a transaction using commit() only prior to the activity saving its state (when the user leaves the activity). If you attempt to commit after that point, an exception is thrown. This is because the state after the commit can be lost if the activity needs to be restored. For situations in which it's okay that you lose the commit, use commitAllowingStateLoss().
For more detail you may refer this link
Updated :
In your code pass bundle value is perfect but when you get string of editext in Mid_fragment
getArguments().getString("editText");
this getting empty that's why solution is you do not need to pass Top fragment value to Mid Fragment just edittext make static like this
change public static EditText editText
Top_fragment extends Fragment {
View view;
public static EditText editText;
}
and Mid_fragment add this
onClick(View v) {
if (Top_fragment.editText!=null)
strtext=Top_fragment.editText.getText().toString();
here is full code of Mid_fragment
public class Mid_fragment extends Fragment {
View view;
private Button buttonSubmit;
public Mid_fragment() {
// Required empty public constructor
}
String strtext="";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_mid_fragment, container, false);
//strtext = getArguments().getString("editText");
buttonSubmit = view.findViewById(R.id.button1);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Top_fragment.editText!=null)
strtext=Top_fragment.editText.getText().toString();
MainActivity main1 = (MainActivity) getActivity();
main1.cacheData1(strtext);
}
});
return view;
}
}