Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Created September 5, 2016 12:28
Show Gist options
  • Save hkurokawa/c0dcf1f1b59ae054a34bbb7f98e95529 to your computer and use it in GitHub Desktop.
Save hkurokawa/c0dcf1f1b59ae054a34bbb7f98e95529 to your computer and use it in GitHub Desktop.
Code to reproduce a problem of InfiniteViewPager
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hiroshi.adaptersample.MainActivity">
<Button
android:id="@+id/reset_adapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset Adapter"/>
<com.antonyt.infiniteviewpager.InfiniteViewPager
android:id="@+id/pager"
android:layout_below="@id/reset_adapter"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hiroshi.adaptersample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
package com.example.hiroshi.adaptersample;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.antonyt.infiniteviewpager.InfinitePagerAdapter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), new String[]{"old 1", "old 2", "old 3", "old 4"});
final PagerAdapter wrapper = new InfinitePagerAdapter(adapter);
pager.setAdapter(wrapper);
final Button button = (Button) findViewById(R.id.reset_adapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pager.setAdapter(
new InfinitePagerAdapter(new MyPagerAdapter(getSupportFragmentManager(), new String[]{"new 1", "new 2", "new 3", "new 4"})));
// adapter.setItems(new String[]{"new 1", "new 2", "new 3", "new 4"});
// adapter.notifyDataSetChanged();
}
});
}
private static class MyPagerAdapter extends FragmentStatePagerAdapter {
private String[] items;
MyPagerAdapter(FragmentManager fm, String[] items) {
super(fm);
this.items = items;
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(items[position]);
}
@Override
public int getCount() {
return items.length;
}
public void setItems(String[] items) {
this.items = items;
}
}
public static class MyFragment extends Fragment {
public static Fragment newInstance(String item) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putString("name", item);
f.setArguments(args);
return f;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
TextView text = (TextView) view.findViewById(R.id.text);
text.setText(getArguments().getString("name"));
return view;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment