gpt4 book ai didi

android - 如何使用 ViewPager 将不同的字符串传递给动态创建的 fragment ?

转载 作者:行者123 更新时间:2023-11-29 00:26:52 25 4
gpt4 key购买 nike

我似乎无法找到一种方法来为我的页面查看器支持的应用程序动态创建的 fragment 提供不同的文本。我已经到了卡壳的地步。对于我的应用程序,我希望有 400-500 个动态创建的 fragment ,您可以在其中水平滑动它们并且 fragment 的每个内容都相同(重复相同的 fragment ),唯一不同的是它们上的文本。这是我在编码时遇到的问题:

package com.example.testarearg;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}

});
}




/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}


@Override
public int getCount() {
// Show 3 total pages.
return 3;
}


}

/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/ private String mText; // display this text in your fragment

public static Fragment getInstance(String text) {
Fragment f = new Fragment();
Bundle args = new Bundle();
args.putString("text", text);
f.setArguments(args);
return f;
}

public void onCreate(Bundle state) {
super.onCreate(state);
setmText(getArguments().getString("text"));
// rest of your code
}

public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}

public String getmText() {
return mText;
}

public void setmText(String mText) {
this.mText = mText;
}
}

}

最佳答案

您需要创建自己的自定义 fragment :

import android.support.v4.app.Fragment;

public class YourFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container, false);

TextView tv = (TextView) v.findViewById(R.id.yourtextview);
tv.setText(getArguments().getString("text"));

return v;
}

public static YourFragment newInstance(String text) {

YourFragment f = new YourFragment();
Bundle b = new Bundle();
b.putString("text", text);

f.setArguments(b);

return f;
}
}

And then modify your adapter. The basic idea is that the adapter contains the Strings that you want to display and the getItem(...) method will choose the text.

public class MyPagerAdapter extends FragmentPagerAdapter {

private String [] strings;

public MyPagerAdapter(FragmentManager fm, String [] stringstodisplay) {
super(fm);
this.strings = stringstodisplay;
}

@Override
public Fragment getItem(int pos) {
return YourFragment.newInstance(strings[pos]);
}

@Override
public int getCount() {
return 500; // or strings.length to be save
}
}

从您的 Activity 中,您将包含不同字符串的字符串数组交给适配器。

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get the content that your fragments will display
String [] strings = new String[500];

for(int i = 0; i < 500; i++) {
strings[i] = "This is Fragment " + i;
}

ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), strings));
}
}

关于android - 如何使用 ViewPager 将不同的字符串传递给动态创建的 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18683583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com