Reputation: 644
CODE:
public class YellowFragment extends Fragment implements FlowerAdapter.FlowerClickListener{
private RecyclerView mRecyclerView;
private FlowerAdapter mFlowerAdapter;
private Button mRefreshButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_yellow, container, false);
mRefreshButton = (Button) view.findViewById(R.id.refreshbutton);
mRefreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//I want this button for refresh, i want my fragment be refreshed when just through click this button.
}
});
if (mFlowerApiService==null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.HTTP.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
mFlowerApiService = retrofit.create(FlowerApiService.class);
}
Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers();
listCall.enqueue(new Callback<List<Flower>>() {
@Override
public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
if (response.isSuccessful()) {
List<Flower> flowerList = response.body();
for(int i =0; i<flowerList.size(); i++) {
Flower flower = flowerList.get(i);
mFlowerAdapter.addFlower(flower);
}
}
}
@Override
public void onFailure(Call<List<Flower>> call, Throwable t) {
}
});
return view;
}
fragment_yellow.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddff00">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/refreshbutton" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_flower"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
I'm making application that shows RecyclerView
using retrofit2
.
That RecyclerView
is on YellowFragment
, but when I open or click that YellowFragment
, the RecyclerView
is refreshed automatically.
I don't want this refreshing this way.
I want to refresh only through a button click (mRefreshButton
).
I had another fragment(BlueFragment
), and that BlueFragment
showing data (which is from my RESTapi and same for YellowFragment
's data) in GridLayout
.
As you can see above code, YellowFragment
shows data in LinearLayout
.
I want these BlueFragment
and YellowFragment
to share the same data.
Therefore I want button to unite refershing.
Maybe i think answer is on FragmentLifecycle
, I don't know the way to solve it. It's too hard.
Question
How to use mRefreshButton
to refresh?
How to make BlueFragment
and YellowFragment
to share data and to refresh at the same time?
Please, I'm waiting for your answer.
Upvotes: 0
Views: 276
Reputation: 2257
"How to use mRefreshButton to refresh?"
you can have a main method , for example loadMyData() for connecting to your web service and refresh your adapter by notifyDatasetChanged method.and you can call this method on your onCreateView and mRefreshButton click listener.
"How to make BlueFragment and YellowFragment to share data and to refresh at the same time?" put a public field in your activity, for example :
public String mySharedString;
you can read and change this data from your fragments this way :
((yourActivityName) getActivity()).mySharedString //do whatever you want
Upvotes: 1