问题
我的App分为两个数据源,本地和线上,纯在本地和纯在线上玩是没有问题的,但是本地和线上的数据源进行切换就会出现问题,对此我查阅了一些资料,加上自己的一些理解,在这里记录一下
原因
我的ViewPage2继承与FragmentStateAdapter,而FragmentStateAdapter继承于 RecyclerView.Adapter,所以他是可以使用RecyclrView的刷新方法,为什么数据源更改之后fragment没有刷新呢,通过查看资料:
可以看到在gcFragment方法中吧新加的和原来的ItemId进行对不,如果不同就删除,但是又因为在原来的代码中ItemId就是Position,所以就导致了数据更改后没有反应,
解决
下面是我的解决办法
重写getItemI()方法和containsItem()方法,给他一个你自己创造的itemId,这样数据源更改后,发现Itemdid不同,就会把不同的删除。
package com.bosd.GansuLogistics.UI.PutOn; package com.bosd.GansuLogistics.UI.PutOn; package com.bosd.GansuLogistics.UI.PutOn; ———————————————— ======================个性签名===================== 之前认为Apple 的iOS 设计的要比 Android 稳定,我错了吗? 下载的许多客户端程序/游戏程序,经常会Crash,是程序写的不好(内存泄漏?刚启动也会吗?)还是iOS本身的不稳定!!! 如果在Android手机中可以简单联接到ddms,就可以查看系统log,很容易看到程序为什么出错,在iPhone中如何得知呢?试试Organizer吧,分析一下Device logs,也许有用.
import com.bosd.GansuLogistics.Data.Entity.TaskInfoDetail_json_puton;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class ScanWareInfoPagerAdapter extends FragmentStateAdapter
{
public List
public ScanWareInfoPagerAdapter(@NonNull FragmentActivity fragmentActivity,
List
{
super(fragmentActivity);
this.dataSource = dataSource;
}
@NonNull
@Override
public Fragment createFragment(int position)
{
ScanWareInfoContentFragment fragment = new ScanWareInfoContentFragment(
position,
dataSource.get(position));
return fragment;
}
@Override
public int getItemCount()
{
return dataSource.size();
}
@Override
public boolean containsItem(long itemId)
{
for(TaskInfoDetail_json_puton.CJsonDataBean bean:this.dataSource )
{
int b = bean.scanedCount;
if(bean.hashCode()+b == itemId)
{
return true;
}
}
return false;
}
@Override
public long getItemId(int position)
{
TaskInfoDetail_json_puton.CJsonDataBean dataBean = this.dataSource.get(position);
long hashcode = dataBean.hashCode()+ dataBean.scanedCount;
return hashcode;
}
}
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.Iori.Util.CommUtil;
import com.Iori.Util.KeyboardUtil;
import com.bosd.GansuLogistics.Data.Entity.TaskInfoDetail_json_puton;
import com.bosd.GansuLogistics.Data.Entity.TaskInfo_json_puton;
import com.bosd.GansuLogistics.R;
import java.text.MessageFormat;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
public class ScanWareInfoContentFragment extends Fragment
{
int iPosition;
private TextView txtScanedCount;
private TaskInfoDetail_json_puton.CJsonDataBean dataSource;// = parentActivity.dataSource.cJsonData.get(iPosition);
public ScanWareInfoContentFragment(int position, TaskInfoDetail_json_puton.CJsonDataBean dataSource)
{
iPosition = position;
this.dataSource = dataSource;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
PutOnScanWareActivity parentActivity = (PutOnScanWareActivity) getActivity();
View v = super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.layout_items_put_on_scan_ware, container, false);
TextView txtProductName = view.findViewById(R.id.txtProductName);
TextView txtNum = view.findViewById(R.id.txtNum);
TextView txtWareCode = view.findViewById(R.id.txtWareCode);
TextView txtSpec = view.findViewById(R.id.txtSpec);
TextView txtBatchNo = view.findViewById(R.id.txtBatchNo);
TextView txtDProduct = view.findViewById(R.id.txtDProduct);
TextView txtDExp = view.findViewById(R.id.txtDExp);
TextView txtProducter = view.findViewById(R.id.txtProducter);
TextView txtWillbeCount = view.findViewById(R.id.txtWillbeCount);
txtScanedCount = view.findViewById(R.id.txtScanedCount);
txtProductName.setText(dataSource.cWareName);
txtNum.setText(dataSource.cOrder);
txtWareCode.setText(dataSource.cWareCode);
txtSpec.setText(dataSource.cSpec);
txtBatchNo.setText(dataSource.cBatchNO);
txtDProduct.setText(dataSource.dProduct);
txtDExp.setText(dataSource.dExp);
txtProducter.setText(dataSource.cProducter);
txtWillbeCount.setText(dataSource.nQntty);
txtScanedCount.setText(MessageFormat.format("{0,number,#}", dataSource.scanedCount));
ViewPager2 pager = parentActivity.findViewById(R.id.pager);
ScanWareInfoPagerAdapter adapter = (ScanWareInfoPagerAdapter)pager.getAdapter();
ImageButton btnEdit = view.findViewById(R.id.btnEdit);
btnEdit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
AlertDialog dialog = CommUtil.showDialog(
ScanWareInfoContentFragment.this.getContext(),
R.layout.dialog_layout_modify_scan_count_puton,
new CommUtil.IoriButtonClickListener()
{
@Override
public void btnOK_click(DialogInterface dialog, Button sender)
{
View dialogView = sender.getRootView();
try
{
EditText txtCount = dialogView.findViewById(R.id.txtScanedCount);
String strCount = txtCount.getText().toString().trim();
dataSource.scanedCount = Integer.parseInt(strCount);
//adapter.notifyDataSetChanged();
adapter.notifyItemChanged(iPosition);
//updateContent(dataSource.scanedCount);
dialog.dismiss();
}
catch (Exception ex)
{
CommUtil.alert(ScanWareInfoContentFragment.this.getContext(), null, ex.getMessage());
}
}
});
TextView txtEnabledCount = dialog.findViewById(R.id.txtEnabledCount);
txtEnabledCount.setText(dataSource.nQntty);
final EditText txtCount = dialog.findViewById(R.id.txtScanedCount);
txtCount.setText(MessageFormat.format("{0,number,#}",dataSource.scanedCount));
txtCount.postDelayed(new Runnable()
{
@Override
public void run()
{
KeyboardUtil.showKeyboard(txtCount);
}
}, 500);
KeyboardUtil.enabledDialogInput(dialog);
}
});
return view;
}
public void updateContent(int scanedCount)
{
txtScanedCount.setText(MessageFormat.format("{0,number,#}", scanedCount));
}
}
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.Iori.Util.EventLocker;
import com.bosd.GansuLogistics.Data.Entity.TaskInfoDetail_json_puton;
import com.bosd.GansuLogistics.R;
import com.bosd.GansuLogistics.UI.Common.CameraScanActivity;
import java.util.ArrayList;
import java.util.List;
public class PutOnScanWareActivity extends CameraScanActivity
{
public TaskInfoDetail_json_puton dataSource;
private PageChangeCallback pageChangeCallback = new PageChangeCallback(this);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_put_on_scan_ware);
setTitle("选品上架");
setCustomActionBar();
//
dataSource = (TaskInfoDetail_json_puton) getIntent().getExtras().getSerializable("dataSource");
ScanWareInfoPagerAdapter adapter = new ScanWareInfoPagerAdapter(this, dataSource.cJsonData);
ViewPager2 pager = findViewById(R.id.pager);
pager.registerOnPageChangeCallback(this.pageChangeCallback);
pager.setAdapter(adapter);
}
@Override
protected void onResume()
{
super.onResume();
initCamera();
}
@Override
protected void onDestroy()
{
super.onDestroy();
ViewPager2 pager = findViewById(R.id.pager);
pager.unregisterOnPageChangeCallback(this.pageChangeCallback);
}
public void btnOK_click(View sender)
{
EventLocker.lock(sender);
ViewPager2 pager = findViewById(R.id.pager);
ScanWareInfoPagerAdapter adapter = (ScanWareInfoPagerAdapter)pager.getAdapter();
adapter.dataSource.remove(0);
adapter.notifyDataSetChanged();
}
public static class PageChangeCallback extends ViewPager2.OnPageChangeCallback
{
private PutOnScanWareActivity parentActivity;
public PageChangeCallback(PutOnScanWareActivity activity)
{
super();
parentActivity = activity;
}
@Override
public void onPageSelected(int position)
{
super.onPageSelected(position);
TextView txtPosition = this.parentActivity.findViewById(R.id.txtPosition);
txtPosition.setText("");
}
}
}
版权声明:本文为CSDN博主「zhuQuA」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42780052/article/details/115344744南来地,北往的,上班的,下岗的,走过路过不要错过!
对于博客园里的网友,不敢称为叫"程序员"的人,你们攻击性太强,看来你们是不会想到我的用意的.园子里有不少人都非常喜欢Jeffrey,是因为它的第一版 框架设计 CLR via C#.
可是从第一版到现在的第三版,没有看到真正底层的东西,内容仅仅是比MSDN文档更丰富一些,可能是我的要求太高了吧.
也就是因为它很多时候会接触到微软开发人员,会经常聊聊某些问题而已,而它又将这些问题反应到书中.也许它就像一个小记者.
它的年龄大我们不多,我的孩子与它小儿子一般大,如果我能向它那样出入微软与它们开发人员长时间交流,不仅仅会牛成它这样.....
可是微软的开发人员不会扔太多时间在它这儿的.所以它会整天追着这个,赶它那个..屁颠个不停吧...
而它的另一版被称为好书的 Windows核心编程,更是没有什么深度可言,仅仅是将windows提供的api,以及内核功能再重申了一遍.
这些书对晋及编程知识是有些贡献的,再说一遍我不是在匾低谁,说说想法而已.