我使用片段和分页器创建了一个简单的选项卡视图示例。我希望在用户使用选项卡按钮时发送数据从一个片段发送到另一个片段。我将在一个选项卡中为您提供更多详细信息,我有一个列表视图。在那里我拿一个数组列表来显示列表视图中的数据。我想在另一个片段中发送该列表视图。所以我创建一个接口并在main活动上实现它。但是我不知道如何发送哪个事件来发送片段。当用户选择选项卡我想将数组列表发送到第二个片段
这是我的代码 的 mainActivity
var numberFormat = us.NumberFormat;
string decimalSeparator = numberFormat.NumberDecimalSeparator;
string thousandsSeparator = numberFormat.NumberGroupSeparator;
fragmentone
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ViewPager viewPager;
FragmentpagerAdapter fragmentpagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar =getActionBar();
viewPager = (ViewPager) findViewById(R.id.pager);
fragmentpagerAdapter =new FragmentpagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(fragmentpagerAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Stations").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("fav Station").setTabListener(this));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
Fragmentone fragmentOne = (Fragmentone) getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.pager,0));
// get your arraylist using method of FragmentOne
ArrayList<DataModel> yourArrayList = fragmentOne.getData();
// refer your second fragment and set the above arraylist in that
FragmentTwo fragmentTwo = (FragmentTwo) getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.pager, 1));
fragmentTwo.setData(yourArrayList);
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
private static String makeFragmentName(int viewPagerId, int index) {
return "android:switcher:" + viewPagerId + ":" + index;
}
}
FragmentTwo
public class Fragmentone extends Fragment {
ArrayList<DataModel> name;
boolean isPressed=false;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
name=new ArrayList<DataModel>();
name.add(new DataModel("First Station",false));
name.add(new DataModel("Second Station",false));
ListView listView = (ListView) view.findViewById(R.id.list_view);
CustomAdapter customAdapter =new CustomAdapter(getActivity(),name);
listView.setAdapter(customAdapter);
return view;
}
public ArrayList getData() {
return name;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
PagerAdaper
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void methodInFragmentB(ArrayList arrayList){
for (int i=0;i<arrayList.size();i++){
}
}
public void setData(ArrayList<DataModel> yourArrayList){
Toast.makeText(getActivity(), "ArrayList Size: " + yourArrayList.size(), Toast.LENGTH_SHORT).show();
}
}
customAdapater
public class FragmentpagerAdapter extends FragmentPagerAdapter {
public FragmentpagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragmentone();
case 1:
return new FragmentTwo();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
MainActivity.xml
public class CustomAdapter extends BaseAdapter implements View.OnClickListener {
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater = null;
boolean isPressed=false;
public CustomAdapter(Activity a, ArrayList d) {
/********** Take passed values **********/
activity = a;
data = d;
inflater = (LayoutInflater) activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public void onClick(View v) {
}
/*********
* Create a holder Class to contain inflated xml file elements
*********/
public static class ViewHolder {
public TextView text;
public ImageButton imageButton;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if (convertView == null) {
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.row_layout, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.station_name);
holder.imageButton = (ImageButton) vi.findViewById(R.id.favorite);
holder.imageButton.setBackgroundResource(R.drawable.off);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
if (data.size() <= 0) {
holder.text.setText("No Data");
} else {
DataModel dataModel = (DataModel) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(dataModel.getText());
// this is for overall row click
vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("row is click","row click"+position);
}
});
// this is for image button onclick
holder.imageButton.setOnClickListener(new View.OnClickListener() {
DataModel dataModel = (DataModel) data.get(position);
@Override
public void onClick(View v) {
if(dataModel.isselected()){
holder.imageButton.setBackgroundResource(R.drawable.off);
dataModel.setIsselected(false);
}else{
holder.imageButton.setBackgroundResource(R.drawable.on);
dataModel.setIsselected(true);
}
isPressed = !isPressed; // reverse
}
});
;
}
return vi;
}
}
fragmentone.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
>
</android.support.v4.view.ViewPager>
Fragmenttwo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#325633"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view">
</ListView>
</LinearLayout>
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ee2333">
</LinearLayout>
我需要在第二个片段中发送 ArrayList name; ..我使用了界面但我不知道如何在用户滑动或标签时获取该数组列表的值第二个片段中的标签视图?
答案 0 :(得分:1)
这里我发布了一些可能有用的代码,请看这个场景是这样的,
您的标签位于Activity
,因此点击和滑动事件将在那里处理,因此声明Interface
无效,因为您无法在Callback
中触发Tab
方法}滑动或点击,这样你可以在FragmentOne
中创建一个方法,它会返回ArrayList
,如下所示
public ArrayList<String> getData(){
return yourArrayList();
}
现在在FragmentTwo
中创建一个方法,该方法将从ArrayList
收到FragmentOne
,如下所示
public void setData(ArrayList<String> yourArrayList){
Toast.makeText(YourActivity.this,"ArrayList Size: "+yourArrayList.size(),Toast.LENGTH_SHORT).show();
}
现在关于如何引用Fragments
中的Activity
,您可以执行以下操作:
private static String makeFragmentName(int viewPagerId, int index) {
return "android:switcher:" + viewPagerId + ":" + index;
}
现在在Tab
滑动或点击上面的方法调用以引用您的片段,如下所示
ArrayList<String> yourArrayList = new ArrayList<>();
FragmentOne fragmentOne = getSupportFragmentManger().findFragmentByTag(makeFragmentName(viewPagerId,0))
if(fragmentOne != null){
// get your arraylist using method of FragmentOne
yourArrayList = fragmentOne.getData();
}
// refer your second fragment and set the above arraylist in that
FragmentTwo fragmentTwo = getSupportFragmentManger().findFragmentByTag(makeFragmentName(viewPagerId,1))
if(fragmentTwo != null){
fragmentTwo.setData(yourArrayList);
}
你完成了
见上面的0和1是你需要管理它的适配器中的片段索引 如果您需要进一步的帮助,请告诉我
答案 1 :(得分:0)
在您名为fragment
的第一个Fragmentone
中,执行以下操作:
if(callBack!=null){
callBack.sendData(yourDataArrayList);
}
执行此操作后,请参阅log
中的内容。