android - 如何在 PagerAdapter 中覆盖 getItemPosition
问题描述
我试图通过覆盖 getItemPosition() 来更新位置值,但它不起作用。请帮忙..
代码在这里 -
@Override
public int getItemPosition(Object object) {
super.getItemPosition(object);
SharedPreferences pos = ctx.getSharedPreferences("forposition", MODE_PRIVATE);
int tom = pos.getInt("pos",0);
Log.d("posTom","value : "+tom );
return tom;
}
这是 instantiateItem() 方法:
public Object instantiateItem(@NonNull ViewGroup container, int position) {
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.swipe,container,false);
ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
SharedPreferences sp = ctx.getSharedPreferences("your_shared_pref_name", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if((sp.getString("array", null)) == null) {
String temp = Arrays.toString(count);
editor.putString("array", temp);
editor.commit();
}
String fetch = sp.getString("array", null);
Log.d("positionlist ","value : "+fetch);
fetch = fetch.substring(1, fetch.length()-1);
String strarray[] =fetch.split(", ") ;
/* SharedPreferences spt= ctx.getSharedPreferences("your_shared_pref_name", MODE_PRIVATE);
int post = spt.getInt("pos",1);*/
Log.d("position","value : "+position );
Log.d("posvalue","value : "+strarray[position] );
Log.d("poscondition","value : "+(Integer.parseInt(strarray[position]) == 0) );
if (Integer.parseInt(strarray[position]) == 0) {
BackgroundTask task = new BackgroundTask(this);
task.execute();
imageView.setImageResource(img[position]);
container.addView(v);
strarray = setValue(position, strarray);
String temp1 = Arrays.toString(strarray);
editor.putString("array", temp1);
editor.commit();
} else {
imageView.setImageResource(img[position]);
container.addView(v);
}
SharedPreferences pos = ctx.getSharedPreferences("forposition", MODE_PRIVATE);
SharedPreferences.Editor poseditor = pos.edit();
poseditor.putInt("pos", position);
poseditor.commit();
return v;
}
请给出解决方案如何从 instantiateItem() 方法更新位置。
解决方案
推荐阅读
- angular - 文件与仅在外壳中包含的文件名不同:从 Angular 7 更新到 8
- java - 单击片段android时收到oom错误代码
- javascript - 未捕获的 ReferenceError js
- sql-server - SQL Server:选择 WHERE 字段可能不包含 VALUE
- tensorflow - AttributeError:“节点”对象没有属性“输出掩码”(NER)
- c# - 从字符串转换为唯一标识符时转换失败 - 使用 ExecuteReader 加载 DataTable
- google-apps-script - 在 Google Sheet Query 中发布格式化日期值
- python - 使用多处理读取大文件并创建字典?(Python)
- angular - 材料表排序分页和过滤所有三个不起作用
- ios - Xamarin iOS:检查设备上是否安装了 Google fit 应用