我正在创建一个RecyclerView
in Fragment
usingDiffUtil
并且AsyncListDiffer
因为我不想使用notifyDataSetChanged()
方法。但是我在使用这种方法时遇到了一个问题。我正在尝试从数据库中检索项目,但每次数据更改(增加)时,UI 都不会直接更新。我必须先关闭我的应用程序,然后再次打开它,或者去另一个班级检查更新的项目。
这是我的适配器:
public class AdapterSaham extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements SectionIndexer, SearchFilter {
public static final int SECTION_VIEW = 0;
public static final int CONTENT_VIEW = 1;
List<DaftarSaham> daftarSahamList;
private ArrayList<Integer> mSectionPositions;
private static int lastClicked = -1;
WeakReference<Context> mContextWeakReference;
private ItemClickListener itemClickListener;
private int lastPosition = -1;
int row_index = -1;
public AdapterSaham(ArrayList<DaftarSaham> daftarSaham, Context context, ItemClickListener clickListener){
this.daftarSahamList = daftarSaham;
this.mContextWeakReference = new WeakReference<Context> ( context );
this.itemClickListener = clickListener;
}
private static final DiffUtil.ItemCallback<DaftarSaham> DIFF_CALLBACK = new DiffUtil.ItemCallback<DaftarSaham> () {
@Override
public boolean areItemsTheSame(@NonNull DaftarSaham oldItem, @NonNull DaftarSaham newItem) {
return newItem.getTicker ().equals ( oldItem.getTicker () ) && newItem.getCompanyName ().equals ( newItem.getCompanyName () );
}
@SuppressLint("DiffUtilEquals")
@Override
public boolean areContentsTheSame(@NonNull DaftarSaham oldItem, @NonNull DaftarSaham newItem) {
return newItem.equals ( oldItem );
}
};
private AsyncListDiffer<DaftarSaham> differ = new AsyncListDiffer<DaftarSaham> (this, DIFF_CALLBACK );
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context ctx = mContextWeakReference.get ();
View view = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.list_item,parent,false );
return new ItemViewHolder ( view, ctx , itemClickListener);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Context ctx = mContextWeakReference.get ();
if (ctx == null){
return;
}
ItemViewHolder itemViewHolder= (ItemViewHolder) holder;
DaftarSaham daftarSaham = differ.getCurrentList ().get ( position);
itemViewHolder.txtTickerSaham.setText ( daftarSaham.getTicker () );
itemViewHolder.txtCompanyName.setText ( daftarSaham.getCompanyName () );
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView txtTickerSaham, txtCompanyName;
private LinearLayout cardView;
ItemClickListener itemClickListener;
public ItemViewHolder(@NonNull View itemView, final Context context, ItemClickListener itemClickListener) {
super ( itemView );
txtTickerSaham = (TextView) itemView.findViewById ( R.id.tickerSaham );
txtCompanyName = (TextView) itemView.findViewById ( R.id.tv_company_name );
cardView = (LinearLayout) itemView.findViewById ( R.id.cvRecycler );
this.itemClickListener = itemClickListener;
}
}
@Override
public int getItemCount() {
return differ.getCurrentList ().size ();
}
public void submitList(List<DaftarSaham> newdataList){
differ.submitList ( newdataList );
daftarSahamList = new ArrayList<> (newdataList);
}
片段中的代码:
ViewGroup.LayoutParams params=recyclerViewIndexer.getLayoutParams();
params.height= h;
recyclerViewIndexer.setLayoutParams(params);
recyclerViewIndexer.setHasFixedSize ( true );
recyclerViewIndexer.setLayoutManager ( new LinearLayoutManager ( getContext ()) );
StringRequest stringRequest = new StringRequest ( Request.Method.GET, URL,
new Response.Listener<String> () {
@Override
public void onResponse(String response) {
ArrayList<HashMap<String,String>> list = new ArrayList<> ();
try {
JSONObject jsonObject = new JSONObject (response);
JSONArray jsonArray = jsonObject.getJSONArray ( "data_emiten" );
for(int i = 0; i < jsonArray.length (); i++){
JSONObject jo = jsonArray.getJSONObject ( i );
String ticker = jo.getString ( "ticker" );
String companyName = jo.getString ( "companyName" );
daftarNonSection.add ( new DaftarSaham (ticker, companyName, false) );
}
} catch (JSONException e) {
e.printStackTrace ();
}
getHeaderListLetter ( daftarSahamList,daftarNonSection );
adapterSaham = new AdapterSaham ( daftarSahamList, getContext (), new AdapterSaham.ItemClickListener () {
@Override
public void onItemClick(Integer numbers, boolean isSelected) {
String ticker = "";
if (isSelected ){
ticker = daftarSahamList.get ( Integer.valueOf ( numbers) ).ticker;
}
SlidingTab slidingTab = (SlidingTab) getActivity ();
slidingTab.setTicker (ticker);
}
} );
recyclerViewIndexer.setAdapter ( adapterSaham );
adapterSaham.submitList ( daftarSahamList );
Log.d ( "TAG", "onResponse: "+ response );
}
},
new Response.ErrorListener () {
@Override
public void onErrorResponse(VolleyError error) {
Log.d ( "TAG", "onErrorResponse: "+error );
}
} );
int socketTimeOut = 50000;
RetryPolicy policy = new DefaultRetryPolicy (socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy ( policy );
RequestQueue requestQueue = Volley.newRequestQueue ( getContext () );
requestQueue.add ( stringRequest );
}
我的模型:
public class DaftarSaham implements Comparable<DaftarSaham> {
public String ticker;
public String companyName;
public boolean isSection;
private boolean isSelected = false;
public DaftarSaham() {
}
public DaftarSaham(String ticker, String companyName, boolean isSection) {
this.ticker = ticker;
this.companyName = companyName;
this.isSection = isSection;
}
public String getCompanyName() {
return companyName;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getTicker() {
return ticker;
}
}
我不知道为什么它不起作用,因为我之前尝试过使用DiffUtil
并且AsyncListDiffer
没有问题。任何帮助或建议都会很棒。提前致谢。