因此,我有一个数组列表,该列表正在从服务器中获取数据,并且该数据显示在回收者视图中。 当我切换活动并返回到包含两个片段的父活动时,Recycler视图仅显示数据。
One of the two fragment class
public class ActiveJobsFragment extends Fragment {
private ActiveJobsAdapter mAdapter;
private RecyclerView recyclerView;
TextView emptyview;
Button create;
SharedPreferences prefs;
View v;
LinearLayoutManager layoutManager;
String message, token;
private static Retrofit retrofit = null;
public ActiveJobsFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activejobsfragment, container, false);
recyclerView = v.findViewById(R.id.active_jobs_recycler);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
emptyview = v.findViewById(R.id.empty_view);
if (prefs != null) {
token = prefs.getString("token", "No token defined");
}
if (CommonObjects.modelList == null) {
recyclerView.setVisibility(View.GONE);
emptyview.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
emptyview.setVisibility(View.GONE);
}
recyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(getActivity());
mAdapter = new ActiveJobsAdapter(getActivity(), CommonObjects.modelList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
mAdapter.SetOnItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getActivity(), ActiveJobCardDetailed.class);
intent.putExtra("project_id",CommonObjects.modelList.get(position).getActive().get(position).getProjectId());
intent.putExtra("bname", CommonObjects.modelList.get(position).getActive().get(position).getBusinsessName());
intent.putExtra("lat", CommonObjects.modelList.get(position).getActive().get(position).getLatitude());
intent.putExtra("long", CommonObjects.modelList.get(position).getActive().get(position).getLongitude());
intent.putExtra("date", CommonObjects.modelList.get(position).getActive().get(position).getCreated() + "-" + CommonObjects.modelList.get(position).getActive().get(position).getExpiry());
intent.putExtra("amount", CommonObjects.modelList.get(position).getActive().get(position).getAmount());
intent.putExtra("address", CommonObjects.modelList.get(position).getActive().get(position).getAddress());
intent.putExtra("reimb", "68");
intent.putExtra("shopper_name", CommonObjects.modelList.get(position).getActive().get(position).getAppliedUser());
startActivity(intent);
}
});
getQuestions();
create = v.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getActivity(), createjobs.class);
onPause();
startActivity(in);
onResume();
}
});
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showMessage(String message) {
Toast.makeText(getActivity(), "" + message, Toast.LENGTH_SHORT).show();
}
private void getQuestions() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).addInterceptor(interceptor).build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(CommonObjects.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
getquestion Service = retrofit.create(getquestion.class);
Call<Question> call = Service.post(token);
call.enqueue(new Callback<Question>() {
@Override
public void onResponse(Call<Question> call, Response<Question> response) {
if (!response.body().getStatus()) {
message = response.body().getMessage();
showMessage(message);
} else {
Question jsonResponse = response.body();
message = response.body().getMessage();
CommonObjects.q.setData(jsonResponse.getData());
CommonObjects.q.setMessage(message);
CommonObjects.q.setStatus(response.body().getStatus());
ArrayList<String> QidList = new ArrayList<String>();
ArrayList<String> QuestionList = new ArrayList<String>();
for (Datum data : jsonResponse.getData()) {
if ((!TextUtils.isEmpty(data.getQId())) &&
(!TextUtils.isEmpty(data.getQuestion()))) {
QidList.add(data.getQId());
QuestionList.add(data.getQuestion());
}
}
CommonObjects.Q_id = QidList.toArray(new String[QidList.size()]);
CommonObjects.Question = QuestionList.toArray(new String[QuestionList.size()]);
for (String s : CommonObjects.Q_id) {
Log.i("tag", s);
}
for (String s : CommonObjects.Question) {
Log.i("tag", s);
}
CommonObjects.Question = QuestionList.toArray(new String[QuestionList.size()]);
}
}
@Override
public void onFailure(Call<Question> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2, t.getMessage()));
}
});
}
}
onCreate
方法protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar5);
prefs = PreferenceManager.getDefaultSharedPreferences(home.this);
setSupportActionBar(toolbar);
if (prefs != null) {
name = prefs.getString("name", "Anne Hataway");
email = prefs.getString("email", "No email defined");
businessname = prefs.getString("address", "No address defined");
profile = prefs.getString("profile", String.valueOf(R.mipmap.icon_podcard_publisher));
city = prefs.getString("city", "No city defined");
token = prefs.getString("token", "No token defined");
id = prefs.getString("id", "No id defined");
location = prefs.getString("location", "No prefs defined");
phone = prefs.getString("phone", "No name defined");
}
CommonObjects.token = token;
CommonObjects.user_id = id;
final DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
mToogle.syncState();
mToogle.setDrawerIndicatorEnabled(false);
ImageView imageViewMenu = findViewById(R.id.drawertoggle);
loadImage(profile, imageViewMenu);
imageViewMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.START);
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
TextView tx = navigationView.getHeaderView(0).findViewById(R.id.ed);
tx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(home.this, EditAccount.class);
onPause();
startActivity(in);
onResume();
}
});
TextView userName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.clientname);
userName.setText(name);
getClient();
CircularImageView mImageView = navigationView.getHeaderView(0).findViewById(R.id.profile);
loadImage(profile, mImageView);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
tabView = findViewById(R.id.home_content);
tabLayout = tabView.findViewById(R.id.tabs);
viewPager = tabView.findViewById(R.id.container);
adapter = new DasboardPagerAdapter(getSupportFragmentManager());
//Add Fragment Adapter
Bundle bundle = new Bundle();
bundle.putString("token", token);
ActiveJobsFragment fragobj = new ActiveJobsFragment();
fragobj.setArguments(bundle);
adapter.AddFragment(fragobj, "ActiveJobs");
adapter.AddFragment(new HistoryFragment(), "History");
viewPager.setAdapter(adapter);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
tabLayout.setSelectedTabIndicator(R.drawable.icon_selector);
}
Adapter
类protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar5);
prefs = PreferenceManager.getDefaultSharedPreferences(home.this);
setSupportActionBar(toolbar);
if (prefs != null) {
name = prefs.getString("name", "Anne Hataway");
email = prefs.getString("email", "No email defined");
businessname = prefs.getString("address", "No address defined");
profile = prefs.getString("profile", String.valueOf(R.mipmap.icon_podcard_publisher));
city = prefs.getString("city", "No city defined");
token = prefs.getString("token", "No token defined");
id = prefs.getString("id", "No id defined");
location = prefs.getString("location", "No prefs defined");
phone = prefs.getString("phone", "No name defined");
}
CommonObjects.token = token;
CommonObjects.user_id = id;
final DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
mToogle.syncState();
mToogle.setDrawerIndicatorEnabled(false);
ImageView imageViewMenu = findViewById(R.id.drawertoggle);
loadImage(profile, imageViewMenu);
imageViewMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.START);
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
TextView tx = navigationView.getHeaderView(0).findViewById(R.id.ed);
tx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(home.this, EditAccount.class);
onPause();
startActivity(in);
onResume();
}
});
TextView userName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.clientname);
userName.setText(name);
getClient();
CircularImageView mImageView = navigationView.getHeaderView(0).findViewById(R.id.profile);
loadImage(profile, mImageView);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
tabView = findViewById(R.id.home_content);
tabLayout = tabView.findViewById(R.id.tabs);
viewPager = tabView.findViewById(R.id.container);
adapter = new DasboardPagerAdapter(getSupportFragmentManager());
//Add Fragment Adapter
Bundle bundle = new Bundle();
bundle.putString("token", token);
ActiveJobsFragment fragobj = new ActiveJobsFragment();
fragobj.setArguments(bundle);
adapter.AddFragment(fragobj, "ActiveJobs");
adapter.AddFragment(new HistoryFragment(), "History");
viewPager.setAdapter(adapter);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
tabLayout.setSelectedTabIndicator(R.drawable.icon_selector);
}
ActiveJobFragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/container1"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/snow">
<android.support.v7.widget.RecyclerView
android:id="@+id/active_jobs_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_gravity="top"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"
android:gravity="center"
android:textColor="@color/soblue"
android:text="No Data Available" />
<LinearLayout
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:layout_weight="2"
android:text="Want To create new project here?"
android:textColor="@color/colorText"
android:textSize="19sp" />
<Button
android:id="@+id/create"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="Create"
android:textColor="@color/colorPrimary"></Button>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>