我有5个片段,我想使用视图寻呼机并滑过。当我运行应用程序时,有5个可滑动的活动,但所有这些活动都是空白的。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CustomPagerAdapter(this));
}
public static class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}}
Blood.java
public class Blood extends Fragment {
public Blood(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_blood, container, false);
String [] pname = {"Dil Kapoor","Kunal Sharma","Manish Jumle","Vikram Sharma","Varun Surya",
"Vishant","Varsha","Vivek Rawat","Deepak Duggal","Vikas Chauhan","Piyush Pachori","Vishesh Sagar","Ramesh Kumar Jain","Pooja Sethi","Dusmanta Nayak"};
String [] phno={"Ph: 9810383305","Ph: 9911090955","Ph: 7053029216","Ph: 9717709787","Ph: 9910308217","Ph: 9958893461","Ph: 9999106564","Ph: 8527063021","Ph: 9910026715","Ph: 9999597059","Ph: 9654758509","Ph: 9999991434","Ph: 9910350461","Ph: 9868091610","Ph: " +
"9250079218"};
String [] btype = {"Blood Group: O+","Blood Group: AB-","Blood Group: AB+","Blood Group: A+","Blood Group: A+","Blood Group: AB-","Blood Group: B-","Blood Group: AB+","Blood Group: O-","Blood Group: O+","Blood Group: A1B+","Blood Group: AB-","Blood Group: B-","Blood Group: A1B-","Blood Group: A+"};
String[] pincode ={"110059","110058","110058","110059","110058","110056","110059","110058","110059","110060","110059","110060","110059","110058","110060"};
ArrayList<BloodObject> marray = new ArrayList<>();
RecyclerView mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_viewblood);
LinearLayoutManager mlLinearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mlLinearLayoutManager);
mRecyclerView.setHasFixedSize(true);
for(int i=0;i<pname.length;i++)
{
marray.add(new BloodObject(pname[i],phno[i],pincode[i],btype[i]));
}
BloodAdapter mBloodAdapter = new BloodAdapter(marray);
mRecyclerView.setAdapter(mBloodAdapter);
return view;
}}
HomeFragment.java
public class HomeFragment extends Fragment {
FloatingActionMenu fabMenu;
FloatingActionButton fab_search, fab_traker, fab_cart, fab_symptoms, fab_refils, fab_remainder;
RecyclerView recyclerView;
private ArrayList<CustomerHome> mList;
private CustomerHomePageAdapter mAdapter;
private String[] title = {"Order Medicines" , "Daily Use" , "Nutrition" , "Body Care" , "Baby Nutrition", "First Aid"};
private int[] imageResourceId = {R.drawable.ic_firstaid ,
R.drawable.ic_baby ,
R.drawable.ic_nutrition,
R.drawable.ic_cosmetics,
R.drawable.ic_nutrition,
R.drawable.ic_presription
};
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
mList = new ArrayList<>();
for(int i = 0 ; i<title.length ; i++)
{
mList.add(new CustomerHome(title[i],imageResourceId[i]));
}
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
mAdapter = new CustomerHomePageAdapter(getActivity() , mList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MainActivity.GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setAdapter(mAdapter);
fabMenu = (FloatingActionMenu) getActivity().findViewById(R.id.fab_menu);
fab_search = (FloatingActionButton) getActivity().findViewById(R.id.fab_search);
fab_traker = (FloatingActionButton) getActivity().findViewById(R.id.fab_health_tracker);
fab_cart = (FloatingActionButton) getActivity().findViewById(R.id.fab_cart);
fab_symptoms = (FloatingActionButton) getActivity().findViewById(R.id.fab_symptoms);
fab_refils = (FloatingActionButton) getActivity().findViewById(R.id.fab_refils);
fab_remainder = (FloatingActionButton) getActivity().findViewById(R.id.fab_remainder);
fab_remainder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Remainder Feature coming soon", Toast.LENGTH_SHORT).show();
}
});
fab_refils.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Refils Feature coming soon", Toast.LENGTH_SHORT).show();
}
});
fab_symptoms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Symptoms checker Feature coming soon", Toast.LENGTH_SHORT).show();
}
});
fab_traker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Health Tracker Feature coming soon", Toast.LENGTH_SHORT).show();
}
});
fab_cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Cart Feature coming soon", Toast.LENGTH_SHORT).show();
}
});
fab_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Search Feature coming soon", Toast.LENGTH_SHORT).show();
}
});
return view;
}}
Hospital.java
public class Hospital extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_hospital, container, false);
String hname[] ={" AIIMS "," Indraprastha Apollo Hospital"," Max Superspecialty Hospital"," Sir Ganga Ram Hospital"," Medanta - The Medicity",
" GB Pant Hospital"," Fortis Hospital"," Dr. Ram Manohar Lohia Hospital"," BLK Super Specialty Hospital"," Batra Hospital "};
String hspec[] ={" SPECIALISATION: Gal Bladder/Kidney Transplant/Cardiologist"," SPECIALISATION: Kidney Transplant/Cardiologist/Spine Chord Specialist"," SPECIALISATION: Cardiologist/ Spine Chord Specialist/Neurologist"," SPECIALISATION: Neurologist/Orthopedic/Liver Transplant"," SPECIALISATION: Liver Transplant/Orthopedic/Cardiologist",
" SPECIALISATION: Autograft/Skin Specialist/Spin Chord Specialist"," SPECIALISATION: Skin Specialist/Cardiologist/Cancer"," SPECIALISATION: Spine Chord Specialist/Skin Care/Orthopedic"," SPECIALISATION: Cardiologist/Skin Care/Spine Chord Specialist"," SPECIALISATION: Orthopedic/Cardiologist/Cancer"};
int hpin[] ={110029,110076,110017,110060,122001,110002,201301,110001,110005,110062};
String hcontno[] ={" Ph: 18602583010"," Ph: +91-11-29871090/1091"," Ph: +91 11 4055 4055"," Ph: +91 11-25750000"," Ph: +91 124 414 1414"," Ph: +91 11 2323 4242"," Ph: +911204300222"," Ph: 011-23365525"," Ph: +91 11 3040 3040"," Ph: 91-11-2995 8747"};
ArrayList<HospitalObject> marray = new ArrayList<>();
RecyclerView mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setHasFixedSize(true);
for(int i=0;i<hname.length;i++)
{
marray.add(new HospitalObject(hname[i],hspec[i],hpin[i],hcontno[i]));
}
HospitalAdapter mHospitalAdapter = new HospitalAdapter(marray);
mRecyclerView.setAdapter(mHospitalAdapter);
return view;
}}
这些是我编码的唯一片段。剩下的两个片段是空白的 我有一种强烈的感觉,我在片段中做错了。
请帮我解决这个问题
CustomPagerAdapter.java
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId()); }}
ModelObject.java
public enum ModelObject {
HOME(R.string.home, R.layout.fragment_home),
BLOOD(R.string.blood, R.layout.activity_blood),
HOSPITAL(R.string.hospital, R.layout.activity_hospital),
NEWS(R.string.news, R.layout.activity_news),
ABOUT(R.string.about, R.layout.activity_about_customer);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}}
答案 0 :(得分:0)
您可以尝试使用support.v4 Fragment; FragmentActivity; FragmentManager; FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private static final String TAG = LogUtil.makeLogTag(MainActivity.class);
private HomeFragment homeFragment;
private ChageFragment chageFragment;
private RankingFragment rankingFragment;
private AntivirusFragment antivirusFragment;
private ViewPager fragment_container;
private RadioGroup rg_tab;
private LocationInfoBean locatbean;
private ArrayList<Fragment> fragList = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initViewPager();
initListener();
}
private void initView() {
fragment_container = (ViewPager) findViewById(R.id.fragment_container);
rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
homeFragment = new HomeFragment();
antivirusFragment = new AntivirusFragment();
rankingFragment = new RankingFragment();
chageFragment = new ChageFragment();
fragList.add(homeFragment);
fragList.add(chageFragment);
fragList.add(rankingFragment);
fragList.add(antivirusFragment);
}
private void initViewPager() {
{
MyFragmentPagerAdapter fAdapter = new MyFragmentPagerAdapter(
getSupportFragmentManager());
fragment_container.setOffscreenPageLimit(3);
fragment_container.setAdapter(fAdapter);
fragment_container
.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
rg_tab.check(R.id.rb_home);
break;
case 1:
rg_tab.check(R.id.rb_chage);
break;
case 2:
rg_tab.check(R.id.rb_ranking);
break;
case 3:
rg_tab.check(R.id.rb_antivirus);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
fragment_container.setCurrentItem(0);
}
}
private void initListener() {
rg_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_home:
fragment_container.setCurrentItem(0);
break;
case R.id.rb_chage:
fragment_container.setCurrentItem(1);
break;
case R.id.rb_ranking:
fragment_container.setCurrentItem(2);
break;
case R.id.rb_antivirus:
fragment_container.setCurrentItem(3);
break;
}
}
});
}
private long aw;
@Override
public void onBackPressed() {
long time = new Date().getTime();
if (time - this.aw < 2000) {
finish();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
System.exit(0);
}
}, 200);
} else {
this.aw = time;
if (!isFinishing()) {
Toast.makeText(this,
getResources().getString(R.string.quithint),
Toast.LENGTH_SHORT).show();
}
}
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
return fragList.get(arg0);
}
@Override
public int getCount() {
return fragList.size();
}
}
}
它可能包含许多错误,替换为碎片。