当在SettingsActivity中打开以仅显示选中的复选框并且我的列表包含近30个项目时,recyclerview会正常滚动,但是当我使用带有900个项目的列表时 - recyclerview会滚动冻结。我更新recyclerview的方法是错误的? Android Studio日志:
E/RecyclerView: No adapter attached; skipping layout
I/Choreographer: Skipped 68 frames! The application may be doing too much work on its main thread.
BikesAdapter:
public class BikesAdapter extends RecyclerView.Adapter<BikesAdapter.BikesViewHolder>{
private static final String PREFS_NAME = "bikes_prefs";
public static List<Categories> categories;
private Context context;
public BikesAdapter(Context context) {
this.context = context;
notifyDataSetChanged();
}
@Override
public BikesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BikesViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_bikes, parent, false));
}
@Override
public void onBindViewHolder(final BikesViewHolder holder, final int position) {
final SharedPreferences setFavoritesCnB = FragmentBikesListXT.context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
boolean isFavoriteChB = setFavoritesCnB.getBoolean(categories.get(position).title, false);
holder.cbStars.setChecked(isFavoriteChB);
holder.cbStars.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = setFavoritesCnB.edit();
editor.putBoolean(categories.get(position).title, holder.cbStars.isChecked());
editor.apply();
if (holder.cbStars.isChecked()) {
openDBHelper.save(position);
Constants.CATEGORIES.add(categories.get(position));
} else {
openDBHelper.delete(position);
Constants.CATEGORIES.remove(categories.get(position));
}
}
});
if (checkSwitch) {
if (!isFavoriteChB) {
holder.cardView.setVisibility(View.GONE);
} else {
holder.textTitle.setText(categories.get(position).title);
}
} else holder.textTitle.setText(categories.get(position).title);
holder.textTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(categories.get(position).link));
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return categories.size();
}
public class BikesViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
private CheckBox cbStars;
private TextView textTitle;
public BikesViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cardView);
cbStars = (CheckBox) itemView.findViewById(R.id.cbStars);
textTitle = (TextView) itemView.findViewById(R.id.tv_title_bike);
}
}
}
使用recyclerview进行分段:
public class FragmentBikesListXT extends Fragment {
public static RecyclerView rvBikes;
public static ProgressBar pbBikes;
public static Context context;
public static CreateDBHelper dbHelper;
public static OpenDBHelper openDBHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_bikes_list_xt, container, false);
rvBikes = (RecyclerView) layout.findViewById(R.id.rvBikes);
pbBikes = (ProgressBar) layout.findViewById(R.id.pbBikes);
SharedPreferences setSwitch = getActivity().getSharedPreferences(PREFS_LIST, MODE_PRIVATE);
checkSwitch = setSwitch.getBoolean("NameOfThingToSave", false);
context = getContext();
dbHelper = new CreateDBHelper(this.getActivity());
openDBHelper = new OpenDBHelper();
initIO(Constants.URL_FULL_LIST);
return layout;
}
public void initIO(String url) {
ParseXT.initializeData(url);
rvBikes.setHasFixedSize(true);
rvBikes.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
}
}
SettingsActivity:
public class SettingsActivity extends AppCompatActivity {
private Switch mSwitch;
public static boolean checkSwitch;
public static final String PREFS_LIST = "list_prefs";
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mSwitch = (Switch) findViewById(R.id.switch_list);
SharedPreferences setSwitch = getSharedPreferences(PREFS_LIST, MODE_PRIVATE);
mSwitch.setChecked(setSwitch.getBoolean("NameOfThingToSave", false));
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
checkSwitch = true;
SharedPreferences.Editor editor = getSharedPreferences(PREFS_LIST, MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
}else{
checkSwitch = false;
SharedPreferences.Editor editor = getSharedPreferences(PREFS_LIST, MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.commit();
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
finish();
return super.onOptionsItemSelected(item);
}
}
OpenDBHelper:
public class OpenDBHelper implements IDBHelper {
private SQLiteDatabase db;
@Override
public void read() {
db = FragmentBikesListXT.dbHelper.getWritableDatabase();
BikesAdapter.categories.clear();
Cursor c = db.query(Constants.TABLE_BIKE, null, null, null, null, null, null);
if (c.moveToFirst()) {
int titleColIndex = c.getColumnIndex(Constants.FIELD_TITLE);
int linkColIndex = c.getColumnIndex(Constants.FIELD_LINK);
do {
BikesAdapter.categories.add(new Categories(
c.getString(titleColIndex),
c.getString(linkColIndex)));
Log.d("DBHelper", "READ ID = " + c.getString(titleColIndex));
} while (c.moveToNext());
} else
c.close();
}
@Override
public void save(int position) {
ContentValues cv = new ContentValues();
db = FragmentBikesListXT.dbHelper.getWritableDatabase();
cv.put(Constants.FIELD_TITLE, BikesAdapter.categories.get(position).title);
cv.put(Constants.FIELD_LINK, BikesAdapter.categories.get(position).link);
long size = db.insert(Constants.TABLE_BIKE, null, cv);
Log.d("DBHelper", "SAVE row inserted, size " + size + " ID = , " + BikesAdapter.categories.get(position).title);
FragmentBikesListXT.dbHelper.close();
}
@Override
public void delete(int position) {
db = FragmentBikesListXT.dbHelper.getWritableDatabase();
String[] whereArgs = {String.valueOf(BikesAdapter.categories.get(position).title)};
db.delete(Constants.TABLE_BIKE, Constants.FIELD_TITLE + " = ?", whereArgs);
Log.d("DBHelper", "DELETE ID = " + BikesAdapter.categories.get(position).title);
db.close();
}
}
解析器类:
public class ParseXT {
private static Elements titleTxt;
private static Elements linkTxt;
private static ArrayList<String> titleList = new ArrayList<>();
private static ArrayList<String> linkList = new ArrayList<>();
public static BikesAdapter bikesAdapter = new BikesAdapter(FragmentBikesListXT.context);
public static void initializeData(String url) {
new NewTreadParsed(url).execute();
}
public static class NewTreadParsed extends AsyncTask<List<Categories>, Void, List<Categories>> {
private String url;
public NewTreadParsed(String url) {
this.url = url;
}
@Override
protected List<Categories> doInBackground(List<Categories>... params) {
Document doc;
List<Categories> categories = new ArrayList<>();
try {
doc = Jsoup.connect(url).get();
titleTxt = doc.select(".topictitle");
linkTxt = doc.select(".a[href]");
titleList.clear();
linkList.clear();
for (Element contents : titleTxt) {
titleList.add(contents.text());
}
for (Element contents1 : titleTxt) {
Element element = contents1.select("a[href]").first();
linkList.add(element.attr("abs:href"));
}
}
catch (IOException e){
e.printStackTrace();
}
for (int i = 0; i < titleList.size(); i++) {
categories.add(new Categories(titleList.get(i), linkList.get(i)));
}
return categories;
}
@Override
protected void onPostExecute(List<Categories> categories) {
super.onPostExecute(categories);
FragmentBikesListXT.pbBikes.setVisibility(ProgressBar.INVISIBLE);
BikesAdapter.categories = categories;
FragmentBikesListXT.rvBikes.setAdapter(bikesAdapter);
}
}
}
答案 0 :(得分:0)
使用notifyDataSetChanged() 如果您正在编写适配器,则尽可能使用更具体的更改事件会更有效。依靠notifyDataSetChanged()