如何使用动态片段制作滑动视图

时间:2019-05-20 13:52:44

标签: java android

我正在尝试使用viewPager和FragmentStatePagerAdapter创建滑动视图。我的片段应该是动态创建的,每个片段都包含一个listview。所以我有一个ArrayList在创建时应该添加一个新的片段。我有一个自定义适配器类,一个片段类和一个活动类。

  

SubPagerAdapter.java

public class SubPagerAdapter extends FragmentPagerAdapter {

    private final int MAXSUB = 10;

    private ArrayList<TopicFragment> fragments = new ArrayList<>();

    public SubPagerAdapter(FragmentManager fm, ArrayList<TopicFragment> fragments1) {
        super(fm);
        this.fragments = fragments1;
    }

    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    public TopicFragment getItem(int i) {
        return fragments.get(i);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        // refresh all fragments when data set changed
        return PagerAdapter.POSITION_NONE;
    }

    public CharSequence getPageTitle(int position) {
        return "PAGE" + position;
    }
}
  

TopicFragment.java

public class TopicFragment extends Fragment {
    ArrayList<String> dataArray = new ArrayList<String>();
    PubsubAndroid clien;
    PubsubAndroid.Subscription new_sub;
    ArrayAdapter<String> displayData;
    ListView listview;
    private String title;

    public static TopicFragment newInstance(int position) {
        Bundle args = new Bundle();
        args.putInt("position", position);
        TopicFragment f = new TopicFragment();
        f.setArguments(args);
        return f;
    }

    public String getTitle() {
        return this.title;
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        ViewGroup v = (ViewGroup) inflater.inflate(R.layout.sub_fragment, container, false);
        listview = v.findViewById(R.id.listSubscribe);
        return v;
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

        Intent intent = Objects.requireNonNull(getActivity()).getIntent();
        String path = intent.getStringExtra("topic-path");
        Bundle bundle = intent.getExtras();
        assert bundle != null;
        client = bundle.getParcelable("pubsub_client");
        displayData = new ArrayAdapter<>(Objects.requireNonNull(getContext()), android.R.layout.simple_list_item_1, dataArray);
        listview.setAdapter(displayData);

        SubscribeListener listen = new SubscribeListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onResponse(String responseText) {
                String time = sdf.format(Calendar.getInstance().getTime());
                dataArray.add(time + ":   " + responseText);
                Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        displayData.notifyDataSetChanged();
                        listview.setSelection(displayData.getCount() - 1);
                    }
                });
            }

            @Override
            public void onError() {
                Toast toast = Toast.makeText(getContext(), "ERROR", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        };

        new_sub = client.new Subscription(path, listen);
        new_sub.subscribe();
    }
}

0 个答案:

没有答案