У меня странная проблема с ViewPager, я следил за этой страницей https://developer.android.com/training/animation/screen-slide,, но вместо этого я использую RecyclerView для своей второй страницы фрагмента. Первая страница, представляющая собой простой фрагмент структуры кадра, отображается правильно. Но страница повторного просмотра полностью загромождена (пролистывание влево). Это как первая страница ниже, а вторая копируется несколько раз сверху, см. screendump . Кто-нибудь, кто признает эту проблему?
The ViewPager (callerpager)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:id="@+id/caller_pager" android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
The RecyclerView (caller_log_list)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="@+id/caller_log_list_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" />
The RecyclerViewItem (caller_log_list_item)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintHeight_default="wrap">
<TextView
android:id="@+id/logcreated"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginStart="4dp"
android:text="Oppretta"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/log"
android:layout_width="211dp"
android:layout_height="39dp"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:text="Logg"
app:layout_constraintStart_toEndOf="@+id/logcreated"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/logbillable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fakturerbar"
app:layout_constraintStart_toEndOf="@+id/log"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/loggedby"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginStart="4dp"
android:text="Logget av"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logcreated" />
</android.support.constraint.ConstraintLayout>
CallLoggerListFragment
public class CallerLogListFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.caller_log_list, container, false);
return rootView;
}
@Override
public void onPause() {
super.onPause();
}
private void refreshFragment() {
recyclerView = (RecyclerView) getView().findViewById(R.id.caller_log_list_view);
layoutManager = new LinearLayoutManager(getContext());
try {
JSONArray log = PMCallogHandler.getCurrentLogList();
if (log.length()>0) {
adapter = new MyAdapter(log);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && getView()!=null) {
System.out.println("User in the Fragment2");
refreshFragment();
}
else {
// User is not viewing the fragment,
// or fragment is our of the screen
System.out.println("User left Fragment2");
}
}
@Override
public void onResume() {
super.onResume();
refreshFragment();
}
}
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private JSONArray dataset;
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView createdDate;
private TextView loggedBy;
private TextView log;
private CheckBox isBillable;
public MyViewHolder(View v) {
super(v);
createdDate = (TextView)v.findViewById(R.id.logcreated);
loggedBy = (TextView)v.findViewById(R.id.loggedby);
log = (TextView)v.findViewById(R.id.log);
isBillable=(CheckBox)v.findViewById(R.id.logbillable);
}
public void setCreatedDate(String date) {
createdDate.setText(date);
}
public void setLoggedBy(String loggedBy) {
this.loggedBy.setText(loggedBy);
}
public void setLog(String log) {
this.log.setText(log);
}
public void setBillable(boolean isBillable) {
this.isBillable.setChecked(isBillable);
}
}
public MyAdapter(JSONArray myDataset) {
dataset = myDataset;
}
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.caller_log_list_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
JSONObject rw = null;
try {
rw = dataset.getJSONObject(position);
holder.setBillable(rw.getBoolean("billable"));
holder.setCreatedDate(rw.getString("created"));
holder.setLog(rw.getString("title"));
holder.setLoggedBy("...");
}catch(Exception e) {
e.printStackTrace();
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return dataset.length();
}
}
The LoggingActivity
public class LoggingActivity extends FragmentActivity {
private ViewPager pager = null;
private PagerAdapter pagerAdapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.caller_pager);
pager = (ViewPager) findViewById(R.id.caller_pager);
pagerAdapter = new CallerLogPageAdapter(getSupportFragmentManager(),getIntent().getExtras().getString("logResponse"));
pager.setAdapter(pagerAdapter);
//pager.addOnPageChangeListener();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
if (pager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private class CallerLogPageAdapter extends FragmentStatePagerAdapter {
private String logResponse;
public CallerLogPageAdapter(FragmentManager fm, String logResponse) {
super(fm);
this.logResponse=logResponse;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0){
return "New Contact log";
}
else {
return "Contact logs";
}
}
@Override
public Fragment getItem(int position) {
Fragment ret = null;
if(position==0) {
ret = new CallerLogFragment();
Bundle args = new Bundle();
args.putString("logResponse", logResponse.toString());
ret.setArguments(args);
} else if(position==1) {
ret = new CallerLogListFragment();
Bundle args = new Bundle();
args.putString("contactLog", "");
ret.setArguments(args);
}
return ret;
}
@Override
public int getCount() {
return 2;
}
}
}