我正在尝试为简单和科学的计算器创建一个带有两个片段的计算器。但是Viewpager是空的,虽然在两个片段之间可以滚动,但片段的内容是不可见的。 此外,如果我将IsViewFromObject更改为true,则两个片段都可以在彼此之上显示。
这是viewpager:
<?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.root.calculatorui.Science">
<EditText
android:inputType="none"
android:textIsSelectable="true"
android:gravity="right|bottom"
android:textSize="50sp"
android:background="#FFFFFF"
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="180dp" />
<android.support.v4.view.ViewPager
android:layout_below="@id/txtResult"
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这里是用于初始化ViewPager的fragmentActivity代码:
package com.example.root.calculatorui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
public class Science extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_for_calculator);
ViewPager viewPager=(ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Log.d("Load", "Started Science");
return new ScienceFragment();
case 1:
Log.d("Load", "Back to Simple");
return new SimpleFragment();
default:
return new SimpleFragment();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
}
}
这是simpleFragment:
<?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.root.calculatorui.Science">
<Button
android:text="C"
android:textColor="@android:color/holo_orange_dark"
android:layout_below="@+id/txtResult"
style="@style/Button"
android:id="@+id/buttonClear" />
<Button
android:text="←"
style="@style/Button"
android:id="@+id/buttonDel"
android:layout_below="@+id/txtResult"
android:layout_toRightOf="@+id/buttonClear"
android:layout_toEndOf="@+id/buttonClear" />
<Button
android:text="÷"
style="@style/Button"
android:id="@+id/buttonDiv"
android:layout_below="@+id/txtResult"
android:layout_toRightOf="@+id/buttonDel"
android:layout_toEndOf="@+id/buttonDel" />
<Button
android:text="x"
style="@style/Button"
android:id="@+id/buttonMul"
android:layout_toRightOf="@+id/buttonDiv"
android:layout_below="@+id/txtResult" />
<Button
android:text="7"
style="@style/Button"
android:id="@+id/button7"
android:layout_below="@+id/buttonClear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="8"
style="@style/Button"
android:id="@+id/button8"
android:layout_below="@+id/buttonDel"
android:layout_toRightOf="@+id/button7"
android:layout_toEndOf="@+id/button7" />
<Button
android:text="9"
style="@style/Button"
android:layout_below="@+id/buttonDiv"
android:layout_toRightOf="@+id/button8"
android:layout_toEndOf="@+id/button8"
android:id="@+id/button9" />
<Button
android:text="-"
android:textSize="30sp"
style="@style/Button"
android:layout_toRightOf="@+id/button9"
android:layout_toEndOf="@+id/button9"
android:id="@+id/buttonSub"
android:layout_below="@+id/buttonDiv" />
<Button
android:text="5"
android:id="@+id/btn5"
style="@style/Button"
android:layout_alignBottom="@+id/btn4"
android:layout_toRightOf="@+id/button7"
android:layout_toEndOf="@+id/button7"
android:layout_below="@+id/button7" />
<Button
android:text="4"
android:id="@+id/btn4"
style="@style/Button"
android:layout_below="@+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/btn6"
android:text="6"
style="@style/Button"
android:layout_below="@+id/button8"
android:layout_toRightOf="@+id/btn5"
android:layout_toEndOf="@+id/btn5" />
<Button
android:text="+"
android:id="@+id/btnPlus"
style="@style/Button"
android:layout_below="@+id/buttonSub"
android:layout_toRightOf="@+id/btn6"
android:layout_toEndOf="@+id/btn6" />
<Button
android:text="1"
android:id="@+id/btn1"
style="@style/Button"
android:layout_below="@+id/btn5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="2"
android:id="@+id/btn2"
style="@style/Button"
android:layout_below="@+id/btn5"
android:layout_toRightOf="@+id/btn4"
android:layout_toEndOf="@+id/btn4" />
<Button
android:text="3"
android:id="@+id/btn3"
style="@style/Button"
android:layout_below="@+id/btn6"
android:layout_toRightOf="@+id/btn2"
android:layout_toEndOf="@+id/btn2" />
<Button
android:text="%"
android:id="@+id/btnPercentage"
style="@style/Button"
android:layout_below="@+id/btn1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="0"
android:id="@+id/btn0"
style="@style/Button"
android:layout_below="@+id/btn2"
android:layout_toRightOf="@+id/btnPercentage"
android:layout_toEndOf="@+id/btnPercentage" />
<Button
android:text="."
android:textSize="40sp"
android:id="@+id/btnDot"
style="@style/Button"
android:layout_below="@+id/btn2"
android:layout_toRightOf="@+id/btn0"
android:layout_toEndOf="@+id/btn0" />
<Button
android:text="="
style="@style/Button"
android:background="@android:color/holo_orange_light"
android:id="@+id/buttonEquals"
android:layout_toRightOf="@+id/btn3"
android:layout_alignBottom="@+id/btnDot"
android:layout_below="@+id/btn6" />
</RelativeLayout>
这是SimpleFragment.Java
package com.example.root.calculatorui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by root on 14/1/17.
*/
public class SimpleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_main,container,false);
return view;
}
public SimpleFragment(){}
public Fragment newInstance(int s, String title) {
SimpleFragment sfrag=new SimpleFragment();
Bundle args = new Bundle();
args.putInt("someInt", s);
args.putString("someTitle",title);
sfrag.setArguments(args);
return sfrag;
}
}
答案 0 :(得分:0)
您没有在getItem(int position)
方法中创建片段的实例。
根据以下内容进行更改,它应该有效: -
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Log.d("Load", "Started Science");
return new ScienceFragment.newInstance("FirstFragment, Instance 1");
case 1:
Log.d("Load", "Back to Simple");
return new SimpleFragment.newInstance("SecondFragment, Instance 2");
default:
return new SimpleFragment.newInstance("DdefaultFragment, Instance default");
}
}
答案 1 :(得分:0)
我正在尝试使用两个片段创建一个简单的计算器 科学计算器
Fragment
s无法容纳其他片段: https://stackoverflow.com/a/6672688/4409113
如果您要创建两个Fragment
,请创建一个名为MyPagerAdapter
的单个类,然后找到MainActivity
(可以通过在Manifest中搜索来完成)< / em>初始化ViewPager
中的Activity
(例如MainActivity
中的),而不是Fragment
,然后将ViewPager
xml代码粘贴到Activity
布局中,而不是Fragment
。
请注意,您可以在MyPagerAdapter
内创建Activity
,但您需要更改代码...
(扩展到AppCompatActivity
等)。
然后你会好起来的!
答案 2 :(得分:0)
使用FragmentPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}}
并像这样设置viewpager
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ScienceFragment(), "Science");
adapter.addFragment(new SimpleFragment(), "Simple");
viewPager.setAdapter(adapter);
答案 3 :(得分:0)
在ScienceFragment中使用它,
public static ScienceFragment newInstance(int page, String title) {
ScienceFragment fragmentFirst = new ScienceFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
同样在SimpleFragment中,并在MainActivity中使用它。
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return ScienceFragment.newInstance(0, "Instance 1");
case 1: return SimpleFragment.newInstance(1, "Instance 2");
default: return ScienceFragment.newInstance(0, "Default");
}
}
@Override
public int getCount() {
return 2;
}
}
答案 4 :(得分:-1)
这是MainActivity Class。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager=(ViewPager)findViewById(R.id.vpPager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return ScienceFragment.newInstance(0, "Science");
case 1: return SimpleFragment.newInstance(1, "Simple");
default: return ScienceFragment.newInstance(0, "Default");
}
}
@Override
public int getCount() {
return 2;
}
}
}
This is ScienceFragment
/**
* Created by Techno Blogger on 15/1/17.
*/
public class ScienceFragment extends Fragment {
private String title;
private int page;
// newInstance constructor for creating fragment with arguments
public static ScienceFragment newInstance(int page, String title) {
ScienceFragment fragmentFirst = new ScienceFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_science, container, false);
TextView tvLabel = (TextView) view.findViewById(R.id.textScience);
tvLabel.setText(page + " -- " + title);
return view;
}
}
这是SimpleFragment
public class SimpleFragment extends Fragment {
// newInstance constructor for creating fragment with arguments
public static SimpleFragment newInstance(int page, String title) {
SimpleFragment fragmentFirst = new SimpleFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simple, null);
return rootView;
}
}