XSLT存档  

不及格的程序员-八神

 查看分类:  ASP.NET XML/XSLT JavaScripT   我的MSN空间Blog

问题
我的App分为两个数据源,本地和线上,纯在本地和纯在线上玩是没有问题的,但是本地和线上的数据源进行切换就会出现问题,对此我查阅了一些资料,加上自己的一些理解,在这里记录一下

原因
我的ViewPage2继承与FragmentStateAdapter,而FragmentStateAdapter继承于 RecyclerView.Adapter,所以他是可以使用RecyclrView的刷新方法,为什么数据源更改之后fragment没有刷新呢,通过查看资料:

 

 

 

 

可以看到在gcFragment方法中吧新加的和原来的ItemId进行对不,如果不同就删除,但是又因为在原来的代码中ItemId就是Position,所以就导致了数据更改后没有反应,

解决
下面是我的解决办法
重写getItemI()方法和containsItem()方法,给他一个你自己创造的itemId,这样数据源更改后,发现Itemdid不同,就会把不同的删除。

 

 

 

 

package com.bosd.GansuLogistics.UI.PutOn;

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<TaskInfoDetail_json_puton.CJsonDataBean> dataSource;
    public ScanWareInfoPagerAdapter(@NonNull FragmentActivity fragmentActivity,
                                    List<TaskInfoDetail_json_puton.CJsonDataBean> dataSource)
    {
        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;
    }


}
package com.bosd.GansuLogistics.UI.PutOn;

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));
    }
}
package com.bosd.GansuLogistics.UI.PutOn;

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

posted on 2022-09-21 09:42  不及格的程序员-八神  阅读(1113)  评论(0编辑  收藏  举报