我在导航抽屉里面使用ViewPager,除了棉花糖设备以外,所有设备都能正常工作。在棉花糖设备中,当我点击切换按钮或滑动导航抽屉(在两个方向上)感觉流动中卡住(毛刺),同时当我将viewpager滑入不同的标签时也发现了卡住。这仅在棉花糖装置中发现。请帮助我,提前谢谢....
这是我的代码
活动类
public class HomeActivity extends AppCompatActivity{
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeViewPagerFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new HomeViewPagerFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
}
return true;
}
});
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
ViewPager片段
public class HomeViewPagerFragment extends Fragment {
ArrayList<Model> mArrayList= new ArrayList<>();
Model model;
TabLayout tabLayout;
ViewPager viewPager;
HomeViewPagerAdapter viewPagerAdapter;
public HomeViewPagerFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_view_pager,container,false);
viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
// parsing and add datas into mArrayList
viewPagerAdapter = new HomeViewPagerAdapter(getActivity().getSupportFragmentManager(),mArrayList);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOffscreenPageLimit(2);
// Give the TabLayout the ViewPager
viewPager.setCurrentItem(Credentials.homeTabSelected);
tabLayout = (TabLayout)rootView.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab){
Credentials.homeTabSelected = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return rootView;
}
}
viewpager适配器
public class HomeViewPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Model> mArrayList = new ArrayList<>();
String tabTitles[] = new String[] { "tab1","tab2" };
public HomeViewPagerAdapter(FragmentManager fm, ArrayList<Model> arrayList) {
super(fm);
mArrayList = arrayList;
}
@Override
public Fragment getItem(int position) {
if(position==0) {
return new HomeCentralFragment();
}
else {
return HomeRecyclerFragment.newInstance(mArrayList);
}
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
HomeCentralFragment
public class HomeCentralFragment extends Fragment {
public HomeCentralFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_central,null);
//my code is here(buttons, click events etc...)
return rootView;
}
}
HomeRecyclerFragment
public class HomeRecyclerFragment extends Fragment {
RecyclerView recyclerView;
public static ArrayList<Model> mArrayList =new ArrayList<>();
Fragment fragment;
FragmentTransaction fragmentTransaction;
public static HomeRecyclerFragment newInstance(ArrayList<Model> arrayList){
HomeRecyclerFragment fragment = new HomeRecyclerFragment();
mArrayList = arrayList;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_recycler_view,null);
recyclerView = (RecyclerView)rootView.findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
//to make horizontal or vertical listview using recyclerview
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter adapter = new HomeRecyclerAdapter(getActivity(), mArrayList);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child != null && gestureDetector.onTouchEvent(e)) {
Credentials.selectedPolititian = rv.getChildAdapterPosition(child);
Intent intent = new Intent(getContext(), PersonDetailActivity.class);
intent.putExtra("Class","HomeActivity");
intent.putExtra("array",mArrayList);
startActivity(intent);
getActivity().finish();
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
return rootView;
}
}
HomeRecyclerAdapter
public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> {
private ArrayList<Model> profiles;
Context mContext;
public HomeRecyclerAdapter(Context context, ArrayList<Model> profile) {
this.profiles = profile;
this.mContext = context;
}
@Override
public HomeRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_fragment_home_recycler, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(HomeRecyclerAdapter.ViewHolder viewHolder, int i) {
viewHolder.name.setText(profiles.get(i).getmName());
int length= profiles.get(i).getmDepartment().size();
int k;
String res ="";
for (k=0;k<length;k++){
res = res + profiles.get(i).getmDepartment().get(k)+"\n";
}
viewHolder.department.setText(res);
}
@Override
public int getItemCount() {
return profiles.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name, department;
//private ImageView imageView;
public ViewHolder(View view) {
super(view);
// imageView = (ImageView) view.findViewById(R.id.image);
name = (TextView) view.findViewById(R.id.p_name);
department = (TextView) view.findViewById(R.id.p_department);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
}
});
}
}
}
答案 0 :(得分:0)
检查视图片段布局,避免.jpeg图像(使用.png图像)... 使用一些简单的片段检查视图寻呼机,如果它可以顺利地检查布局视图..