滚动recycler view
时出现问题。它总是滞后,不向上或向下滚动。当我看到logcat时,这是消息
我/编舞:跳过39帧!该应用程序可能在其主线程上做过多的工作。
我不知道出了什么问题,我真的很困惑。另外,我结合了https://github.com/viethoa/recyclerview-alphabet-fast-scroller-android中的快速滚动器库。除了延迟,工作真的很顺利。
以下是主要活动代码:
public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private View view_menu, view_click, view_list;
private ImageView btn_arrow_back;
RecyclerView mRecyclerView;
RecyclerViewFastScroller fastScroller;
private List<String> mDataArray;
private List<AlphabetItem> mAlphabetItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
fastScroller = (RecyclerViewFastScroller) findViewById(R.id.fast_scroller);
mAlphabetItems = new ArrayList<AlphabetItem>();
mDataArray = new ArrayList<String>();
mDataArray = DataHelper.getAlphabetData();
view_menu = (View) findViewById(R.id.menu_layout);
view_list = (View) findViewById(R.id.list_vendor2);
view_click = (View) view_menu.findViewById(R.id.click_vendor);
btn_arrow_back = (ImageView) view_list.findViewById(R.id.arrow_back);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
List<String> strAlphabets = new ArrayList<>();
for (int i = 0; i < mDataArray.size(); i++) {
String name = mDataArray.get(i);
if (name == null || name.trim().isEmpty())
continue;
String word = name.substring(0, 1);
if (!strAlphabets.contains(word)) {
strAlphabets.add(word);
mAlphabetItems.add(new AlphabetItem(i, word, false));
}
}
Log.d("isi_data"," "+mDataArray);
ViewCompat.setNestedScrollingEnabled(mRecyclerView,false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecyclerView.setAdapter(new RecyclerAdapter(mDataArray));
fastScroller.setRecyclerView(mRecyclerView);
fastScroller.setUpAlphabet(mAlphabetItems);
view_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view_menu.setVisibility(View.GONE);
view_list.setVisibility(View.VISIBLE);
}
});
btn_arrow_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view_list.setVisibility(View.GONE);
view_menu.setVisibility(View.VISIBLE);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
以下是适配器代码:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
implements RecyclerViewFastScroller.BubbleTextGetter {
private List<String> mDataArray;
public RecyclerAdapter() {
mDataArray = new ArrayList<String>();
}
public RecyclerAdapter(List<String> dataset) {
mDataArray = dataset;
}
@Override
public int getItemCount() {
if (mDataArray == null)
return 0;
return mDataArray.size();
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_layout, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("isi_data"," "+mDataArray.get(position));
holder.mTextView.setText(mDataArray.get(position));
}
@Override
public String getTextToShowInBubble(int pos) {
if (pos < 0 || pos >= mDataArray.size())
return null;
String name = mDataArray.get(pos);
if (name == null || name.length() < 1)
return null;
return mDataArray.get(pos).substring(0, 1);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.tv_alphabet);
}
}
}
如果需要XML代码,只需注释。如果在这里插入它太长了。 data helper
类仅用于添加数组列表并发送回inactivity
类。谢谢
答案 0 :(得分:0)
尝试:
recyclerView.setNestedScrollingEnabled(false);
如果可能,请使RecyclerView的所有元素具有相同的高度。并添加:
recyclerView.setHasFixedSize(true);
使用默认的RecyclerView图形缓存方法,并根据您的情况进行调整。您不需要第三方库:
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);